【发布时间】: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