gpt4 book ai didi

java - Spring Security 不适用于 "hasRole(' ROLE_ADMIN')"或 ROLE_ADMIN

转载 作者:搜寻专家 更新时间:2023-11-01 01:50:11 28 4
gpt4 key购买 nike

我正在使用 Spring Security 4.1 版。如果我在安全配置中指定access="hasRole('ROLE_ADMIN')"access="ROLE_ADMIN",我可以登录,但我无法访问我的管理页面。

<security:http use-expressions="true">
<security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
<!-- security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" / -->
<security:intercept-url pattern="/createmanufsensors" access="isAuthenticated()" />
</security:http>
<security:global-method-security secured-annotations="enabled"></security:global-method-security>

调试错误如下:

DEBUG [http-bio-8080-exec-10] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Secure object: FilterInvocation: URL: /admin; Attributes: [hasRole('ROLE_ADMIN')]     
2016-06-25 10:07:53,667 [] DEBUG [http-bio-8080-exec-10] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@cc305a73: Principal: org.springframework.security.core.userdetails.User@74b46745: Username: francatore ; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN ; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 7F702A6911A71EA5556C750B6D424FF5; Granted Authorities: ROLE_ADMIN
2016-06-25 10:07:53,667 [] DEBUG [http-bio-8080-exec-10] [org.springframework.security.access.vote.AffirmativeBased] Voter: org.springframework.security.web.access.expression.WebExpressionVoter@170ea084, returned: -1
2016-06-25 10:07:53,668 [] DEBUG [http-bio-8080-exec-10] [org.springframework.security.web.access.ExceptionTranslationFilter] Access is denied (user is not anonymous); delegating to AccessDeniedHandler

我可能遗漏了什么?

最佳答案

我对此有一个小的解释。在这里,您被认证为普通用户,但无权查看管理页面。

如果您正在使用 access="hasRole('ROLE_ADMIN')" 表达式,那么 Spring EL 类(即 SecurityExpressionRoot)将添加前缀 ROLE_ 到每个角色我们在 hasRole() 表达式中提供的。因此,在您的情况下,您在 hasRole('ROLE_ADMIN') 中提供的角色解析为 ROLE_ROLE_ADMIN

这就是为什么您被认证为具有 ROLE_ADMIN 的用户。但是要在Spring Security框架中查看admin页面的用户必须要有角色ROLE_ROLE_ADMIN(因为 SecurityExpressionRoot 类添加了 ROLE_ 前缀)。

因此,为此删除代码中的 ROLE_ 前缀,即此处 access="hasRole('ADMIN')"因此,Spring Security 将自动添加 ROLE_ 前缀。并确保您已将数据库中的管理员角色指定为 ROLE_ADMIN

关于java - Spring Security 不适用于 "hasRole(' ROLE_ADMIN')"或 ROLE_ADMIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030022/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com