我一直在尝试一起运行我的 Eureka 服务器和云配置,我使用的是 Spring Boot 3.1.3 和 Spring Cloud 版本 2022.0.5。由于很多东西都被弃用了,我更新了我的代码并得到了未经授权的错误:
引起: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : "{"timestamp":"2024-04-01T11:58:53.982+00:00","status":401,"error": "未经授权","路径":"/eureka-server/default"}"
我的云配置运行完美,并尝试了过去 5 天的所有操作。 我尝试过的事情:
需要帮助。
配置服务器/SpringConfig.java
@Configuration
public class SecurityConfig{
@Value("${spring.cloud.config.server.git.uri}")
String cloudLocation;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
System.out.println(cloudLocation + "-------------------------------" );
return http.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.anyRequest().permitAll())
.httpBasic(Customizer.withDefaults())
.build();
}
}
配置服务器/application.yml
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: D:\Learning\config-repo
cloneOnStart: true
security:
user:
name: root
password: root
activate:
on-profile: native
# WARNING: Exposing all management endpoints over http should only be used during development, must be locked down in production!
management:
endpoint:
health:
show-details: "ALWAYS"
endpoints:
web:
exposure:
include: "*"
security:
enabled: false
logging:
level:
root: info
Eureka-服务器/SecurityConfig.java
@Configuration
public class SecurityConfig {
private final String username;
private final String password;
@Autowired
public SecurityConfig(
@Value("${spring.security.user.name}") String username,
@Value("${spring.security.user.password}") String password
) {
this.username = username;
this.password = password;
}
@Bean
public InMemoryUserDetailsManager userDetailsService() {
System.out.println("UseName---------" + username + "Password-----------" + password);
UserDetails user = User.withDefaultPasswordEncoder()
.username(username)
.password(password)
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
return http
.csrf(cs -> cs.disable())
.authorizeRequests(auth -> auth.anyRequest().permitAll())
.httpBasic(Customizer.withDefaults())
.build();
}
}
尤里卡服务器/application.yml
spring.config.import: "configserver:http://localhost:8888"
spring:
application.name: eureka-server
security:
user:
name: root
password: root
cloud.config:
failFast: true
retry:
initialInterval: 3
multiplier: 1.3
maxInterval: 10
maxAttempts: 20
uri: http://localhost:8888
username: ${CONFIG_SERVER_USR}
password: ${CONFIG_SERVER_PWD}
management:
security:
enabled: false
经过多次尝试,我通过修改 Config-server/SecurityConfig.java 文件解决了这个问题
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
System.out.println(cloudLocation + "-------------------------------" );
return http.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.anyRequest().permitAll())
.httpBasic(ht -> ht.disable())
.build();
}