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.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
随机推荐
- Azure、数据、AI开发工具
Azure.数据.AI开发工具 在今天召开的 Connect(); 2017 开发者大会上,微软宣布了 Azure.数据.AI 开发工具的内容.这是第一天的 Connect(); 2017 的主题演讲 ...
- 申请edu邮箱
https://blog.csdn.net/w305607610/article/details/86771593 手把手教你申请CCC(City Colleges of Chicago)教育邮箱 台 ...
- 消息队列activeMq 使用介绍
深入浅出 消息队列 ActiveMQhttp://blog.csdn.net/jwdstef/article/details/17380471 一. 概述与介绍 ActiveMQ 是Apache出 ...
- SQL Server里的 ISNULL 与 NULLIF(转)
SQL Server 中有两个参数,语法: ISNULL(check_expression, replacement_value) check_expression 与 replacement ...
- runsv
runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...
- Facebook Gradient boosting 梯度提升 separate the positive and negative labeled points using a single line 梯度提升决策树 Gradient Boosted Decision Trees (GBDT)
https://www.quora.com/Why-do-people-use-gradient-boosted-decision-trees-to-do-feature-transform Why ...
- terminal中 启动ios模拟器,并安装软件
启动运行模拟器: xcrun instruments -w 'iPhone 6 Plus' 在已经启动好的模拟器中安装应用: xcrun simctl install booted Calculato ...
- java实现二叉树的构建以及3种遍历方法(转)
转 原地址:http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历 ...
- 剑指Offer:链表中环的入口节点【23】
剑指Offer:链表中环的入口节点[23] 题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 第一步确定链表中是否包含环,怎么确定呢?我们定义两个指针橙和 ...
- bind、call、apply的区别与实现原理
1.简单说一下bind.call.apply的区别 三者都是用于改变函数体内this的指向,但是bind与apply和call的最大的区别是:bind不会立即调用,而是返回一个新函数,称为绑定函数,其 ...