Eureka服务端
1. 引入security依赖
<!--开启eureka的认证--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2. 配置项
#从的值 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:18081/eureka/ #Eureka认证,老版本没有spring spring.security.basic.enabled=true spring.security.user.name=allen spring.security.user.password=allen eureka.instance.prefer-ip-address=true
3. springboot2.x 整合spring-security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false
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; @EnableWebSecurity @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //关闭csrf http.csrf().disable(); //开启认证 http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); } }
Eureka客户端
1. 配置项
注册地址:http://用户:密码@Eureak服务IP或主机名:port/eureka
server.port=8081 #设置服务提供者名字 spring.application.name=eureka-client-api-server eureka.instance.hostname=localhost #必须和eureka服务端的值一样,集群,都写上,用逗号隔开 eureka.client.service-url.defaultZone=http://allen:allen@${eureka.instance.hostname}:18080/eureka/ ,http://allen:allen@${eureka.instance.hostname}:18081/eureka/ eureka.instance.prefer-ip-address=true