1.Eureka 的pom文件添加安全依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.配置文件配置security 賬號、密碼
spring:
application:
name: eureka # 應用名稱
security:
user:
name: epf
password: epf2020
3.啓動類上關閉默認的csrf驗證
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication extends WebSecurityConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// Configure HttpSecurity as needed (e.g. enable http basic).
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
//注意:爲了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,
// 如果是form方式,不能使用url格式登錄
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
4.其他服務需要配置相應的賬號、密碼
eureka:
client:
serviceUrl:
defaultZone: http://epf:[email protected]:8761/eureka/