Spring Cloud(五) 为 eureka 注册中心添加 security 安全访问控制

一、Spring Security是什么

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架(简单说是对访问权限进行控制嘛)。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
 

二、eureka注册中心添加访问权限操作

Spring Security 包含了很细致的安全控制配置,本案例并没有详细介绍Security的用法

只是很简单的为 eureka 添加访问权限,如果想了解有关Spring Security,请从网上查找资料

springCloud的eureka注册中心增加访问权限( Springboot2.0版本解决方案)

  • SpringCloud组件它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路有(Zuul),客户端负载均衡(Ribbon),Archaius,Turbine等
  • Eureka作用相当于zookeeper,用于微服务项目中的服务注册及发现,在采用springBoot+springCloud开发微服务时,通过一些简单的配置就能够达到基本的目的
     

1、eureka注册中心添加依赖

 <!-- 添加注册中心权限依赖  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2、eureka注册中心添加配置信息

配置步骤如下:

  1. spring--》application 中添加 security 配置:配置访问用户名和密码
  2. eureka --》client--》service-url--》defaultZone 的地址前面添加用户名和密码,格式为 username : password@(例如:http://username:password@localhost:8000/eureka/
spring:
  application:
    #这个spring应用的名字(之后调用会用到)
    name: homepage-eureka
  #1、添加安全访问配置,设置访问用户名和密码
  security:
    user:
      name: admin
      password: admin

server:
  #服务注册中心端口号
  port: 8000

eureka:
  instance:
    #服务注册中心实例的主机名
    hostname: localhost
  client:
    # 表示是否从 eureka server 中获取注册信息(检索服务),默认是true
    fetch-registry: false
    # 表示是否将自己注册到 eureka server(向服务注册中心注册自己),默认是true
    register-with-eureka: false
    service-url:
      #服务注册中心的配置内容,指定服务注册中心的位置,eureka 服务器的地址(注意:地址最后面的 /eureka/ 这个是固定值)
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #2、在原先的基础上添加security用户名和密码(例如:http://username:password@localhost:8000/eureka/)
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

 

3、eureka注册中心security关闭csrf检验

Spring Cloud 2.0 以上的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false

因为如果不将csrf检验关闭,会出现其他服务无法注册进 eureka注册中心的情况(我就遇到了这个问题)

步骤:

  • 添加一个继承 WebSecurityConfigurerAdapter 的类
  • 在类上添加 @EnableWebSecurity 注解;
  • 覆盖父类的 configure(HttpSecurity http) 方法,关闭掉 csrf

package com.imooc.homepage;

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开启验证后无法连接注册中心?
 * spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

4、访问结果

用原先的地址访问发现我们需要先登录才能查看 eureka  server 服务注册中心

登录信息就是我们先前配置的

 

 

三、eureka client 向注册中心注册时需修改配置

 其他客户端向注册中心注册时,需要在注册地址中添加访问用户名和密码才能将其注册进去

在 eureka --》client--》service-url--》defaultZone 的地址前面添加用户名和密码,格式为 username : password@

修改前:http://localhost:8000/eureka/

修改后:http://admin:admin@localhost:8000/eureka/

spring:
  application:
    name: homepage-eureka-client

server:
  port: 8101

eureka:
  client:
    service-url:
      #将自己注册进下面这个地址的服务注册中心
      defaultZone: http://admin:admin@localhost:8000/eureka/

四、遇到的问题

1、eureka开启验证后服务无法连接注册中心解决方案

运行错误提示:

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

解决步骤:

(1)客户端向服务注册中心注册时是否有添加账号密码

服务注册中心注册时加上账号密码
eureka.client.serviceUrl.defaultZone=http://admin:123456@localhost:8761/eureka/ 

(2)是否有关闭 security 的 csrf 检验

Spring Cloud 2.0 以上的security默认启用了csrf检验,要在 eurekaServer 端配置 security 的 csrf 检验为false

(可参阅本文中的:二、3.eureka注册中心security关闭csrf检验)

  1. 添加一个继承 WebSecurityConfigurerAdapter 的类
  2. 在类上添加 @EnableWebSecurity 注解;
  3. 覆盖父类的 configure(HttpSecurity http) 方法,关闭掉 csrf
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值