栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud eureka 集合 spring security 实现注册服务认证

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring cloud eureka 集合 spring security 实现注册服务认证

1.前言

为了提高我们微服务注册中心的安全性,我们需要给注册中心加认证的功能,防止随意注册服务。

2.实现步骤 2.1 修改pom文件引用 spring security
        
            org.springframework.cloud
            spring-cloud-starter-security
        
2.2 修改eureka application.yml
spring:
  security:
    user:
      name: root
      password: admin
2.3 修改服务的 application.yml
eureka:
  client:
    service-url:
      defaultZone: http://root:root!!!123@localhost:7001/eureka/
      #true 表示自己是一个注册的服务,需要在注册中心搜索服务名
    fetch-registry: true
    # 需要向注册中心注册自己
    register-with-eureka: true

注意:加入了spring security 后 注册中心的地址发生了一些变化,其规则为:

 http://${spring.security.user.name}:${spring.security.user.password}@${eureka.host} :${eureka.port}/eureka
2.4 在eureka 注册中心加入配置文件

由于 spring security 默认开启了csrf 安全防护,我们需要关闭它,不然在服务注册的时候就会产生403的错误,如图:

关于csrf的介绍请看: 浅谈CSRF攻击方式 ,关闭csrf的代码如下:

package com.flow.eureka.server.config;

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;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();
        super.configure(http);
    }
}

这样我们就完成了注册中心注册认证的所有功能了, 启动我们的注册中心,我们用浏览器访问注册中心的地址就可以看到

友情提示 在我们设置注册中心密码的时候需要规避一下特殊字符,如: ‘#’, ‘@’,’%'等,这样可以减少一些不必要的麻烦

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644704.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号