spring boot 学习三:OAuth2 认证
1: 代码地址: https://github.com/liufeiSAP/uaa-zuul
2: 安装: postgres
下载 https://www.openscg.com/bigsql/postgresql/installers.jsp/
具体安装过程参考: https://www.cnblogs.com/winkey4986/p/5360551.html
3:
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
@EnableOAuth2Sso //这个注解会帮我们完成跳转到授权服务器,当然要些配置application.yml
@Configuration
@EnableOAuth2Sso
public class SecurityConfig extends WebSecurityConfigurerAdapter{} ZuulFallbackProvider : // 是当我们的zuul进行路由分发时,如果后端服务没有启动,或者调用超时,这时候我们希望Zuul提供一种降级功能,而不是将异常暴露出来。
// 这个接口就是做这个的, 实现public ClientHttpResponse fallbackResponse() 就可以 ,快过期了用 refresh token 刷一下,就省去让用户再输入帐号密码登录然后授权你的这一步了。
spring:
profiles:
active: ${SPRING_PROFILES_ACTIVE:dev}
application:
name: api-gateway
# cloud:
# config:
# uri: http://${config.host:192.168.1.140}:${config.port:8888} server:
port: 8080 eureka:
client:
serviceUrl:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/ zuul:
routes: // 定义路由
uaa:
path: /uaa/**
sensitiveHeaders:
serviceId: auth-server // 服务名称,注册到注册中心的服务名
order:
path: /order/**
sensitiveHeaders:
serviceId: order-service
add-proxy-headers: true security:
oauth2:
client:
access-token-uri: http://localhost:8080/uaa/oauth/token
user-authorization-uri: http://localhost:8080/uaa/oauth/authorize
client-id: webapp
resource:
user-info-uri: http://localhost:8080/uaa/user
prefer-token-info: false
- ClientDetailsServiceConfigurer:用来配置客户端详情服务(ClientDetailsService),客户端详情信息在这里进行初始化,你能够把客户端详情信息写死在这里或者是通过数据库来存储调取详情信息。(就是把client信息保存在memory, jdbc等)
- UserDetailsService验证用户名、密码和授权: 这个是验证用户明和密码等;
- ClientDetailsService: 这个验证的是client, 比如android, webapp, tsk等。
An OAuth 2 authentication token can contain two authentications: one for the client and one for the user. Since some
* OAuth authorization grants don't require user authentication, the user authentication may be null.
*
* @author Ryan Heaton
*/
public class OAuth2Authentication extends AbstractAuthenticationToken {
网上关于OAUTH2的都是讲一本原理,大部分没讲oauth2的token到底如何校验的,这个spring 项目中用redis存储的token,
RedisTokenStore.java的源码看了一下,就知道是如何根据token得到所有相关信息了。
eids中有如下keys:
1) "auth:9c380ddb-9cfc-4035-81e3-4bb6e0280084" // principal信息(用户名);
2) "refresh_to_access:73fcc7fc-4349-4b90-bc33-7d1533c24f96"
3) "uname_to_access:android:admin"
4) "refresh_auth:73fcc7fc-4349-4b90-bc33-7d1533c24f96"
5) "auth_to_access:12d0c29da8b9ebf45ab14ec4b48a8a1e"
6) "access_to_refresh:9c380ddb-9cfc-4035-81e3-4bb6e0280084"
7) "client_id_to_access:android"
8) "refresh:73fcc7fc-4349-4b90-bc33-7d1533c24f96"
9) "access:9c380ddb-9cfc-4035-81e3-4bb6e0280084" // 这个存储的是token相关信息(何时过期, 类型 , refresh_token, scope, )
DefaultTokenServices.java 的createAccessToken()的代码展示了,在确认了用户信息后生成token的逻辑。
看源码分析出来: token就是一个UUID,不带任何信息,reids存储的时候把token做成可以,把Authentication做成value) Refresh_token: 快过期了用 refresh token 刷一下,就省去让用户再输入帐号密码登录然后授权你的这一步了。
spring boot 学习三:OAuth2 认证的更多相关文章
- spring boot 学习(三)API注解记录及测试
spring boot API注解记录及测试 部分注解解析 @Controller : 修饰创建处理 http 处理对象,一般用于页面渲染时使用. @RestController : Json数据交互 ...
- Spring Security 解析(三) —— 个性化认证 以及 RememberMe 实现
Spring Security 解析(三) -- 个性化认证 以及 RememberMe 实现 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...
- 15 个优秀开源的 Spring Boot 学习项目,一网打尽!
Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...
- 15 个优秀开源的 Spring Boot 学习项目
Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...
- Spring Boot Security 整合 OAuth2 设计安全API接口服务
简介 OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版.本文重点讲解Spring Boot项目对OAuth2进行的实现,如果你对OAut ...
- Spring Cloud下基于OAUTH2认证授权的实现
GitHub(spring -boot 2.0.0):https://github.com/bigben0123/uaa-zuul 示例(spring -boot 2.0.0): https://gi ...
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot 2 (三):Spring Boot 2 相关开源软件
Spring Boot 2 (三):Spring Boot 2 相关开源软件 一.awesome-spring-boot Spring Boot 中文索引,这是一个专门收集 Spring Boot 相 ...
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
随机推荐
- MyBatis做动态模糊查询时,like后面要不要加单引号??
做项目遇到了个奇怪的问题,项目里面要对商品.账户.进行分别的多条件查询,于是我就采用动态多条件分页查询,起初在做账户部分的时候Mybatis是这样写的 <!-- 动态多条件分页查询 --> ...
- openssl源码安装
下载最新的OpenSSL http://openssl.org/source/ ./config make make install 通过命令openssl version或者openssl ...
- 01 redis特点及安装使用
一:redis的特点 ()redis是一个开源,BSD许可高级的key-value存储系统.可以用来存储字符串,哈希结构,链表,集合,因此,常用来提供数据结构服务. 二:redis和memcached ...
- 漫反射和Lambert模型
粗糙的物体表面向各个方向等强度地反射光,这种等同地向各个方向散射的现象称为光的漫反射(diffuse reflection).产生光的漫反射现象的物体表面称为理想漫反射体,也称为朗伯(Lambert) ...
- System.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1.提示错误信息: zipSystem.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' ...
- 九度OJ 1174:查找第K小数 (排序、查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6376 解决:2539 题目描述: 查找一个数组的第K小的数,注意同样大小算一样大. 如 2 1 3 4 5 2 第三小数为3. 输入: ...
- 使用ab 进行并发压力测试
使用ab 进行并发压力测试 - 参与商 - 博客园 https://www.cnblogs.com/shenshangzz/p/8340640.html 使用ab 进行并发压力测试 ab全称为:a ...
- Azkaban_Oozie_action
http://azkaban.github.io/azkaban/docs/2.5/ There is no reason why MySQL was chosen except that it is ...
- 【网络与系统安全】20179209 利用metasploit对windows系统的渗透
这次实验的主角是素有"内网杀手"之称的metasploit.还是少说一些夸赞它的话(因为即使功能再强大,不明白它的原理,不会灵活使用它集成的功能,一样没有用),我们直入主题.简单说 ...
- C++ 构造函数+析构函数
3-2 构造函数的调用 Time Limit: 1000MS Memory limit: 65536K 题目描述 通过本题目的练习可以掌握构造函数和析构函数的调用: 创建类A:类中仅包含构造函数和析构 ...