- 首先添加安全验证的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
- 添加配置
spring: security: user: name: root # 用户名 password: root # 密码
- 添加启动配置文件
package com.my.eureka.app; 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; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 关闭csrf http.csrf().disable(); // 支持httpBasic http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); } }
- 修改eureka注册的地址
defaultZone: ${EUREKA_SERVER:http://root:root@localhost:8761/eureka/}
备注:所有的客户端注册都需要添加账号密码,web管理页面登录也需要账号密码