在上一篇文章中,我介绍了如何使用Cloud Foundry UAA项目启动OAuth2授权服务器,以及如何使用OAuth2授权代码流程中涉及的一些参与者来填充它。

我已经在Digital Ocean网站上找到了这篇文章,在描述OAuth2授权代码流方面做得非常好,因此,与其重新哈希该流中涉及的内容,我不如直接使用Spring Boot / Spring Security来实现该流。

下图受此处的启发,显示了授权码授予类型的高级流程:

我将有两个应用程序-一个资源服务器公开用户的一些资源,以及一个客户端应用程序代表用户访问这些资源。 如前面的博客文章所述,可以启动授权服务器本身。

文章的其余部分可以更轻松地跟随我的github存储库中的代码

授权服务器

可以使用我之前的博客文章中描述的步骤轻松启动Cloud Foundry UAA服务器。 一旦完成,可以使用以下uaac命令来填充运行样本所需的不同凭据。

这些脚本将为客户端应用程序创建客户端证书,并添加一个名为“ user1”的用户,其范围为“ resource.read”和“ resource.write”。

# Login as a canned client
uaac token client get admin -s adminsecret# Add a client credential with client_id of client1 and client_secret of client1
uaac client add client1 \--name client1 \--scope resource.read,resource.write \-s client1 \--authorized_grant_types authorization_code,refresh_token,client_credentials \--authorities uaa.resource# Another client credential resource1/resource1
uaac client add resource1 \--name resource1 \-s resource1 \--authorized_grant_types client_credentials \--authorities uaa.resource# Add a user called user1/user1
uaac user add user1 -p user1 --emails user1@user1.com# Add two scopes resource.read, resource.write
uaac group add resource.read
uaac group add resource.write# Assign user1 both resource.read, resource.write scopes..
uaac member add resource.read user1
uaac member add resource.write user1

资源服务器

资源服务器通过以下方式公开了一些端点,这些端点使用Spring MVC表示并使用Spring Security进行保护:

@RestController
public class GreetingsController {@PreAuthorize("#oauth2.hasScope('resource.read')")@RequestMapping(method = RequestMethod.GET, value = "/secured/read")@ResponseBodypublic String read(Authentication authentication) {return String.format("Read Called: Hello %s", authentication.getCredentials());}@PreAuthorize("#oauth2.hasScope('resource.write')")@RequestMapping(method = RequestMethod.GET, value = "/secured/write")@ResponseBodypublic String write(Authentication authentication) {return String.format("Write Called: Hello %s", authentication.getCredentials());}
}

公开了两个端点uri –授权用于范围“ resource.read”的“ /安全/读取”和授权用于范围“ resource.write”的“ /安全/写入”

保护这些端点并将应用程序标记为资源服务器的配置如下:

@Configuration
@EnableResourceServer
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {@Overridepublic void configure(ResourceServerSecurityConfigurer resources) throws Exception {resources.resourceId("resource");}@Overridepublic void configure(HttpSecurity http) throws Exception {http.antMatcher("/secured/**").authorizeRequests().anyRequest().authenticated();}
}

该配置以及描述如何验证令牌的属性是使资源服务器运行所需的全部。

客户

使用Spring Security OAuth2的OAuth2的客户端配置也非常简单,@ EnableAuth2SSO批注提取所有必需的配置以为OAuth2流连接Spring安全过滤器:

