sec:authorize的使用

一、在.html页面引入spring security的命名空间html

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

二、引入依赖spring

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

三、使用sec:authorize属性post

<body>
<p>点击<a th:href="@{/hello}">这里</a>打个招呼吧</p>
<form th:action="@{/logout}" method="post">
    <input type="submit" value="退出登录">
</form>
<p sec:authorize="hasRole('ROLE_admin')"><a th:href="@{/hello/helloAdmin}">管理员</a></p>
<p sec:authorize="hasRole('ROLE_normal')"><a th:href="@{/hello/helloUser}">普通用户</a></p>
</body>

四、从页面上显示当前登录的用户名code

<h1>欢迎光临
    当前登录帐号:<label th:text="${name}"></label>
    经过标签设置::<label sec:authentication="name"></label>
</h1>
相关文章
相关标签/搜索