java - 使用 Spring Security,我如何使用 HTTP 方法(例如 GET、PUT、POST)来区分特定 URL 模式的安全性?

标签 java spring spring-security http-method

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/

相关文章:

java - 无法让 SHA-256 哈希与我的 Spring Security 一起使用

web-services - 如何使用Spring Security保护Grails Web服务

java - 启动位图

java - Java是否有计划用默认方法(java8)替代抽象类?

java - 从 XML 文件生成 Java 类的在线资源

java - Swing:马拉雅拉姆语字符串无法在 JFrame 中正确呈现

java - 初始化具有内部类类型字段的bean

java - Spring Cloud SQS 消费会阻塞,直到所有消息均已处理完毕

java - Spring 启动 : Mock user authentication with security context using Junit

tomcat - 缺少 Grails spring security plugin 插件?