I can not register a micro service to Eureka Server with Spring Security.

I have tried almost every solution on the internet. But I haven't solved it.

Spring Boot version: 2.1.3.RELEASE
Spring Cloud: Greenwich.RELEASE

Here is my configuration with Eureka server:

spring:
  application:
    name: registration-center
  security:
    user:
      name: eureka
      password: 123456
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      default-zone: http://eureka:[email protected]${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: false

And this is Eureka Client's configuration:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      - id: route_service_user
        uri: lb://SERVICE-USER
        predicates:
        - Path=/user/**
  application:
    name: gateway-client
server:
  port: 8901
eureka:
  client:
    servers-url:
      default-zone: http://eureka:[email protected]:8761/eureka
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true
    hostname: localhost

I use Spring Security dependencies, and I can't register any Eureka client to Eureka server. Here is the exception:

2019-05-11 20:01:07.454  WARN 3864 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:829) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121) ~[eureka-client-1.9.8.jar:1.9.8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_201]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_201]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_201]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_201]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_201]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_201]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

I also disabled csrf in WebSecurityConfig class:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/actuator/**").permitAll()
                .anyRequest()
                .authenticated()
                .and().httpBasic();
    }
}

All of these didn't work at all.

But if I remove the Spring Security Dependency, it will success.

It must be something wrong in my configuration but I can't figure it out.

Could you please help me out of this problem?

Thank you very much!

0 Answers