Spring Cloud 学习系列:(四)为 Eureka Server 添加用户认证(1)

security:

basic:

enabled: true # 开启基于HTTP basic的认证

user:

name: user

password: test123

server:

port: 8761

eureka:

instance:

hostname: localhost

client:

register-with-eureka: false

fetch-registry: false

service-url:

default-zone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/

如果 name 和 password 不设置的话,用户名默认为 user,密码是一个随机值,该值会在启动时打印出来。

4、配置 Eureka Client 的 application.yml

server:

port: 8762

spring:

application:

name: microservice-provider-service-hi

eureka:

client:

service-url:

defaultZone: http://user:test123@localhost:8761/eureka/

注意:像这种用户名和密码一般不能直接明文写出来,需要加密下。

有兴趣的可以了解:[

SpringBoot项目中基于jasypt实现mysql配置文件自定义密码加密]( )

将 eureka.client.service-url.defaultZone 配置成 http://user:password@EUREKA_HOST:EUREKA_PORT/eureka/ 这种形式,就可以将 HTTP basic 认证加到 Eureka Client 了。

eureka:

client:

service-url:

defaultZone: http://user:test123@localhost:8761/eureka/

5、注册中心关闭Spring Security的CSRF验证

(这一步骤非常重要) 如果不关闭,那么客户端就连接不上!

package com.riemann.microserviceeurekaserver;

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;

/**

  • 如果Eureka Server设置了登录密码 就必须关闭Spring Security的CSRF验证

*/

@EnableWebSecurity

@Configuration

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.csrf().disable(); //关闭csrf

super.configure(http);

}

学习分享,共勉

这里是小编拿到的学习资源,其中包括“中高级Java开发面试高频考点题笔记300道.pdf”和“Java核心知识体系笔记.pdf”文件分享,内容丰富,囊括了JVM、锁、并发、Java反射、Spring原理、微服务、Zookeeper、数据库、数据结构等大量知识点。同时还有Java进阶学习的知识笔记脑图(内含大量学习笔记)!

资料整理不易,读者朋友可以转发分享下!

Java核心知识体系笔记.pdf

记一次蚂蚁金服Java研发岗的面试经历,分享下我的复习笔记面经

中高级Java开发面试高频考点题笔记300道.pdf

记一次蚂蚁金服Java研发岗的面试经历,分享下我的复习笔记面经

架构进阶面试专题及架构学习笔记脑图

记一次蚂蚁金服Java研发岗的面试经历,分享下我的复习笔记面经

Java架构进阶学习视频分享
65159)]

中高级Java开发面试高频考点题笔记300道.pdf

[外链图片转存中…(img-y4BwZXtZ-1718866365160)]

架构进阶面试专题及架构学习笔记脑图

[外链图片转存中…(img-E3Kkjsv3-1718866365160)]

Java架构进阶学习视频分享

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值