25

Eureka Server 开启Spring Security Basic认证

 5 years ago
source link: http://www.codesheep.cn/2018/11/27/eureka-server-security-basic/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Eureka Server 最基本的安全措施

2UrIZnz.jpg!web

文章共 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组件:

<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的基础认证

实验验证

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

ZjMvEzN.png!web

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

qENnUrB.png!web

Zbmy2av.png!web

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

mEF7ju6.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK