【SpringCloud】Eureka 安全认证

【SpringCloud】Eureka 安全认证,第1张

Eureka 安全认证 1、添加依赖

注册中心添加依赖 security 依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-securityartifactId>
dependency>
2、配置文件

注册中心配置安全认证

spring:
  # 安全认证
  security:
    user:
      name: root
      password: 123456
3、修改访问集群节点的 url

注册中心的配置文件

server:
  port: 8761 # 端口

spring:
  application:
    name: eureka-server # 注册名称
  # 安全认证
  security:
    user:
      name: root
      password: 123456

# 配置 Eureka Server 注册中心
eureka:
  instance:
    hostname: eureka01 # 主机名,不配置的时候将根据 *** 作系统的主机名来获取
    preferIpAddress: true # 是否使用 ip 地址注册
    instanceId: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    # 设置服务注册中心地址,指向另一个注册中心
    serviceUrl: # 注册中心对外暴露的注册地址
      defaultZone: http://root:123456@localhost:8762/eureka/

服务提供者的配置文件

server:
  port: 9090 # 端口

spring:
  application:
    name: service-consumer # 注册名称

# 配置 Eureka Server 注册中心
eureka:
  client:
    register-with-eureka: false # 是否将自己注册到注册中心,默认为 true
    registry-fetch-interval-seconds: 10 # 表示 Eureka Client 间隔多久去服务器拉取信息,默认为 30 秒
    service-url: # 设置服务注册中心地址
      defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8762/eureka/

服务消费者的配置文件

server:
  port: 7070 # 端口

spring:
  application:
    name: service-provider # 注册名称

# 配置 Eureka Server 注册中心
eureka:
  instance:
    prefer-ip-address: true # 是否使用 ip 地址注册
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    service-url: # 设置服务注册中心地址
      defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8762/eureka/

management:
  endpoints:
    web:
      exposure:
        include: shutdown # 开启 shutdown 访问
  endpoint:
    shutdown:
      enabled: true # 开启 shutdown 实现停服
4、过滤 CSRF

Eureka 会自动化配置 CSRF 防御机制,Spring Security 认为 POST,PUT,and DELETE http methods 都是有风险的,如果这些 method 发送过程中没有带上 CSRF token 的话,会被直接拦截并返回 403 forbidden。

官方给了解决办法,可以参考 spring cloud issue 2754,里面有大量的讨论,这里提供两种解决方法。

首先注册中心配置一个 @EnableWebSecurity 配置类,继承 WebSecurityConfigurerAdapter,然后重写 configure 方法。

方案一

让 CSRF 忽略 /eureka/** 的所有请求

package org.fengluo.config;

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;

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http); // 这句是为了访问 eureka 控制台和 /actuator 时能做安全控制
        http.csrf().ignoringAntMatchers("/eureka/**"); // 忽然 /eureka/** 的所有请求
    }
}
方案二

密码验证依然开启,仅仅关闭 CSRF 的防御机制

package org.fengluo.config;

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;

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 注意:这里如果直接 disable 的话,会把密码验证也关闭了
        http.csrf().disable().authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();
    }
}
5、访问

使用配置好的用户名和密码登录以后可以看到注册中心界面,启动服务提供者和服务消费者,功能正常使用!至此 Eureka 注册中心的所有知识点就学习结束了!

笔者后面会继续学习 Ribbon 负载均衡器,欢迎大家关注!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/795139.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存