2 回答
TA贡献1785条经验 获得超8个赞
就我而言,使用“Spring Security 5”和“thymeleaf-extras-springsecurity4”导致了这个问题。如果您使用的是 Spring Security 5,请改用“thymeleaf-extras-springsecurity5”。(最近发布了“thymeleaf-extras-springsecurity5”)
TA贡献1875条经验 获得超5个赞
通过挖掘越来越多的解决方案,我找到了一个适合我的解决方案:
不能有
web.ignoring().antMatchers("/");at 之类的东西SecurityConfig.configure()。
即,您要应用“授权过滤器”的页面不得设置为被安全忽略。使用
sec:authorize,而不是sec:authentication(这会导致误差)index.html。
工作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
(未登录时)
授权 - 始终
授权 - 匿名
授权 - 未认证
用户名:anonymousUser
org.springframework.security.authentication.UsernamePasswordAuthenticationToken@00000000:委托人:....
40
添加回答
举报
