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. 伪造ip头

    x-forwarded-for: 127.0.0.1x-remote-IP: 127.0.0.1x-remote-ip: 127.0.0.1x-client-ip: 127.0.0.1x-client ...

  2. PAT甲级——A1116 Come on! Let's C

    "Let's C" is a popular and fun programming contest hosted by the College of Computer Scien ...

  3. vue中的data用return返回

    为什么在大型项目中data需要使用return返回数据呢? 答:不使用return包裹的数据会在项目的全局可见,会造成变量污染:使用return包裹后数据中变量只在当前组件中生效,不会影响其他组件. ...

  4. 微信小程序(mpvue框架) 购物车

    效果图: 说明:全选/全不选, 1.数据: products:[{checked:true,code:"4",echecked:false,hasPromotions:true,i ...

  5. Java后端WebSocket的Tomcat实现(转)

    文章摘要随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端 ...

  6. LINUX交换分区

    交换分区最大容量为64G,最多只能建32个,          创建交换分区 #fdisk /dev/hdaànà+容量àpàt(修改系统ID)à分区号à82àpàw #mkswap /dev/hda ...

  7. Apache服务器中运行CGI程序的方法,文中以Perl脚本作为示例

    关于apache与CGI在这里就不解释了. 1.apache下面以2.0.63为例介绍运行CGI程序的配置.(http://www.nklsyy.com) 2.下载Windows下的Perl解释器Ac ...

  8. 两个datagrid的数据移动(支持多选)

    1.需求 :点击卸车和撤销按钮可以实现 1和2 之间数据的移动(支持多选) 2. 代码 (这里只写一个撤销的功能) //撤销按钮 function moveOut() { var item = $(' ...

  9. String相加解析

    Java代码 package com.utils.test; public class TestObject { public static void main(String[] args) { St ...

  10. where方法的用法是ThinkPHP查询语言的精髓

    where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询.表达式查询.快捷查询.区间查询.组合查询在内的查询操作.where方法 ...