1.Eureka 的pom文件添加安全依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.配置文件配置security 账号、密码
spring:
application:
name: eureka # 应用名称
security:
user:
name: epf
password: epf2020
3.启动类上关闭默认的csrf验证
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication extends WebSecurityConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
@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();
}
}
4.其他服务需要配置相应的账号、密码
eureka:
client:
serviceUrl:
defaultZone: http://epf:epf2020@127.0.0.1:8761/eureka/