【问题标题】:Thymeleaf: sec:authentication shows up at ANY case while sec:authorize at NO ANY caseThymeleaf: sec:authentication 出现在 ANY case 而 sec:authorize 在 NO ANY case
【发布时间】:2019-03-12 00:12:09
【问题描述】:

无论是否登录,都会显示带有 sec:authentication="..." 的每个 <div>。即使是显式的false 也会导致div 出现。

另一方面,<div>s 和 sec:authorize="..." 是隐藏的,即使是显式的 true

我尝试检查 Maven 依赖项、Spring MVC 配置、ServletContextConfig 中的 Spring Security Dialect 以及许多其他答案,但没有适合我的解决方案。

index.html

<!DOCTYPE html>
<html
       xmlns:th="http://www.thymeleaf.org"
       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8"/>
    <title>blah blah</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
</head>

<body style="text-align: center;">    

<div sec:authentication="true">
    authentication - always
</div>

<div sec:authentication="false">
    authentication - never
</div>

<div class="container" sec:authentication="isAnonymous()">
    authentication - anonymous
</div>

<div class="container" sec:authentication="!isAnonymous()">
    authentication - not anonymous
</div>

<div class="container" sec:authentication="isAuthenticated()">
    authentication - authenticated
</div>

<div class="container" sec:authentication="!isAuthenticated()">
    authentication - not authenticated
</div>

<div sec:authorize="true">
    authorize - always
</div>

<div sec:authorize="false">
    authorize - never
</div>

<div class="container" sec:authorize="isAnonymous()">
    authorize - anonymous
</div>

<div class="container" sec:authorize="!isAnonymous()">
    authorize - not anonymous
</div>

<div class="container" sec:authorize="isAuthenticated()">
    authorize - authenticated
</div>

<div class="container" sec:authorize="!isAuthenticated()">
    authorize - not authenticated
</div>


<strong> Username: <span sec:authentication="name"></span> </strong>
<div th:text="${#authorization.getAuthentication()}">1</div>
<div th:text="${40}">1</div>




<!-- end of content! -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</body>
</html>

预期结果(未登录时):

身份验证 - 始终

身份验证 - 匿名

认证 - 未认证

授权 - 始终

授权 - 匿名

授权 - 未通过身份验证

用户名:匿名

40

实际结果

身份验证 - 始终

身份验证 - 从不

身份验证 - 匿名

身份验证 - 非匿名

认证 - 已认证

认证 - 未认证

用户名:

40

【问题讨论】:

    标签: java spring spring-security thymeleaf


    【解决方案1】:

    通过挖掘越来越多的解决方案,我找到了适合我的解决方案:

    1. SecurityConfig.configure() 上不得有类似 web.ignoring().antMatchers("/"); 的内容。
      IE。您要应用“授权过滤器”的页面不得设置为被安全忽略。
    2. index.html 中使用sec:authorize,而不是sec:authentication(这会导致错误)。

    工作index.html

    <!DOCTYPE html>
    <html
            xmlns:th="http://www.thymeleaf.org"
            xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
    <head>
        <meta charset="UTF-8"/>
        <title>bla bla bla</title>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    </head>
    
    <body style="text-align: center;">
    
    <div sec:authorize="true">
        authorize - always
    </div>
    
    <div sec:authorize="false">
        authorize - never
    </div>
    
    <div class="container" sec:authorize="isAnonymous()">
        authorize - anonymous
    </div>
    
    <div class="container" sec:authorize="!isAnonymous()">
        authorize - not anonymous
    </div>
    
    <div class="container" sec:authorize="isAuthenticated()">
        authorize - authenticated
    </div>
    
    <div class="container" sec:authorize="!isAuthenticated()">
        authorize - not authenticated
    </div>
    
    <strong> Username: <span sec:authentication="name"></span> </strong>
    <div th:text="${#authorization.getAuthentication()}">1</div>
    <div th:text="${40}">1</div>
    
    <!-- end of content! -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    </body>
    </html>

    结果: (登录时)

    授权 - 始终

    授权 - 非匿名

    授权 - 认证

    用户名:test2

    org.springframework.security.authentication.UsernamePasswordAuthenticationToken@00000000:主体:....

    40

    (未登录时)

    授权 - 始终

    授权 - 匿名

    授权 - 未通过身份验证

    用户名:匿名用户

    org.springframework.security.authentication.UsernamePasswordAuthenticationToken@00000000:主体:....

    40

    【讨论】:

      【解决方案2】:

      就我而言,将“Spring Security 5”与“thymeleaf-extras-springsecurity4”一起使用会导致此问题。如果您使用的是 Spring Security 5,请改用“thymeleaf-extras-springsecurity5”。 (最近发布了“thymeleaf-extras-springsecurity5”)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-30
        • 2013-08-30
        • 2013-08-21
        • 2018-03-30
        • 1970-01-01
        • 2019-02-22
        • 2018-11-18
        • 2016-05-26
        相关资源
        最近更新 更多