Spring Security 引用声明:
You can use multiple elements to define different access requirements for different sets of URLs, but they will be evaluated in the order listed and the first match will be used. So you must put the most specific matches at the top. You can also add a method attribute to limit the match to a particular HTTP method (GET, POST, PUT etc.). If a request matches multiple patterns, the method-specific match will take precedence regardless of ordering.
如何配置 Spring Security,以便根据用于访问 URL 模式的 HTTP 方法对特定 URL 模式的访问进行不同的保护?
最佳答案
这只是关于配置。它说 <intercept-url>
元素将在您的 <http />
中从上到下进行评估配置文件的标签:
<http auto-config="true">
<intercept-url pattern="/**" access="isAuthenticated" />
<intercept-url pattern="/login.jsp" access="permitAll" />
</http>
在上面的示例中,我们试图只允许经过身份验证的用户访问所有内容,当然登录页面除外(用户必须先登录,对吧?!)。但是,根据文档,这行不通,因为不太具体的匹配项位于顶部。因此,实现此示例目标的(其中一个)正确配置是:
<http auto-config="true">
<intercept-url pattern="/login.jsp" access="permitAll" />
<intercept-url pattern="/**" access="isAuthenticated" />
</http>
将更具体的匹配放在顶部。
引用的最后一件事是关于 HTTP 方法。你可以用它来指定匹配,所以:
<http auto-config="true">
<intercept-url pattern="/client/edit" access="isAuthenticated" method="GET" />
<intercept-url pattern="/client/edit" access="hasRole('EDITOR')" method="POST" />
</http>
在第二个示例中,访问 /client/edit
通过 GET 用户只需要进行身份验证,但要访问 /client/edit
通过 POST(比如说,提交编辑表单),用户需要拥有 EDITOR
角色。在某些地方可能不鼓励使用这种 url 模式,但这只是一个示例。
关于java - 使用 Spring Security,我如何使用 HTTP 方法(例如 GET、PUT、POST)来区分特定 URL 模式的安全性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7347183/