thymeleaf与SpringSecurity整合

导包

<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

PS: 注意将springboot降到2.1.0以下版本,否则会有许多问题

html中使用

<div>
    <!--判断是否有登录,如果未登录-->
    <div sec:authorize="!isAuthenticated()">
        <a th:href="@{/login}">登录</a>
    </div>
    <!--如果已登录-->
    <div sec:authorize="isAuthenticated()">
        用户名:<span sec:authentication="name"></span>
    </div>
    <div sec:authorize="isAuthenticated()">
        <a th:href="@{/logout}">注销</a>
    </div>
</div>

PS:注意在中导入约束

<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

登出失败的可能原因

未关闭CSRF功能
在SecurityConfig中添加
http.csrf().disable();

PS: 跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF 或者 XSRF, 是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法。跟跨网站脚本(XSS)相比,XSS 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户网页浏览器的信任。

posted @ 2021-09-10 16:39  keacua  阅读(193)  评论(0)    收藏  举报