OAuth2.0系列四:OAuth2.0简化模式
简化模式
有些纯前端应用,必须将令牌储存在前端。那么可以使用简化模式(隐藏式)来申请授权,颁发令牌。
- 浏览器向授权服务器申请授权,并设置response_type=token,表示直接返回令牌
- 授权服务器通过校验后跳转到重定向地址并返回token
- 浏览器通过token去申请资源
这种方式把令牌直接传给前端是很不安全的。因此,只能用于一些安全要求不高的场景,并且令牌的有效期必须非常短,通常
浏览器关掉,令牌就失效了。
代码实战
第一步,创建maven项目implicit_server,pom文件如下所示
<parent><artifactId>microservice-parent</artifactId><groupId>com.curise.microservice</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>implicit_server</artifactId><description>OAuth2.0简化模式</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- for OAuth 2.0 --><dependency><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
第二步,创建授权服务器,这里设置了简化模式令牌有效时间120s
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory()// 客户端id.withClient("app")// 客户端密钥.secret("123")// 权限.scopes("read","write")// 获取授权码后重定向地址.redirectUris("http://localhost:9000/callback")// 授权码和刷新token.authorizedGrantTypes("authorization_code","refresh_token").and().withClient("app1").secret("1234").scopes("read", "write")// 密码模式和刷新token.authorizedGrantTypes("password", "refresh_token").and().withClient("app2").secret("1234").scopes("read", "write").redirectUris("http://localhost:9000/callback")// 令牌有效时间120s.accessTokenValiditySeconds(120)// 简化模式.authorizedGrantTypes("implicit");}
}
第三步,创建资源服务器
@Configuration
@EnableResourceServer
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {@Overridepublic void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().requestMatchers()// /api/**请求需要OAuth鉴权.antMatchers("/api/**");}
}
第四步,创建配置文件
server.port=8080
security.user.name=root
security.user.password=root
第五步,创建Controller输出资源
@RestController
public class UserController {@GetMapping("/api/userInfo")public ResponseEntity<UserInfo> getUserInfo(){User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();String email = user.getUsername() + "@qq.com";UserInfo userInfo = new UserInfo();userInfo.setUsername(user.getUsername());userInfo.setEmail(email);return ResponseEntity.ok(userInfo);}}
第六步,启动服务
第七步,浏览器发送请求获取令牌
http://localhost:8080/oauth/authorize?client_id=app2&redirect_uri=http://localhost:9000/callback&response_type=token&scope=read
client_id:客户端id
redirect_uri:申请授权后重定向地址(这里是随便写的)
response_type:设置返回token
scope:设置权限是read
授权后调整的url如图所示
第八步,通过令牌获取用户信息
代码已经共享到GitHub,地址:https://github.com/WYA1993/microservice
OAuth2.0系列四:OAuth2.0简化模式相关推荐
- Vue3.0系列(一): VUE3.0的新特性
和Vue2.0 相比,Vue3.0有哪些亮点: 序号 特性 解析 1 Performance 性能上比Vue2.0快1.3-2倍 2 Tree shaking support 按需编译,体积更加轻量化 ...
- zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控
Zabbix3.0.4添加对Nginx服务的监控 通过Nginx的http_stub_status_module模块提供的状态信息来监控,所以在Agent端需要配置Nginx状态获取的脚本,和添加ke ...
- Microsoft Enterprise Library 5.0 系列(四) Logging Application Block
企业库日志应用程序模块工作原理图: 从上图我们可以看清楚企业库日志应用程序模块的工作原理,其中LogFilter,Trace Source,Trace Listener,Log Formatter的信 ...
- diskgeniusv4.4.0_一文上手最新Tensorflow2.0系列|TensorFlow2.0安装
本文中我们将会详细讲解TensorFlow2.0安装.Tensorflow兼容性最好的是Unix内核的系统,如Linux,MacOS等.另外TensorFlow的GPU版本仅支持Linux环境,不支持 ...
- 实战Spring Boot 2.0系列(一) - 使用Gradle构建Docker镜像
前言 通常我们使用 Dockerfile 来构建项目的 Docker 镜像.但是也有使用 gradle 在编译项目的时候一起把镜像给 构建 并 上传 的需求.本文将会讲解如何使用 gradle 编写并 ...
- oauth2.0 php简化模式,OAuth2.0学习(1-5)授权方式2-简化模式(implicit grant type)
授权方式2-简化模式(implicit grant type) 简化模式(implicit grant type)不通过第三方应用程序的服务器,直接在浏览器中向认证服务器申请令牌,跳过了"授 ...
- 接口测试工具-Jmeter使用笔记(八:模拟OAuth2.0协议简化模式的请求)
背景 博主的主要工作是测试API,目前已经用Jmeter+Jenkins实现了项目中的接口自动化测试流程.但是马上要接手的项目,API应用的是OAuth2.0协议授权,并且采用的是简化模式(impli ...
- Oauth2.0的四种模式
1. 授权码模式 (1)资源拥有者打开客户端,客户端要求资源拥有者给予授权,它将浏览器被重定向到授权服务器,重定向时会 附加客户端的身份信息.如: /uaa/oauth/authorize? clie ...
- OAuth2.0系列之使用JWT令牌实践教程(八)
OAuth2.0系列之使用JWT令牌实践教程(八) 1.文章前言介绍 2.例子实验验证 3.功能简单测试 OAuth2.0系列博客: OAuth2.0系列之基本概念和运作流程(一) OAuth2.0系 ...
- 一口气说出 OAuth2.0 的四种授权方式
上周我的自研开源项目开始破土动工了,<开源项目迈出第一步,10 选 1?页面模板成了第一个绊脚石 > ,密谋很久才付诸行动,做这个的初衷就是不想让自己太安稳,技术这条路不进步就等于后退,必 ...
最新文章
- QIIME 2教程. 15样品分类和回归q2-sample-classifier(2020.11)
- 转载:PHP JSON_ENCODE 不编码中文汉字的方法
- How Tomcat Works(十一)
- mongodb安装失败与解决方法(附安装教程)
- Python下Selenium PhantomJs设置header的方法
- 老板让你抗住千万级流量,如何做架构设计?
- MAT之ELM:ELM基于近红外光谱的汽油测试集辛烷值含量预测结果对比
- Spring接入RabbitMQ
- linux 内存使用原理,linux中内存使用原理
- rsync的原理和安装使用及配制详解(三)(转)
- 基于JAVA+SpringMVC+Mybatis+MYSQL的校园二手市场系统
- 随想录(关于signal的实验)
- memcache/redis 缓存学习笔记
- mysqldump对mysql数据库的影响
- VScode输出中文乱码的解决方法------测试过可以用
- 高性能图像放大算法——waifu2x方法
- Python求积分(定积分)
- 直播系统源码搭建、手机直播源码APP平台开发制作
- 【计算几何】德劳内三角剖分算法 | 利用 scatter 绘制散点图 | 实现外接圆生成 | scipy库的 Dealunay 函数 | 实战: A-B间欧氏距离计算
- 2022-2028全球快餐肉汤食品行业调研及趋势分析报告