Spring Cloud Security&Eureka安全认证(Greenwich版本)


一·安全

Spring Cloud支持多种安全认证方式,比如OAuth等。而默认是可以直接添加spring-boot-starter-security来配置HTTP BASIC认证。如果没有配置用户和密码,那么默认的用户是user,并随机生成一个密码,在启动的控制台中显示出来。但是这种方式在实践中几乎无实际用途,所以最好还是需要显式设置(参数名为spring.security.user.password)

二· 密码加密

直接配置明文敏感信息是比较冒险的,所以一种可行的办法就是将明文加密成密文。密文是以{cipher}开头,系统会自动在使用之前将其解密。如果配置的属性(keyname)对应密文无法解密,那么系统将会将此属性移除,并增加一个属性invalid${keyname}: not applicable

数据的加密解密可以通过接口/encrypt /decrypt完成,比如:

$ curl localhost:8888/encrypt -d mysecret
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda $ curl localhost:8888/decrypt -d 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda

mysecret

如果安装了扩展Spring Cloud CLI extensions,那么可以直接Spring命令工具:

$ spring encrypt mysecret --key foo
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
$ spring decrypt --key foo 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
mysecret

也可以使用密钥文件加解密,文件路径参数前需要个特殊符号@,如下:

$ spring encrypt mysecret --key @${HOME}/.ssh/id_rsa.pub
AQAjPgt3eFZQXwt8tsHAVv/QHiY5sI2dRcR+...

注意:要使用加密解密的功能,必须要下载JCE(Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files)。这些默认是不包含在JDK中的。

三.Eureka Server配置HTTP BASIC

在配置文件中增加spring-boot-starter-security,如下:

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

注意,在最新版本Greenwich中,口令不是配置security.user.namesecurity.user.password属性中,它已被废弃不推荐使用,而是配置在Spring中(spring.security.user.namespring.security.user.password)。以下的是Eureka多集群的(Greenwich)的配置文件:

USER: light
PASSWORD: 123456 spring:

security:

user:

name: ${USER}

password: ${PASSWORD} eureka:

client:

service-url:

defaultZone: http://${USER}:${PASSWORD}@peer1.com:9801/eureka/,http://${USER}:${PASSWORD}@peer2.com:9802/eureka/,http://${USER}:${PASSWORD}@peer3.com:9803/eureka/
spring:

profiles: peer1

application:

name: application-peer1 server:

port: 9801 eureka:

environment: dev

datacenter: hangzhou

instance:

hostname: peer1.com

appname: lighthouse
spring:

profiles: peer2

application:

name: application-peer2 server:

port: 9802 eureka:

environment: dev

datacenter: beijing

instance:

hostname: peer2.com

appname: lighthouse
spring:

profiles: peer3

application:

name: application-peer3 server:

port: 9803 eureka:

environment: dev

datacenter: guangzhou

instance:

hostname: peer3.com

appname: lighthouse

由于默认是开启CSRF,所以需要将其关闭,不然会出现如下错误:

javax.ws.rs.WebApplicationException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'timestamp' does not match expected ('instance') for type [simple type, class com.netflix.appinfo.InstanceInfo]

创建一个WebSecurityConfig类,代码如下:

// WebSecurityConfig.java

@EnableWebSecurity

@Configuration

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.csrf().disable(); //关闭csrf

super.configure(http); //开启认证

}

}

启动三个配置文件对应的三个实例,就可以看到已经启用HTTP BASIC认证:



关于Eureka Server集群的配置,请参考另外一篇文章

Spring Cloud Eureka集群配置及注意事项(Greenwich版本)

Eureka Client和Service Provider配置方法类似,只需要在defaultZone中配置好含有认证信息的url即可,如下所示。


USER: light
PASSWORD: 123456 eureka:

client:

service-url:

defaultZone: http://${USER}:${PASSWORD}@peer1.com:9801/eureka/,http://${USER}:${PASSWORD}@peer2.com:9802/eureka/,http://${USER}:${PASSWORD}@peer3.com:9803/eureka/

四·参考

