[学习笔记]springboot2.1.5整合SpringSecurity的sec标签无效的解决方案

94 篇文章 2 订阅
8 篇文章 0 订阅

Springboot整合SpringSecurity遇到的坑

今天在使用springboot2.1.5整合SpringSecurity过程中,发现sec:这个标签输出的语句好像无效,查阅很多资料发现好像是版本问题,但网上仅说2.0.x版本前如何除了,最后通过修改SpringSecurity的版本和html名称空间解决问题.

1.环境

首先说一下我的环境:

Spingboot 2.1.5

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.15.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

自动导入了thymeleaf模块

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--其中的版本是-->
   <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring5</artifactId>
      <version>3.0.11.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-java8time</artifactId>
      <version>3.0.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>

自动导入security模块

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--其中的版本是-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.1.16.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>5.1.11.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>5.1.11.RELEASE</version>
      <scope>compile</scope>
    </dependency>

2.遇到的问题

导入springsecurity4的名称空间,发现有自动补全,我以为我对了很开心

<!--如果没有认证-->
<div sec:authorize="!isAuthenticated()">
   <h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</a></h2>
</div>
<!--如果已经认证-->
<div sec:authorize="isAuthenticated()">
   <h2 align="center"><span sec:authentication="name"></span>您好,您的角色有:
      <span sec:authentication="principal.authorities"></span></h2>
   <form th:action="@{/logout}" method="post">
      <input type="submit" value="注销">
   </form>
</div>

在导入名称空间以后,进入对应网页,却发现没有任何效果,所有标签体原样输出

想了下,应该是版本问题

3.解决问题

在查阅了很多答案后,发现大家调用的都是springsecurity4的名称空间

但事实上,查阅文档发现,2.1.x版本后已经改成springsecurity5的名称空间了

https://github.com/thymeleaf/thymeleaf-extras-springsecurity

**Status**

This is a *Thymeleaf Extras* module, not a part of the Thymeleaf core (and as such following its own versioning schema), but fully supported by the Thymeleaf team.

This repository contains 3 projects:

- **thymeleaf-extras-springsecurity3** for integration with Spring Security 3.x
- **thymeleaf-extras-springsecurity4** for integration with Spring Security 4.x
- **thymeleaf-extras-springsecurity5** for integration with Spring Security 5.x

Current versions:

- **Version 3.0.4.RELEASE** - for Thymeleaf 3.0 (requires Thymeleaf 3.0.10+)
- **Version 2.1.3.RELEASE** - for Thymeleaf 2.1 (requires Thymeleaf 2.1.2+)

**Requirements (3.0.x)**

- Thymeleaf **3.0.10+**
- Spring Framework version **3.0.x** to **5.1.x**
- Spring Security version **3.0.x** to **5.1.x**
- Web environment (Spring Security integration cannot work offline). Works with both Spring MVC and Spring WebFlux.
**Namespace**
The namespace for all versions of this dialect is http://www.thymeleaf.org/extras/spring-security.

<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

很明显,名称空间发生了变化

然后根据说明文档可以看出,SpringSecurity需要注入的依赖是springsecurity5

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

修改了名称空间后的html,Sec也有自动补全提示了

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
     xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<!--如果没有认证-->
<div sec:authorize="!isAuthenticated()">
   <h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</a></h2>
</div>
<!--如果已经认证-->
<div sec:authorize="isAuthenticated()">
   <h2 align="center"><span sec:authentication="name"></span>您好,您的角色有:
      <span sec:authentication="principal.authorities"></span></h2>
   <form th:action="@{/logout}" method="post">
      <input type="submit" value="注销">
   </form>
</div>

输出一切正常,sec:这样的模板引擎有用了

4.总结(springboot2.1.x以上)

1.注入依赖要更改

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

2.名称空间需要更改

<html xmlns:th="http://www.thymeleaf.org"
     xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

很高兴解决了问题,有问题可以在评论去讨论哈

参考源码文档:https://github.com/thymeleaf/thymeleaf-extras-springsecurity

首先,需要在 pom.xml 中添加 slf4j 相关的依赖: ```xml <dependencies> <!-- SpringBoot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- slf4j 相关依赖 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> ``` 其中,slf4j-api 是 slf4j 的接口,logback-classic 是 slf4j 的实现。 接下来,需要在 src/main/resources 目录下添加 logback.xml 配置文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <!-- 控制台输出日志格式 --> <encoder> <pattern>%-5level %d{HH:mm:ss.SSS} [%thread] %logger{50} - %msg%n</pattern> </encoder> </appender> <appender name="file" class="ch.qos.logback.core.FileAppender"> <!-- 日志文件路径 --> <file>logs/myapp.log</file> <encoder> <!-- 日志文件输出日志格式 --> <pattern>%-5level %d{HH:mm:ss.SSS} [%thread] %logger{50} - %msg%n</pattern> </encoder> </appender> <root level="info"> <!-- 控制台输出日志级别 --> <appender-ref ref="console" /> <!-- 日志文件输出日志级别 --> <appender-ref ref="file" /> </root> </configuration> ``` 其中,控制台输出和日志文件输出的格式可以根据自己的需要进行调整。 最后,在代码中使用 slf4j 来输出日志: ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { private Logger logger = LoggerFactory.getLogger(HelloController.class); @GetMapping("/hello") public String hello() { logger.info("Hello, World!"); return "Hello, World!"; } } ``` 这里使用了 LoggerFactory 类来获取 Logger 对象,然后使用 Logger 对象输出日志。在上面的例子中,输出的日志级别是 info,所以只有 info 及以上级别的日志会被输出到控制台和日志文件中。如果需要输出 debug 或者其他级别的日志,可以在 logback.xml 中进行配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值