thymeleaf sec:授权在 spring boot 中不工作 [英] thymeleaf sec:authorize not working in spring boot
问题描述
我有一个带有 Thymeleaf 和内存认证的 Spring MVC 项目.在我的 html 中,我想显示当前登录的用户并仅在有人登录时显示注销按钮.
这里是一个简单的 html,它应该显示用户名,但总是显示 Bob 和一个只有在有人登录时才应该显示的文本,但它总是显示.
知道我做错了什么吗?
这是我的 pom.xml
<modelVersion>4.0.0</modelVersion><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/><!-- 从存储库中查找父级 --></父母><groupId>info.climbinggyms</groupId><artifactId>main</artifactId><version>0.0.1-SNAPSHOT</version><name>main</name><description>网站概述了现有的攀岩馆</description><属性><java.version>8</java.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>运行时</scope></依赖><依赖><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jdbc</artifactId></依赖><依赖><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId></依赖><依赖><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>运行时</scope></依赖><依赖><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖><!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api --><依赖><groupId>javax.mail</groupId><artifactId>邮件</artifactId><version>1.4.3</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.1.RELEASE</version></依赖><依赖><groupId>nz.net.ultraq.thymeleaf</groupId><artifactId>thymeleaf-layout-dialect</artifactId><version>2.3.0</version></依赖><依赖><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity4</artifactId></依赖><依赖><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring4</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></依赖></依赖项><构建><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build></项目>
我的 html:
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"xmlns:sec="http://www.thymeleaf.org/extras/spring-security"布局:装饰器=布局/root_layout"lang="zh-cn"><头><title>我的攀岩馆</title>头部><身体><div layout:fragment="page-content"><div class="容器"><部分><br><br><br><h1>我的攀岩馆</h1><p>欢迎来到我的攀岩馆</p><p>这个还在建设中,本站只包含虚拟数据</p><div sec:authorize="isAuthenticated()">此内容仅向经过身份验证的用户显示.
<span sec:authentication="name">Bob</span></节>