亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Thymeleaf:sec:authentication 出現在任何情況

Thymeleaf:sec:authentication 出現在任何情況

瀟瀟雨雨 2021-11-03 11:03:58
無論是否登錄,每一個<div>都sec:authentication="..."顯示。即使是顯式也會false導致div出現。另一方面<div>s withsec:authorize="..."是隱藏的,即使是顯式的true.我曾嘗試檢查 Maven 依賴項、Spring MVC 配置、Spring Security DialectServletContextConfig和許多其他答案,但沒有解決方案適用于我的情況。索引.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>預期結果(未登錄時):身份驗證 - 始終身份驗證 - 匿名認證 - 未認證授權 - 始終授權 - 匿名授權 - 未認證用戶名:匿名40實際結果:身份驗證 - 始終身份驗證 - 從不身份驗證 - 匿名身份驗證 - 非匿名身份驗證 - 經過身份驗證認證 - 未認證用戶名:40
查看完整描述

2 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

就我而言,使用“Spring Security 5”和“thymeleaf-extras-springsecurity4”導致了這個問題。如果您使用的是 Spring Security 5,請改用“thymeleaf-extras-springsecurity5”。(最近發布了“thymeleaf-extras-springsecurity5”)


查看完整回答
反對 回復 2021-11-03
?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

通過挖掘越來越多的解決方案,我找到了一個適合我的解決方案:

  1. 不能有web.ignoring().antMatchers("/");at 之類的東西SecurityConfig.configure()。
    即,您要應用“授權過濾器”的頁面不得設置為被安全忽略。

  2. 使用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


查看完整回答
反對 回復 2021-11-03
  • 2 回答
  • 0 關注
  • 418 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號