欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

eureka添加security 后客户端启动报错:Cannot execute request on any known server

程序员文章站 2022-04-15 18:14:08
...
配置安全密码前可以连接成功 配置密码后无法连接

#注册中心地址用户密码地址
eureka.client.service-url.defaultZone= http://admin:[email protected]:8080/eureka/

客户端启动后日志打印


com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.7.0.jar:1.7.0]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.7.0.jar:1.7.0]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.7.0.jar:1.7.0]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.7.0.jar:1.7.0]

原因:2.1版本的security默认加上了 csrf 拦截,需要在注册中心重写 WebSecurityConfigurerAdapter中得方法禁用csrf拦截

带代码如下:


package com.sumu.eureka;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
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;
 /**
  * 注册中心
  * @author wang
  */
@SpringBootApplication //spring-boot 启动注解
@EnableEurekaServer // spring-cloud 注册中心服务注解
public class RegisterMain{
     public static void main(String [] args) {
         SpringApplication.run(RegisterMain.class, args);
     }
     /**
      * 2.1版本的security默认加上了 csrf 拦截, 所以需要通过重写方法, 把csrf拦截禁用
     */
     @EnableWebSecurity
     static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
         @Override
         protected void configure(HttpSecurity http) throws Exception {
        	 http.csrf().disable().authorizeRequests()
             .anyRequest()
             .authenticated()
             .and()
             .httpBasic();
         }
     }
}

启动客户端成功!!