当我们不配置Eureka账号密码是,只要输入地址即可访问,下面记录如何对Eureka的服务中心进行加密
加密方法比较简单,首先是引入jar
pom.xml文件增加如下配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.yml配置如下:
spring:
application:
name: eureka
security:
user:
name: admin
password: pass
eureka:
client:
service-url:
defaultZone: http://admin:pass@localhost:8761/eureka/,http://admin:pass@localhost:8762/eureka/
这里dafaultZone的地址中将用户名密码拼入,否则将无法注册,同样,向这个Eureka注册的微服务也需要将用户名密码拼入,到这一步加密成功,访问http:localhost:8761就会显示输入用户名密码,但是输入用户名密码后,还是不能访问到Eureka服务中心,这是因为被拦截了,需要增加如下配置,将这个拦截的地址忽略掉
WebSecurityConfig.java
package com.example.eurekadiscovery;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// /eureka路径忽略csrf
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
加入该文件后再启动,访问localhost:8761,输入用户名密码后即可正常访问。