【发布时间】:2016-09-17 14:28:36
【问题描述】:
我一直在尝试检查当前用户是否具有 thymeleaf-extras-springsecurity 的任何特定角色
<dependencies>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
在 servletcontext.xml 中
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
</bean>
在我看来,我一直在努力
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<body>
<!-- Template page showcasing C-EASY graphic elements -->
<div th:fragment="content">
home
<div sec:authorize="hasAuthority('ROLE_PORTEFEUILLE')">Show ROLE_PORTEFEUILLE</div>
<div sec:authorize="hasAuthority('ROLE_BOITEAOUTIL')">Show ROLE_BOITEAOUTIL.</div>
<div sec:authorize="hasAuthority('ROLE_SIMULER')">Show ROLE_SIMULER</div>
<div sec:authorize="hasAuthority('ROLE_SOUSCRIRE')">ShowROLE_SOUSCRIRE.</div>
<div sec:authorize="hasRole('ROLE_ADMINTESTING')">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>
<div sec:authorize="isAuthenticated()">THis user is authenticated </div>
<div sec:authorize="isAnonymous()">This user is anonymous </div>
<div sec:authentication="name">
The value of the "name" property of the authentication object should appear here.
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">
This content is only shown to administrators.
</div>
</div>
</body>
</html>
当前用户拥有除ROLE_BOITEAOUTIL以外的所有角色。
home
Show ROLE_PORTEFEUILLE
Show ROLE_BOITEAOUTIL.
Show ROLE_SIMULER
ShowROLE_SOUSCRIRE.
Show ROLE_SIMULER_TEST
仅当经过身份验证的用户具有角色 ROLE_ADMIN 时才会显示。
THis user is authenticated
This user is anonymous
The value of the "name" property of the authentication object should appear here.
此内容仅向管理员显示。
如何同时对用户进行身份验证和匿名? hasRole(role) 似乎每次都返回 true。 有什么问题,我该如何调试?
【问题讨论】:
标签: spring spring-security thymeleaf