Eureka server 导入 security 包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Eureka server 设置账号密码
spring:
security: # security 账号密码
user:
name: ludb
password: ludb123
设置连接Eureka server 端 的地址的账号密码
server:
port: 8761 #端口号
eureka:
client:
# 设置服务注册中心地址,指向另一个注册中心,以实现高可用
serviceUrl:
defaultZone: http://ludb:ludb123@localhost:8762/eureka/
server:
port: 8762 #端口号
eureka:
client:
# 设置服务注册中心地址,指向另一个注册中心,以实现高可用
serviceUrl:
defaultZone: http://ludb:ludb123@localhost:8761/eureka/
设置连接Eureka client 端 的地址的账号密码
server:
port: 8081
eureka:
client:
# 设置服务注册中心地址【向注册中心集群注册(注册两台是为了防止第一台宕机还可以挂在第2台上)】
service-url:
defaultZone: http://ludb:ludb123@localhost:8761/eureka/,http://ludb:ludb123@localhost:8762/eureka/
修改过虑CSRF
加入了安全认证模块后,默认会开启 CSRF 跨站脚本攻击
新版(Spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false。(两种方法用一种就可以)
package com.example.eureakserver1.config;
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;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);//访问eureka控制台和actuator控制台时能做安全控制
//方法1:关闭csrf
http.csrf().disable();
//方法2:忽略/eureka/** 所有请求
http.csrf().ignoringAntMatchers("/eureka/**");
}
}