我问过relative question关于 hasAuthority() 和 hasRole() 以及 this part is clear 。 我尝试使用 group authorities 配置我的应用程序。如果我使用指令 .hasAuthority("ADMIN")
,配置对我有用http
.antMatcher("/monitor/console/**")
.authorizeRequests()
.antMatchers("/monitor/console/**")
.hasAuthority("ADMIN")
.... // other unimportant java code..
但是如果我将其更改为 .hasRole("ADMIN") 或 .hasRole("ROLE_ADMIN") 限制将不起作用。我可以检查 JdbcDaoImpl 服务中登录时加载的用户详细信息,并且我可以看到附加的权限列表,但我不知道为什么hasRole(..) 不起作用。我希望有人知道我需要在哪里使用断点来检查对象状态并比较结果或如何修复它。
我真的很困惑,因为当我使用单个权限表时,限制可以正常工作......
最佳答案
在 Spring 中,角色与权限相同。在 Spring Security 4 之前,它们的处理方式相同。 hasAuthority() 方法只是在内部调用了 hasRole() 方法。
现在,在 Spring Security 4 中,方法 hasRole() 将前缀 ROLE_ 添加到角色名称中,因此 hasAuthority('ROLE_ADMIN') 与 hasRole('ADMIN') 相同,因为 ROLE_ 前缀会自动添加。
您可以在此处查看更多内容。 http://docs.spring.io/spring-security/site/migrate/current/3-to-4/html5/migrate-3-to-4-jc.html#m3to4-role-prefixing
就您而言,我认为您需要在加载角色时将前缀 ROLE_ 添加到数据库中的角色。
关于java - Spring Security jdb通过组进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38358639/