为 Eureka Server添加用户认证

版本信息:
springboot:2.1.0.RELEASE
springcloud:Greenwich.RELEASE

之前搭建的Eureka Server是允许匿名访问的,为了进一步加强安全设置,今天会对的Eureka Server进行改造,使其需要登录后才能访问。

第一步:在pom文件中添加spring-boot-starter-security依赖

<dependency>
   	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

第二步:在application.properties文件中添加以下配置:

spring.security.user.name=user
spring.security.user.password=password123

第三步:重启Eureka Server服务http://localhost:1111/login
在这里插入图片描述
此处输入配置文件中的 name 和 password 即可。

第四步:将微服务注册到需要认证的Eureka Server上
只需要修改Eureka客户端配置文件eureka.client.serviceUrl.defaultZone=http://user:password123@localhost:1111/eureka/

第五步:重启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:112) ~[eureka-client-1.9.2.jar:1.9.2]
	at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.2.jar:1.9.2]
	at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.9.2.jar:1.9.2]
	at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.2.jar:1.9.2]
	at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.2.jar:1.9.2]
	at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:829) ~[eureka-client-1.9.2.jar:1.9.2]
	at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121) [eureka-client-1.9.2.jar:1.9.2]
	at com.netflix.discovery.InstanceInfoReplicator$1.run(InstanceInfoReplicator.java:101) [eureka-client-1.9.2.jar:1.9.2]
	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]

2019-08-05 16:57:21.749  WARN 18648 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator

怎么办呢?开始寻找解决办法:因为新版本的security默认开启csrf了,关掉就好了,Eureka Server新建一个配置类

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable(); // 关闭csrf
		http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 开启认证
	}
}

再次重启Eureka Server和Eureka客户端,客户端注册成功。
在这里插入图片描述

发布了22 篇原创文章 · 获赞 13 · 访问量 5777

猜你喜欢

转载自blog.csdn.net/weixin_43839457/article/details/98485917