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咯,很容易漏的。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂拧螺丝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值