这里先说一下旧版
当时项目中使用的是Springboot 1.0 +spring cloud D版,eureka的用户认证方式如下:
一、导入依赖:
<!-- 开启eureka密码验证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
二、修改.porperties文件或:.yml文件
.porperties
security.user.name=jxysgzs
security.user.password=123456
security-basic-enabled=true
.yml
security:
user:
password: 2190268123asd
name: jxysgzs
完成以上两个步骤后启动项目直接访问即可弹出输入账号密码的框
注意,其他eureka客户端需要如下配置(这里只说一下yml的吧,properties的自己去改一下就可以了)
eureka:
client:
serviceUrl:
defaultZone: http://jxysgzs:132456@localhost:8761/eureka/
这里说一下F版之后,G版也是如此配置,H版目前还没有测试过
使用的版本是springboot2.0.3 springcloud F版
一、导入依赖:
<!-- 开启eureka密码验证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
二、修改.yml文件(.porperties大家自行修改一下就行)
yml
spring:
security:
user:
password: 2190268123asd
name: jxysgzs
F版之后将配置放到了Sping下,且无效了security-basic-enabled=true
三、修改启动文件
添加一个class文件继承WebSecurityConfigurerAdapter接口,修改验证方式
在spring boot 2.0.3中,默认开启了csrf的认证,我们这里手动进行关闭,然后开启httpBasic的认证,这个和以前版本中配置文件security.basic.enabled=true的作用类似。
package cn.jxys.security;
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;
/**
* @date 2019年12月12日16:10:44
**/
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录必须是httpBasic
http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
完成以上两个步骤后启动项目直接访问即可弹出输入账号密码的框
修改之后客户端配置同上即可