Spring Security hasRole()不起作用
使用Spring Security && Thymeleaf时遇到问题,特别是在尝试使用hasRole表达式时。 ‘admin’用户有一个角色’ADMIN’但是hasRole('ADMIN')
无论如何都会解析为false
我的HTML:
1. 2. 3.true 4. 5. 6. IS ADMIN 7. IS ADMIN 8. 9.
结果是:
1.admin 2.[ADMIN] 3.true 4.true 5.ADMIN 6."prints nothing because hasRole('ADMIN') resolves to false" 7."prints nothing because hasRole(#vars.role_admin) resolves to false" 8.false 9.false
我在security.xml文件中启用了use-expressions
并且还在我的配置中包含了SpringSecurityDialect
我的pom.xml文件中的所有必需依赖项
org.springframework.security spring-security-core 4.0.1.RELEASE org.springframework.security spring-security-web 4.0.1.RELEASE org.springframework.security spring-security-config 4.0.1.RELEASE org.thymeleaf.extras thymeleaf-extras-springsecurity4 2.1.2.RELEASE compile
Role.java
@Entity @Table(name = "roles") public class Role implements Serializable { @Id @Enumerated(EnumType.STRING) private RoleType name; //... getters, setters }
角色类型
public enum RoleType { ADMIN }
User
有一组Role
为什么hasRole()
不起作用?
感谢您的帮助,谢谢
解决方法
th:if="${#strings.contains(#authentication.principal.authorities,'ADMIN')}"
尝试在HTML标记内使用hasAuthority
而不是hasRole
。
sec:authorize="hasAuthority('ADMIN')"
我有同样的问题从Spring Security 3.x升级到4.x. 将hasRole()
更改为hasAuthority()
为我做了诀窍。
http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#el-common-built-in
我最近遇到了同样的问题。 你需要做的是:
-
在你的html中添加以下语句:
(您可以根据您使用的内容在springsecurity4或springsecurity3之间进行更改)。
-
确保已将此资源添加到库中。 我正在使用gradle,但你可以和Maven一样。
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE'
-
在你的SpringWebConfiguration类或xml中,请确保为thymeleaf SpringSecurity添加方言:我正在使用java类进行配置。
@Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.addDialect(new SpringSecurityDialect()); return templateEngine; }
但你也可以定义为alexsource说: Spring安全和Thymeleaf不起作用
我希望这适合你! 问候!
你错过了一个概念:
- 如果使用
hasRole('ADMIN')
,则在您的ADMIN Enum
必须是ROLE_ADMIN
而不是ADMIN
。 - 如果您使用
hasAuthority('ADMIN')
,则您的ADMIN Enum
必须是ADMIN
。
在spring security中, hasRole()
与hasAuthority()
相同,但hasRole()
函数映射与Authority
没有ROLE_
前缀。
您可以在这篇文章中找到接受的答案: Spring Security中Role和GrantedAuthority之间的区别
请参阅官方文档。 http://www.thymeleaf.org/doc/articles/springsecurity.html
This content is only shown to administrators. This content is only shown to users.
您可以在没有${
… }
情况下尝试如下。
IS ADMIN
我相信你没有用ROLE_
作为前缀。 如果是这样,可以添加前缀,如下所示
IS ADMIN
我遇到了同样的问题,因为spring-security
4.0
。 由于某种原因, thymeleaf-extras-springsecurity4
与spring-security
4.0和thymeleaf
2.x
不兼容。 所以我将spring-security
版本降级到3.2.9.RELEASE
,它开始工作了。 如果你仍然想使用spring-security 4.0
,那么你可以尝试将thymeleaf-extras-springsecurity4
提升到3.0.0.RELEASE
和thymeleaf
到3.0
或者如果您使用的是spring boot应用程序,那么情况会变得更加棘手,那么剩下的唯一选择就是降级spring-security
或将spring boot版本升级到1.4.x(仍然在BETA中)
在您的特定方案中,进行以下pom更改应使hasRole正常工作
org.springframework.security spring-security-core 3.2.9.RELEASE org.springframework.security spring-security-web 3.2.9.RELEASE org.springframework.security spring-security-config 3.2.9.RELEASE org.thymeleaf.extras thymeleaf-extras-springsecurity4 2.1.2.RELEASE compile