springboot+security+thymeleaf中sec:authorize不生效
作者:互联网
thymeleaf编写前端web页面时需要根据权限对一些菜单进行显示控制,后端使用springsecurity进行认证
写页面时控制新增按钮只有管理员界面出现
<span sec:authorize="hasRole('ADMIN')" ><button class="layui-btn layuiadmin-btn-useradmin" data-type="edit">编辑</button></span>
但sec:authorize=“hasRole(‘ADMIN’)”,没有生效。
解决方法:
SpringBoot2.*中SpringSecurity版本升级到了5.x版本
pom依赖中
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
<dependency>
改为
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
<dependency>
thymeleaf模板页面题头
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security4">
改为
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security5">
解决过程中看到一些用法也记录一下
<div sec:authorize="isAuthenticated()">
<p>已登录</p>
<p>登录名:<span sec:authentication="name"></span></p>
<p>Password:<span sec:authentication="principal.password"></span></p>
<div sec:authentication="principal.authorities"></div> <!-- works fine -->
<p>Name:<span sec:authentication="principal.username"></span></p>
<p>Status:<span sec:authentication="principal.status"></span></p>
</div>
标签:authorize,springboot,thymeleaf,extras,sec,RELEASE,页面 来源: https://blog.csdn.net/shalyniu/article/details/116707887