其他分享
首页 > 其他分享> > Spring Boot+Security+Thymeleaf sec:authorize-url 标签不生效

Spring Boot+Security+Thymeleaf sec:authorize-url 标签不生效

作者:互联网

用 SpringBoot + SpringSecurity + Thymeleaf 搭建了一个应用,发现 Thymeleaf sec:authorize-url 以及 sec:authorize="hasRole('ROLE_ADMIN')" 标签都不生效。
后来发现是 Maven 引入 thymeleaf-extras-springsecurity4 时没有指定版本号,直接使用是SpringBoot的版本。

解决方案:

在maven中把 thymeleaf-extras-springsecurity4 改成

<!--springsecurity4 要指定3.0以上版本,否则权限标签可能无法工作-->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.2.RELEASE</version>
</dependency>

然后 thymeleaf 模板中就可以这样使用了:

<div sec:authorize="hasRole('ROLE_ADMIN')">
  This content is only shown to administrators.
</div>


<div sec:authorize="hasRole('ROLE_USER')">
  This content is only shown to users.
</div>

但是又发现一个问题。在新版的springboot 2.1.x 中也会导致标签失效,暂时先用2.0.7及以下吧。

标签:authorize,url,Spring,Thymeleaf,thymeleaf,extras,sec,springsecurity4
来源: https://blog.csdn.net/qq_32252917/article/details/100766664