SpringBoot2.5.1整合thymeleaf以及springsecurity后sec:authorize无效的问题

2023-02-25 16:10:39 浏览数 (1)

sec:authorize无效的问题

  • 1、问题描述
  • 2、解决方案:
  • 3、扩充

1、问题描述

修改之前我的pom.xml文件如下,但是sec:authorize一直不生效。

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.atguigu</groupId>
    <artifactId>springboot-05-security</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-05-security</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
           <!-- <version>3.0.4.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、解决方案:

将springsecurity的版本切换到5就行

代码语言:javascript复制
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
           <!-- <version>3.0.4.RELEASE</version>-->
        </dependency>

然后在thymeleaf的模板开头引入:

代码语言:javascript复制
<html xmlns:th="https://www.thymeleaf.org"
	  xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

重新启动项目,发现sec:authorize生效了

3、扩充

我发现换成springsecurity5之后,模板引擎的sec:authorize相关的所有标签没有补全提示了,但还能用,影响不大

0 人点赞