引用:https://blog.csdn.net/qq_33802316/article/details/80677644
由于spring-cloud使用了高版本,1.5.9以上及2.0.0及以上版本
eurka配置文件改:

server:
  port: 1111

spring:
  application:
    name: eureka-server

  security:
    user:
      name: admin
      password: 123456

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    #是否向注册中心注册注册自己
    register-with-eureka: false
    #是否开启检索服务
    fetch-registry: false
    #注册服务地址
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:1111/eureka/

客户端改为:

server:
  port: 7777

spring:
  application:
    name: config-server

  profiles:
    active: native

  security:
    user:
      name: admin
      password: 123456

  cloud:
    config:
      server:
        native:
          searchLocations: classpath:/config

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:1111/eureka/

但是客户端一启动就报异常:

2018-06-29 17:43:48.074 [DiscoveryClient-HeartbeatExecutor-0] ERROR com.netflix.discovery.DiscoveryClient - DiscoveryClient_CONFIG-SERVER/Kison-PC:config-server:7777 - was unable to send heartbeat!
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)
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89)
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92)
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77)
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89)
    at com.netflix.discovery.DiscoveryClient.renew(DiscoveryClient.java:846)
    at com.netflix.discovery.DiscoveryClient$HeartbeatThread.run(DiscoveryClient.java:1399)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

解决方案,在eurka服务中添加一个安全认证类

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 高版本的丢弃了 
     * 
     * security: 
     *   basic: 
     *    enabled: true 
     * 
     * 配置,应该使用以下方式开启
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Configure HttpSecurity as needed (e.g. enable http basic).
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
        // 如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

至此完美解决问题。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