注册中心主要采用spring cloud中的eureka模块。eureka默认是没有添加用户拦截的,这边我们需要对注册中心添加用户拦截。
## 注册中心添加用户密码验证
1. pom引入Seurity jar 包
~~~
<!--security-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
~~~
2. 添加配置项
yml 配置注册中心的用户名和密码
~~~
spring:
# 注册中心登录信息
security:
user:
name: spark
password: spark
~~~
添加Security配置
~~~
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* @Description: 高版本的丢弃了
* security:
* basic:
* enabled: true 配置,应该使用以下方式开启
* @Param: [http]
* @Return: void
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
// Configure HttpSecurity as needed (e.g. enable http basic).
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
//注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
// 如果是form方式,不能使用url格式登录
http.csrf().disable()
.authorizeRequests()
.antMatchers("/actuator/**").permitAll()
.anyRequest()
.authenticated().and().httpBasic();
}
~~~
