java - Spring Security,访问 ="ROLE_ADMIN"与访问 ="hasAnyRole(' ROLE_ADMIN')

标签 java spring spring-security

在 Spring 安全中:

<sec:http pattern="/api/**" create-session="never"
        entry-point-ref="oauthAuthenticationEntryPoint"
        access-decision-manager-ref="accessDecisionManager"
        xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false" />
        <intercept-url pattern="/api/**" access="ROLE_ADMIN" />
        <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
        <access-denied-handler ref="oauthAccessDeniedHandler" />
</sec:http>

在这一行<intercept-url pattern="/api/**" access="ROLE_ADMIN" />

如果我写:有什么不同的含义:

<intercept-url pattern="/api/**" access="hasRole('ROLE_ADMIN')" />

或者:

<intercept-url pattern="/api/**" access="hasAnyRole('ROLE_ADMIN')" />

最佳答案

Spring Security documentation状态:

hasRole([role]): Returns true if the current principal has the specified role

hasAnyRole([role1,role2]): Returns true if the current principal has any of the supplied roles (given as a comma-separated list of strings).

此外,在 access 属性上,documentation状态:

access: Lists the access attributes which will be stored in the FilterInvocationSecurityMetadataSource for the defined URL pattern/method combination. This should be a comma-separated list of the security configuration attributes (such as role names).

但就您而言,您将单个元素列表传递给 hasAnyRole,因此:

access="ROLE_ADMIN" Vs access="hasAnyRole('ROLE_ADMIN')

hasRole('ROLE_ADMIN')hasAnyRole('ROLE_ADMIN') 相同,都意味着当前主体应具有 ROLE_ADMIN权威。

(“主体”通常是指可以在您的应用程序中执行操作的用户、设备或其他系统)。

关于java - Spring Security,访问 ="ROLE_ADMIN"与访问 ="hasAnyRole(' ROLE_ADMIN'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37511928/

相关文章:

Java/TCP 流量类

java - 对每个 API 调用进行身份验证

java - 如何用Java编写SQL查询从Snowflake获取数据

grails - 如何在 session 中保存Spring Security登录用户

java - 如何在 Netbeans 中创建可由用户编辑的提醒?

spring - 来自组合 Spring 数据规范的查询在同一个表上有多个连接

java - Spring Boot 嵌入式 HornetQ 集群不转发消息

spring - Spring 中的自定义注释处理器

java - 捕获 AuthenticationProvider 中抛出的异常

spring-boot - 如何在 Spring Security 中检查用户是否已登录或匿名