SpringSecurity4+thymeleaf sec:authorize 标签使用无效
SpringSecurity4+thymeleaf sec:authorize 标签使用无效
近期在搭建SpringCloud项目中涉及到页面根据权限显示隐藏菜单的操作,后台配置啥的都搞好了,但是sec:authorize
标签放页面上不起作用,无奈纠结了很久,Baidu/Google半天终于发现问题了~直接开始吧
pom.xml引入相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
test.html代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
</head>
<body>
<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>Email :<span sec:authentication="principal.email"></span></p>
<p>Name:<span sec:authentication="principal.username"></span></p>
<p>Status:<span sec:authentication="principal.status"></span></p>
<p>拥有的角色:
<span sec:authorize="hasRole('ROLE_ADMIN')">管理员</span>
<span sec:authorize="hasRole('ROLE_USER')">用户</span>
</p>
</div>
<div sec:authorize="isAuthenticated()"><p>未登录</p></div>
</body>
</html>
test.html页面运行结果
额~没用,上面的已登录和未登录都显示出来了,sec:authorize这个标签无效,怎么肥事(纠结了我好久好久),改吧
修改pom.xml文件,添加properties
...
<properties>
<!-- upgrade to thymeleaf version 3 -->
<thymeleaf.version>3.0.8.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>
</properties>
<dependencies>
<dependency>
...
</dependency>
</dependencies>
...
再次运行项目,查看test页面结果
嗯~
不要忘记添加/设置themleaf相关的properties咯,很容易漏的。