@EnableOAuth2Sso
@Configuration
public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {@Overridepublic void configure(WebSecurity web) throws Exception {super.configure(web);}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable();//@formatter:offhttp.authorizeRequests().antMatchers("/secured/**").authenticated().antMatchers("/").permitAll().anyRequest().authenticated();//@formatter:on}}

要调用下游系统,客户端必须将OAuth令牌作为标头传递到下游调用中,方法是通过挂接一个称为OAuth2RestTemplate的专用RestTemplate来完成,该模板可以从上下文中获取访问令牌并将其传递到下游连接了一个安全的下游呼叫,如下所示:

public class DownstreamServiceHandler {private final OAuth2RestTemplate oAuth2RestTemplate;private final String resourceUrl;public DownstreamServiceHandler(OAuth2RestTemplate oAuth2RestTemplate, String resourceUrl) {this.oAuth2RestTemplate = oAuth2RestTemplate;this.resourceUrl = resourceUrl;}public String callRead() {return callDownstream(String.format("%s/secured/read", resourceUrl));}public String callWrite() {return callDownstream(String.format("%s/secured/write", resourceUrl));}public String callInvalidScope() {return callDownstream(String.format("%s/secured/invalid", resourceUrl));}private String callDownstream(String uri) {try {ResponseEntity<String> responseEntity = this.oAuth2RestTemplate.getForEntity(uri, String.class);return responseEntity.getBody();} catch(HttpStatusCodeException statusCodeException) {return statusCodeException.getResponseBodyAsString();}}
}

示范

可以使用此处的说明启动客户端和资源服务器。 一旦所有系统启动,访问客户端将向用户显示一个页面,如下所示:


访问安全页面将导致授权服务器显示登录页面:

客户端正在向用户请求“ resource.read”和“ resource.write”范围,提示用户授权这些范围:

假设用户已授权“ resource.read”但未授权“ resource.write”,则令牌将呈现给用户:


在这一点上,如果请求的下游资源要求范围为“ resource.read”,则应检索它:

并且,如果请求的下游资源具有用户未授权的范围,在这种情况下为“ resource.write”:

参考

  • 大多数代码基于此处提供的Cloud Foundry UAA应用程序示例– https://github.com/pivotal-cf/identity-sample-apps
  • 帖子中的代码在这里 :https://github.com/bijukunjummen/oauth-uaa-sample

翻译自: https://www.javacodegeeks.com/2017/03/using-uaa-oauth2-authorization-server-client-resource.html

使用UAA OAuth2授权服务器–客户端和资源相关推荐

  1. uaa 授权_使用UAA OAuth2授权服务器–客户端和资源

    uaa 授权 在上一篇文章中,我介绍了如何使用Cloud Foundry UAA项目启动OAuth2授权服务器,以及如何使用OAuth2授权代码流程中涉及的一些参与者来填充它. 我在Digital O ...

  2. uaa 授权_使用UAA引导OAuth2授权服务器

    uaa 授权 快速获得强大的OAuth2服务器在本地计算机上运行的方法是使用出色的Cloud Foundry UAA项目. UAA用作Cloud Foundry部署中的基础OAUth2授权服务器,可以 ...

  3. 使用UAA引导OAuth2授权服务器

    快速获得强大的OAuth2服务器在本地计算机上运行的方法是使用出色的Cloud Foundry UAA项目. UAA用作Cloud Foundry部署中的基础OAUth2授权服务器,可以大规模扩展,但 ...

  4. 胖哥和几个群友写了个好用的OAuth2授权服务器

    停更这些天,业余时间和粉丝群的几个大佬合作写了一个基于Spring Authorization Server的OAuth2授权服务器的管理控制台项目Id Server,我觉得这个项目能够大大降低OAu ...

  5. spring security oauth2 授权服务器负载均衡解决方案

    研究了好几天的授权服务对资源服务是如何实现负载均衡的 真的是丈二和尚摸不着头脑,研究了几天今天终于找到了一篇文章 真的是翻:烂了 奈何自己太菜 上一下资源服务的yml配置(oauth-server是注 ...

  6. SpringSecurity(二十)---OAuth2:实现资源服务器(上)资源服务器搭建以及直接调用授权服务器模式

    一. 前言 本章将讨论如何使用Spring Security实现一个资源服务器,资源服务器是管理用户资源的组件.另外,学习本章有个前提,需要先把前面搭建授权服务器的相关文章先给阅读,否则可能后面出现的 ...

  7. 微服务架构如何设计API代理网关和OAuth2授权认证框架

    1,授权认证与微服务架构 1.1,由不同团队合作引发的授权认证问题 去年的时候,公司开发一款新产品,但人手不够,将B/S系统的Web开发外包,外包团队使用Vue.js框架,调用我们的WebAPI,但是 ...

  8. 【spring authorization server系列教程】(一)入门系列,spring authorization server简介。快速构建一个授权服务器(基于最新版本0.3.0)

    系列文章目录 [spring authorization server系列教程](一)入门系列,快速构建一个授权服务器 文章目录 系列文章目录 前言 一.目前已实现的功能 二.入门,一步一步快速开始构 ...

  9. springCloud 授权服务器

    之前弄好了非常非常简单的限流,现在就是登陆进来的用户,我们需要对他进行认证.这个时候就要用到我们的授权服务器了. 什么是授权服务器,就是给客户端一个身份,一个token.让我们的服务器认识他. spr ...

最新文章

  1. 【学习——字符串】字符串之一网打尽quq
  2. 日本語のマナーを学びましょう
  3. uniapp 定时执行_ftp上传,完成ftp定时上传、下载只需3步
  4. [剑指offer]面试题第[54]题[JAVA][二叉搜索树的第k大节点][递归][迭代]
  5. Java高并发程序设计学习笔记(十):并发调试和JDK8新特性
  6. 【python】dict4ini和xmltodict模块用途
  7. c语言程序原版PDF,正式版C语言程序设计.pdf
  8. 关于Xshell的使用和网络攻防原理
  9. 常用英语高频词汇android,英语常用高频词汇
  10. 忘记电脑开机密码怎么办、win10、win7忘记开机密码怎么解决
  11. 站在巨人的肩膀上--邵泓鑫
  12. Ubuntu 18.04 笔记本双显卡 Nvidia 驱动安装
  13. pthon3+itchat微信机器人,自定义回复,定时发送晚安,微信小号控制,信息群发功能,获取位置
  14. easyui实例案例介绍
  15. fps php,帧率60帧是什么意思
  16. 滴滴:去年协助警方破获25案件 成立打击黑产专项组
  17. opencv | A02 播放本地视频 调用摄像头
  18. 阿里跟腾讯又㕛叒打起来了,这次是在东南亚
  19. pythonidle如何调字体_python IDLE 背景以及字体的修改
  20. Contiki 系统框架

热门文章

  1. 线上服务器内存分析及问题排查
  2. 数据库连接池的选择及其开发配置
  3. C++描述杭电OJ 2014. 青年歌手大奖赛_评委会打分 ||
  4. 你们好好的学,回头教教我~
  5. java中如何对汉字进行排序?
  6. 第二章 变量、数据类型和运算符
  7. [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
  8. ping 命令使用代理_网络检测知识篇:ping命令使用知识,你知道几点?
  9. mysql duplicate key与replace into对比
  10. Word中标题、图表自动编号的方法