Eureka Server 开启Spring Security Basic认证

Desktop

文章共 503字,阅读大约需要 2分钟 !


概 述

Eureka Server 在实际使用过程中必须考虑安全问题,比如 未认证的用户 不允许其随意调用 Eureka Server的 API;还有一个则是 未认证的 Eureka Client 也禁止其注册到 Eureka Server中来,这些都是可以在工程中进行配置的,当然这也是最最基本的安全认证措施,本文实践之。

本文实验环境如下:

  • Spring Cloud:Finchley.RELEASE
  • Spring Boot:2.0.3.RELEASE

注: 本文首发于 My Personal Blog:CodeSheep·程序羊,欢迎光临 小站


Eureka Server搭建与配置

Eureka Server 开启 Spring Security Basic认证首先需要在 Eureka Server中引入 Spring Security组件:

1<dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-security</artifactId> 4</dependency>

接下来关键的一步则是配置 Eureka Server工程的 yml配置文件,加入和认证相关的信息:

1server: 2 port: 1111 3 4spring: 5 security: 6 user: 7 name: codesheep 8 password: www.codesheep.cn 9 10eureka: 11 client: 12 registerWithEureka: false 13 fetchRegistry: false

spring.security 配置的意图应该很明确了吧,需要用户名和密码方可认证通过。


Eureka Client搭建与配置

既然上面的 Eureka Server已开启认证环节,则相应的 Eureka Client也需要对应的配置,方可通过认证再注册到 Eureka Server中来

搭建好 Eureka Client工程后,需要在项目配置文件中加入类似 Eureka Server的配置:

1server: 2 port: 1112 3 4spring: 5 application: 6 name: eureka-client 7 8eureka: 9 client: 10 security: 11 basic: 12 user: codesheep 13 password: www.codesheep.cn 14 15 serviceUrl: 16 defaultZone: http://${eureka.client.security.basic.user}:${eureka.client.security.basic.password}@localhost:1111/eureka/

这样就完成了基于 Spring Security Basic的基础认证


实验验证

  • 启动 Eureka Server后,再也不能裸进 Eureka Server管理界面了,需要输入用户名/密码 认证

进 Eureka Server管理界面了,需要输入用户名/密码 认证

  • 没有通过认证的 Eureka Client是无法注册到 Eureka Server中心的,控制台会报无法发送心跳的错误

没有通过认证的 Eureka Client无法注册到 Eureka Server中心

没有通过认证的 Eureka Client无法注册到 Eureka Server中心

  • 而认证通过的 Eureka Client还是按照往常一样注册到 Eureka Server中心

认证通过的 Eureka Client还是按照往常一样注册到 Eureka Server中心


后 记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!



点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Eureka Server启用 https服务指北

!NewMacMini(https://uploadimages.jianshu.io/upload_images/9824247f2c7bceed604f8f7.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)文章共591字,阅读大约需要2分钟!概述

Docker容器跨主机通信之:直接路由方式

!Desktop(https://uploadimages.jianshu.io/upload_images/9824247a72fa1bbdcf60e4e.jpeg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)概述就目前Docker自身默认的网络来说

Redis 哈希结构内存模型剖析

!Profile(https://uploadimages.jianshu.io/upload_images/9824247af07824cf5595df9.png?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)本文共1231字,阅读大约需要5分钟!概述