为什么默认用户仍然创建甚至添加了EnableWebSecurity注释和InMemoryUserDetailsManager

问题描述 投票:0回答:1

我正在尝试创建一个使用自定义 UserAdmin 凭据进行身份验证的应用程序。但是,自定义用户凭据会出现“未经授权”异常。只有我可以使用系统生成的凭据用户生成的密码登录。 安全配置.java

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(requests -> requests.anyRequest().authenticated()) .formLogin(withDefaults()) .httpBasic(withDefaults()); return http.build(); } @Bean public InMemoryUserDetailsManager userDetailsService() { System.out.println("--------------------------------In UserDetailsService"); UserDetails user = User.withUsername("user") .password("{noop}password") .roles("USER") .build(); UserDetails admin = User.withUsername("admin") .password("{noop}password") .roles("USER", "ADMIN") .build(); return new InMemoryUserDetailsManager(user, admin); }}

ApiGateway应用程序,java

@SpringBootApplication public class ApiGatewayApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayApplication.class, args); } }

应用程序.属性

spring.application.name=ApiGateway server.port=8083 spring.main.allow-bean-definition-overriding=true logging.level.org.springframework.security=DEBUG spring.cloud.gateway.routes[0].id=QuestionService spring.cloud.gateway.routes[0].uri=lb://QuestionService spring.cloud.gateway.routes[0].predicates[0]=Path=/question/** spring.cloud.gateway.routes[1].id=QuizService spring.cloud.gateway.routes[1].uri=lb://QuizService spring.cloud.gateway.routes[1].predicates[0]=Path=/quiz/**, /quiz-test/**

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 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>3.3.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gateway</groupId> <artifactId>ApiGateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ApiGateway</name> <description>This is a API Gateway Server</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>23</java.version> <spring-cloud.version>2023.0.3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

API网关日志

2024-10-10T15:00:11.167-04:00 INFO 21580 --- [ApiGateway] [ main] ctiveUserDetailsServiceAutoConfiguration : Using generated security password: da26512d-fcb0-49c0-a8e3-5814686ed7a5

enter image description here

java spring spring-boot spring-mvc spring-security
1个回答
0
投票
userDetailsService

bean 的返回类型错误。应该是这样的:

    @Bean
    public UserDetailsService userDetailsService() {
    //...
        return new InMemoryUserDetailsManager(user, admin);
    }

	
© www.soinside.com 2019 - 2024. All rights reserved.