如何使注册中心开启密码认证?
完成:第一遍
1. 如何使注册中心开启密码认证?
步骤一:在Eureka Server的pom.xml中先添加security依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
步骤二:修改配置文件添加spring.security.user的
name:adbycool
password:123456
server:
port: 8001
spring:
application:
name: eureka
security:
user:
name: adbycool
password: 123456
eureka:
client:
register-with-eureka: false
fetch-registry: false
server-url:
defaultZone: http://localhost:8001/eureka
步骤三:在src\main\java\com\itzhiya\eureka\server下创建一个配置文件类SecurityConfig
需要继承WebSecurityConfigurerAdapter
重写configure()方法
package com.itzhiya.eureka.server;
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;
import org.springframework.stereotype.Component;
@Component
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//首先关闭 http.csrf().disable();
http.csrf().disable();
//支持httpBasic
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
保存,重启服务器
访问:localhost:8001
2.当注册中心Eureka Server开启密码认证,Eureka Client如何在配置文件中添加注册中心的账户和密码信息?
Eureka Client原来的配置文件application.yml
server:
port: 8002
spring:
application:
name: buildingservice
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka
Eureka Client添加账户和密码,即更改后的的配置文件application.yml
server:
port: 8002
spring:
application:
name: buildingservice
eureka:
client:
service-url:
defaultZone: http://adbycool:123456@localhost:8001/eureka