原文地址:http://www.easysb.cn/2019/06/429.html

Spring Cloud Security&Eureka安全认证(Greenwich版本)的更多相关文章

  1. Spring Cloud Security OAuth2.0 认证授权系列(一) 基础概念

    世界上最快的捷径,就是脚踏实地,本文已收录[架构技术专栏]关注这个喜欢分享的地方. 前序 最近想搞下基于Spring Cloud的认证授权平台,总体想法是可以对服务间授权,想做一个基于Agent 的无 ...

  2. Spring Cloud下基于OAUTH2认证授权的实现

    GitHub(spring -boot 2.0.0):https://github.com/bigben0123/uaa-zuul 示例(spring -boot 2.0.0): https://gi ...

  3. spring cloud之eureka简介

    最近线上的接口出了一些问题,有一些可能不是代码的问题,但是由于是测试和其他方面的同事爆出来的,所以感觉对接口的监控应该提上日程. 经过搜索发现,spring cloud的eureka就是专门做这方面工 ...

  4. 使用Spring Cloud Security OAuth2搭建授权服务

    阅读数:84139 前言: 本文意在抛砖引玉,帮大家将基本的环境搭起来,具体实战方案还要根据自己的业务需求进行制定.我们最终没有使用Spring Security OAuth2来搭建授权服务,而是完全 ...

  5. Spring Cloud与Eureka

    Spring Cloud与Eureka 一.使用SpringCloud注册中心Eureka 1.1 Eureka和Zookeeper对比 1.1.1 Zookeeper保证CP 1.1.2 Eurek ...

  6. Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  7. Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix

    Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...

  8. Spring Cloud和eureka启动报错 解决版本依赖关系

    导读 An attempt was made to call a method that does not exist. The attempt was made from the following ...

  9. 学习SPRING BOOT, SPRING CLOUD之Eureka和security

    有意思,明天去杨浦报名了一个SPRING CLOUD沙龙, 今天再抓紧看看哈哈哈. Eureka服务端: EurekaApplication.java package com.packtpub.Eur ...

随机推荐

  1. HTTP协议请求篇

    http协议的基本概念 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.是工作在tcp/ip协议基础上的,所有的WWW文件都必须 ...

  2. tomcat部署war和war exploded区别和intellij idea部署项目的位置

    tomcat部署war和war exploded区别和intellij idea部署项目的位置 来自https://blog.csdn.net/u013041642/article/details/7 ...

  3. 11_springmvc之RESTful支持

    一.理解RESTful RESTful架构,就是一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. RESTful(即Representational Sta ...

  4. Python Fabric模块详解

    Python Fabric模块详解 什么是Fabric? 简单介绍一下: ​ Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...

  5. python3 使用aria2下载的一个脚本

    import requests import time ariaurl="http://localhost:6800/jsonrpc" dlurl="http://xxx ...

  6. Android开发 获取View的尺寸的2个方法

    前言 总所周知,在activity启动的onCreate或者其他生命周期里去获取View的尺寸是错误的,因为很有可能View并没有初始化测量绘制完成.你这个时候获取的宽或的高不出意外就是0.所以,我们 ...

  7. Axios的get和post请求写法

    执行get请求 // 为给定 ID 的 user 创建请求 axios.get('/user?ID=12345') .then(function (response) { console.log(re ...

  8. postgresql数据库安装后的pgadmin4中无法加载本地连接解决办法

    postgresql 在安装最后一步提示the database cluster initialisation failed, 而后点开pgadmin4发现如下图所示 经过百度搜索找出问题原因, 由于 ...

  9. 【JZOJ2679】跨时代

    description 钟逆时针而绕,恶物狰狞的倾巢,我谦卑安静地于城堡下的晚祷,压抑远古流窜的蛮荒暗号,而管风琴键高傲的说,那只是在徒劳.我的乐器在环绕,时代无法淘汰我霸气的皇朝. 你无法预言,因为 ...

  10. 自己编写jquery插件

    http://blog.csdn.net/chenxi1025/article/details/52222327 https://www.cnblogs.com/ajianbeyourself/p/5 ...