gpt4 book ai didi

spring-security - Thymeleaf Spring 安全集成 sec :authorize is not working

转载 作者:行者123 更新时间:2023-12-02 03:15:42 25 4
gpt4 key购买 nike

我正在使用具有 Spring 安全性的 Thymeleaf 模板引擎。我还使用 Thymeleaf Spring Security 集成模块来使用 sec:authorize 功能,但由于某种原因它不起作用。我没有收到任何错误,但无论用户具有哪个角色,html div block 中的所有代码都会执行。

例如,当我以员工身份登录时,我还会看到“转至领导”和“转至系统”按钮,但我不希望员工看到这些按钮。

这是我的 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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>security</name>
<description>Demo project for Spring Boot security</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.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>

这是我的安全配置文件

package com.example.security.config;

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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private AuthenticationSuccessHandler authenticationSuccessHandler;

public SecurityConfig(AuthenticationSuccessHandler authenticationSuccessHandler) {
this.authenticationSuccessHandler = authenticationSuccessHandler;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
User.UserBuilder users = User.withDefaultPasswordEncoder();

auth.inMemoryAuthentication()
.withUser(users.username("john").password("john").roles("TEST", "EMPLOYEE"))
.withUser(users.username("mary").password("mary").roles("EMPLOYEE", "MANAGER"))
.withUser(users.username("susan").password("susan").roles("EMPLOYEE", "ADMIN"));
}

@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers("/").hasRole("EMPLOYEE")
.antMatchers("/leaders/**").hasRole("MANAGER")
.antMatchers("/systems/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/showmyloginpage")
.loginProcessingUrl("/authenticateuser")
//.successHandler(authenticationSuccessHandler)
.permitAll()
.and()
.logout()
.permitAll()
.and()
.exceptionHandling().accessDeniedPage("/access-denied");
}
}

这是我的主页html文件

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<title>Home page</title>
</head>
<body>
<p>Welcome to the home page!</p>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Logout" />
</form>
<form th:action="@{/showuser}" method="get">
<input type="submit" value="Show data" />
</form>

<div sec:authorize="hasRole('ROLE_MANAGER')">
<form th:action="@{/leaders}" method="get">
<input type="submit" value="GO to leaders" />
</form>
</div>

<div sec:authorize="hasRole('ROLE_ADMIN')">
<form th:action="@{/systems}" method="get">
<input type="submit" value="GO to systems" />
</form>
</div>

</body>
</html>

最佳答案

以下依赖项应该可以解决问题

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

同时更改您的 XML 命名空间

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"

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

关于spring-security - Thymeleaf Spring 安全集成 sec :authorize is not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56187017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com