【问题标题】:Thymeleaf spring security百里香弹簧安全
【发布时间】: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


    【解决方案1】:

    尝试将此添加到您的&lt;html&gt;

    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
    

    如果这不起作用,请尝试使用hasAuthority('&lt;somerole&gt;')

    【讨论】:

    【解决方案2】:

    hasRole(role) 似乎每次都返回 true。

    这可能是因为您忘记了模板文件中的 sec 命名空间。

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.springframework.org/security/tags">
    

    如果在输出文件中您仍然看到 'sec:authorize="hasRole('ROLE_ADMIN')"' 则说明您的方言配置有问题。

    如果没有,那么你必须调试。负责这些方法的类是

    org.springframework.security.access.expression.SecurityExpressionRoot
    

    扩展SecurityExpressionOperations 接口

    只需在您要测试的任何方法上添加一个断点,然后查看结果为何如此。

    boolean isAnonymous();
    boolean isAuthenticated();
    boolean isRememberMe();
    boolean hasRole(String role);
    boolean hasAnyRole(String... roles);
    boolean hasAuthority(String authority);
    ...
    and others
    

    【讨论】:

    • 我添加了。仍然每次都返回 true
    猜你喜欢
    • 2018-11-24
    • 2021-12-11
    • 2015-12-31
    • 2017-07-20
    • 2014-05-13
    • 2018-11-20
    • 2018-04-02
    • 2014-06-25
    相关资源
    最近更新 更多