【SpringCloud】Eureka 安全认证

您所在的位置:网站首页 springcloud登录认证 【SpringCloud】Eureka 安全认证

【SpringCloud】Eureka 安全认证

2023-12-27 23:02| 来源: 网络整理| 查看: 265

在这里插入图片描述

Eureka 安全认证 1、添加依赖

注册中心添加依赖 security 依赖

org.springframework.boot spring-boot-starter-security 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、访问

image-20220501134817565

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

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3