Eureka Server默认谁都可以访问,如果是部署在生产环境就太不安全了。我们使用Spring Security来实现登录验证。
Spring Cloud版本:Finchley.SR1
Spring Boot版本:2.0.4.RELEASE
一、在pom.xml中加入Spring Security支持
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
二、在配置文件中配置用户名密码
spring.security.user.name=fate spring.security.user.password=123456
启动项目后,再进入Eureka Server,会跳转到一个很丑的登录页,输入设置的用户名密码后,就跳回Eureka Server的页面了
三、配置WebSecurityConfigurerAdapter
只做以上操作,你会发现,Eureka Client在注册的时候也会跳转,这就不对了,我们需要把服务注册的url忽略掉(根据配置文件,我配的eureka.client.serviceUrl.defaultZone=http://192.168.1.247/eureka/,所以下面是/eureka/**)
@Override public void configure(WebSecurity web) throws Exception { //配置需要忽略验证的地址 web.ignoring().antMatchers("/eureka/**"); }