Spring Boot整合Eureka与Cloud Config时遇401未授权错误求助
Eureka Server与Cloud Config整合时的401未授权问题
使用Spring Boot 3.1.3和Spring Cloud 2022.0.5版本整合Eureka Server与Cloud Config,更新代码后出现401未授权错误,错误信息如下:
Caused by: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : "{"timestamp":"2024-04-01T11:58:53.982+00:00","status":401,"error":"Unauthorized","path":"/eureka-server/default"}"
已尝试以下操作但均无效:
- 添加
org.springframework.cloud:spring-cloud-starter-bootstrap依赖 - 修改Config Server的application.yml配置
- 移除Config Server依赖,本地运行Eureka Server排查安全问题
相关代码及配置如下:
Config-server/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(); } }
Config-server/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-server/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(); } }
Eureka-server/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
配置仓库地址:https://github.com/kakarot987/config-repo

内容的提问来源于stack exchange,提问作者MAYANK DEVRANI
相关产品推荐
相关产品推荐

