006springCloud注册中心之Eureka开启认证模式

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值