网上很多资料写的不全,不细致。
 springcloud架构,本地运行代码是eureka地址一般为localhost:port(自己暴露的端口),例如http://localhost:9000/ ,但是如果在服务器,且使用k8s部署,一般会另外暴露端口。
 且更改配置与springcloud版本有关,
 首先根目录pom文件增加包
<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
</dependency>
<!--        添加安全认证-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
其次eureka服务更改yaml文件配置,修改serviceUrl,增加
eureka:
	client:
		serviceUrl:
		     defaultZone: http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
security:
  user:
    name: xxx
    password: xxx
最后修改eureka启动类,新增以下代码
@EnableWebSecurity
    public class EurekaConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()//禁用掉 csrf 跨域攻击,以免我们的服务无法注册到 eureka
                    .authorizeRequests()//需要认证所有的请求
                    .mvcMatchers("/eureka/**").permitAll()//符合以上路径规则的放行
                    .mvcMatchers("/actuator/**").permitAll()//放行
                    .anyRequest().authenticated().and().httpBasic();//剩余的所有的请求都需要验证
        }
    }

最终效果
 



















