Eureka Server 开启最基本的 Spring Security Basic 安全措施

2018-11-27 06:44:31 +08:00
 hansonwang99

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


概 述

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

本文实验环境如下:

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


Eureka Server 搭建与配置

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

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

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

server:
  port: 1111

spring:
  security:
    user:
      name: codesheep
      password: www.codesheep.cn

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

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


Eureka Client 搭建与配置

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

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

server:
  port: 1112

spring:
  application:
    name: eureka-client

eureka:
  client:
    security:
      basic:
        user: codesheep
        password: www.codesheep.cn

    serviceUrl:
      defaultZone: http://${eureka.client.security.basic.user}:${eureka.client.security.basic.password}@localhost:1111/eureka/

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


实验验证


后 记

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



1101 次点击
所在节点    Blogger
1 条回复
zhazi
2018-11-27 08:36:41 +08:00
这种东西太初级了,官方文档读两本都会做,你说说你这个照文档有什么优势

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/511782

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX