spring cloud实战(五)UAA
我们以spring cloud security OAuth2.0+JWT为案例分析如何搭建自己的用户认证中心(UAA: User Account and Authentication)。spring cloud security的安全体系庞大而复杂。先看看关键的依赖包的关系:
以上是搭建一个UAA所需要最核心的jar包,我等凡人实在无法在有限的时间内阅读完所有spring security的源代码。只能通过关键的过程看看最核心的功能。
基本配置
基于以上我们看到的依赖链,我们只需要引入很少的jar包就可以搭建起来UAA。
pom.xml配置:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-jwt</artifactId><!-- spring-security-jwt不在parent版本管理中,需要配置版本号 --><version>1.0.10.RELEASE</version>
</dependency>
application.yml配置
security:oauth2:authorization: token-key-access: permitAll()jwt: key-store: classpath:jwt-rsa.jkskey-store-password: passwdkey-alias: mytest
配置过程分析
spring cloud security oauth2的自动配置在spring boot2.0已经移动到spring-security-oauth2-autoconfigure.jar中。该工程导入了OAuth2AutoConfiguration自动配置。
OAuth2AutoConfiguration的引入链为:
OAuth2AutoConfiguration=> OAuth2ClientProperties=> OAuth2AuthorizationServerConfiguration=> AuthorizationServerTokenServicesConfiguration=> AuthorizationServerProperties=> OAuth2ResourceServerConfiguration=> ResourceServerTokenServicesConfiguration=> OAuth2MethodSecurityConfiguration=> OAuth2RestOperationsConfiguration
我们自建UAA需要通过@EnableAuthorizationServer引入认证中心的相关配置。
@EnableAuthorizationServer的引入链为
@EnableAuthorizationServer=> AuthorizationServerEndpointsConfiguration=> TokenKeyEndpointRegistrar=> AuthorizationServerSecurityConfiguration=> ClientDetailsServiceConfiguration=> AuthorizationServerEndpointsConfiguration
我们的认证中心自身也需要得到安全管理,spring-boot-autoconfigure.jar通过WebSecurityEnablerConfiguration导入了@EnableWebSecurity。但我们通常需要重载这个注解。
@EnableWebSecurity的引入链为:
@EnableWebSecurity=> WebSecurityConfiguration=> SpringWebMvcImportSelector=> WebMvcSecurityConfiguration=> OAuth2ImportSelector
OAuth2AutoConfiguration为我们自动引入了AuthorizationServerTokenServicesConfiguration和ResourceServerTokenServicesConfiguration。这两个java config根据配置文件初始化不同的TokenService。由于我们配置了security.oauth2.authorization.jwt,所以初始化了JwkTokenStore。
security.oauth2.authorization.jwt.key-store配置的是在classpath下的一个RSA密钥文件。
我们需要通过keytool生成keystore:
keytool -genkeypair -alias mytest -keyalg RSA -keypass passwd -keystore jwt-rsa.jks -storepass passwd
ResourceServerTokenServicesConfiguration的配置在不同环境下有不同的初始化方案。
我们实际开发过程中只需要根据需要配置security.oauth2.resource.jwt参数即可。
测试OAuth2.0接口:
客户端模式
curl first-client:se@localhost:8100/oauth/token -dgrant_type=client_credentials -dscope=any账号密码模式
curl first-client:se@localhost:8100/oauth/token -dgrant_type=password -dscope=any -d username=test -d password=test通过refresh_token刷新access_token
curl first-client:se@localhost:8100/oauth/token -d grant_type=refresh_token -dscope=any -d refresh_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9。。。获取JWK
curl first-client:se@localhost:8100/oauth/token_key请求资源
curl -H "Authorization: $TOKEN" http://localhost:8101/whoami
参考
Using JWT with Spring Security OAuth: https://www.baeldung.com/spring-security-oauth-jwt
spring cloud实战(五)UAA相关推荐
- 我的新书《Spring Cloud实战》预告
从去年6月开始编写<Spring Cloud构建微服务架构>系列博文开始,受到了不少同行的关注与支持.随后也开通了多个交流群.创建了相关的论坛(http://bbs.springcloud ...
- Spring Cloud实战小贴士:Zuul处理Cookie和重定向
由于我们在之前所有的入门教程中,对于HTTP请求都采用了简单的接口实现.而实际使用过程中,我们的HTTP请求要复杂的多,比如当我们将Spring Cloud Zuul作为API网关接入网站类应用时,往 ...
- Spring Cloud实战小贴士:Zuul统一异常处理(一)
在上一篇<Spring Cloud源码分析(四)Zuul:核心过滤器>一文中,我们详细介绍了Spring Cloud Zuul中自己实现的一些核心过滤器,以及这些过滤器在请求生命周期中的不 ...
- Spring Cloud实战小贴士:Zuul统一异常处理(二)
在前几天发布的<Spring Cloud实战小贴士:Zuul统一异常处理(一)>一文中,我们详细说明了当Zuul的过滤器中抛出异常时会发生客户端没有返回任何内容的问题以及针对这个问题的两种 ...
- Spring Cloud实战小贴士:Zuul统一异常处理(三)【Dalston版】
本篇作为<Spring Cloud微服务实战>一书关于Spring Cloud Zuul网关在Dalston版本对异常处理的补充.没有看过本书的读书也不要紧,可以先阅读我之前的两篇博文:& ...
- Spring Cloud实战Zuul统一异常处理
Spring Cloud实战Zuul统一异常处理 Spring Cloud Zuul中自己实现的一些核心过滤器,以及这些过滤器在请求生命周期中的不同作用.我们会发现在这些核心过滤器中并没有实现erro ...
- spring cloud 实战项目搭建
spring cloud 实战项目搭建 Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断 ...
- Spring Cloud实战(三)-监控中心
接着上一篇 Spring Cloud实战(二)-注册中心 现在开始搭建监控中心 一.监控中心monitor-server 1.添加spring-boot-admin版本控制到cloud-action的 ...
- 《重新定义Spring Cloud实战》
摘要: 今天是2018年9月24号中秋节,祝福大家中秋节快乐,本文主要介绍<重新定义Spring Cloud实战>. 1. 前言 随着互联网的快速普及,云计算近年来得到蓬勃发展,企业的IT ...
最新文章
- A and B and Lecture Rooms CodeForces - 519E LCA+dfs序
- 性能超过人类炼丹师,AutoGluon 低调开源
- 从书上截取一段TCP三次握手和四次挥手
- 29.TCP/IP 详解卷1 --- 网络文件系统
- Thinkphp 实现动态include
- bert做文本摘要_BERT文本摘要
- 关于java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)的问题解决办法。(相对路径出错)
- word中常用技巧,快速删除word中光标前或后的内容
- Pycharm下载与安装教程
- 剑桥2021计算机专业,2021剑桥大学计算机专业录取条件出炉 你有信心过线吗
- 2022-12-3 图形化界面工具 添加数据
- 有趣的灵魂用有趣的屏保SadMac.saver
- RStudio(R语言) 输出内容到文件
- 无信息变量选择(UVE)波长筛选算法--基于OpenSA开源库实现
- 第314场Leetcode周赛|使用机器人打印字典序最小的字符串
- 计算机考研人工智能方向的学校,北航软件学院首招人工智能方向研究生
- 数据安全入门产品——数据库审计系统详解
- three.js的着色器(巨详细 初学者 大白话)
- 第1175期机器学习日报(2017-12-06)
- Linux netcat/nc 网络利器!