eureka注册中心的安全问题(对eureka也要授信用户才能访问)

  1. 在eureka 模块中pom.xml中引入security
   <!-- 引入spring的security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
  1. 在eureka模块的application.yml中加入安全配置
	spring:
      security:
        user:
          name: admin
          password: a
  1. 再重启eureka,测试一下授权登录访问 http://localhost:端口号

此时 其他服务也无法在eureka上注册了,所以要在其他服务端中加入登录配置
在这里插入图片描述
解决方案; 在 其他服务端的application.yml中修改登录eureka登录配置

eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://admin:a@localhost:7001/eureka  #授信用户登录

并且要求在eureka模块中增加一个类: 关闭 csrf

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 EurekaSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http)throws Exception{
      http.csrf().disable();
      super.configure(http);
  }
}

关于什么是csrf 可以查看 :https://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html
之后
再重启eureka,再重启 服务端,可以看到 服务端在eureka中注册成功
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45942124/article/details/109080806
今日推荐