thymeleaf sec:授权在 Spring Boot 中不工作

Posted

技术标签:

【中文标题】thymeleaf sec:授权在 Spring Boot 中不工作【英文标题】:thymeleaf sec:authorize not working in spring boot 【发布时间】:2019-08-07 09:23:37 【问题描述】:

我有一个带有 Thymeleaf 和内存身份验证的 Spring MVC 项目。 在我的 html 中,我想显示当前登录的用户并仅在有人登录时显示注销按钮。

这是一个简单的 html,应该显示用户名,但总是显示 Bob 和一个应该只在有人登录时显示的文本,但它总是显示。

知道我做错了什么吗?

这是我的 pom.xml

<?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 http://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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>info.climbinggyms</groupId>
    <artifactId>main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>main</name>
    <description>website with an overview of the existing climbing gyms</description>

    <properties>
        <java.version>8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>



        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>



        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

                <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.1.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>



    </dependencies>

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

</project>

我的html:

<!DOCTYPE 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"
      layout:decorator="layout/root_layout"
      lang="en">
<head>
    <title>My Climbing Gyms</title>
</head>
<body>
    <div layout:fragment="page-content">
    <div class="container">
        <section>
            <br>
            <br>
            <br>
            <h1>My Climbing Gyms</h1>
            <p>Welcome to my climbing gyms</p>
            <p>This is still under construction, this site only contains dummy data</p>

            <div sec:authorize="isAuthenticated()">
                This content is only shown to authenticated users.
            </div>

            <span sec:authentication="name">Bob</span>


        </section>
    </div>
    </div>

</body>
</html>

和我的安全配置:

package info.climbinggyms.main;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter 

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        http
                .httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers("/admin/**").hasAnyRole("ADMIN","USER")
                .and()
                .csrf().disable().headers().frameOptions().disable()
                .and()
                .formLogin()
                .and()
                .logout();
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception 
        auth.inMemoryAuthentication()
                .withUser("bleau83").password("noopbleau83").roles("ADMIN")
                .and()
                .withUser("user").password("noopuser").roles("USER");
    

【问题讨论】:

【参考方案1】:

我将 thymeleaf 安全更新为 springsecurity5,现在它可以工作了

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

【讨论】:

这似乎也对我有用,虽然我不知道为什么。 @bleau83 我遇到了同样的问题。我在我的 html 中定义了 xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5" 并尝试从 sec:authentication="principal.fullname" 获取值,但我无法显示该值。我只通过[[$#request.userPrincipal.principal.fullName]] 展示它。我该如何解决我的问题?【参考方案2】:

我也遇到过类似的问题,我的implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' 完全从build.gradle 中丢失了。

【讨论】:

我遇到了同样的问题。我在我的 html 中定义了xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5" 并试图从sec:authentication="principal.fullname" 中获取值,但我无法显示该值。我只通过[[$#request.userPrincipal.principal.fullName]] 展示它。我该如何解决我的问题?【参考方案3】:

是的,您需要更新到 springsecurity5

thymeleaf-extras-springsecurity5

【讨论】:

这与 bleau83 给出的答案相同。考虑支持现有的答案,而不是重复它。

以上是关于thymeleaf sec:授权在 Spring Boot 中不工作的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot Security - Thymeleaf sec:授权不起作用

Spring Security - Thymeleaf - 我可以在 sec:authorize 标签中评估 SPEL 表达式吗?

如何使用 sec:authorize with thymeleaf 向特定用户授权 html 元素?

sec:authorize 不起作用 - Spring Boot 2、Thymeleaf 3、Thymeleaf Spring Security 5 集成包

sec:authorize 在 Spring 启动应用程序中使用 Thymeleaf 时未按预期工作

如何使用 Thymeleaf 配置 Spring Boot 并使用 sec:authentication 标签