您现在的位置是:IT资讯 >>正文
OAuth2授权服务器Id Server一键生成配置原理
IT资讯4人已围观
简介OAuth2客户端的配置参数非常多,虽然Id Server通过控制台可视化解决了创建OAuth2客户端的问题。但是如何进一步降低OAuth2的使用难度,把创建的OAuth2客户端 ...
OAuth2客户端的权服配置参数非常多,虽然Id Server通过控制台可视化解决了创建OAuth2客户端的键生问题。但是成配如何进一步降低OAuth2的使用难度,把创建的置原OAuth2客户端转化为配置成为了刚需,从技术角度上感觉也并不是权服很难实现。
我们先来看看最终效果 ,键生点击配置生成按钮即可直接生成Spring Security的成配客户端yaml配置 :

这个效果是如何实现的源码库呢?
highlightjs主要依托于highlightjs这个代码高亮库,平常我们在各大技术社区看到的置原五颜六色的代码块很多就依赖这个JS库,连我自己的权服技术博客felord.cn都用了这个类库来做代码片段美化。它使用起来很简单 :
复制<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="https://felord.cn/css/gruvbox-dark.min.css"> <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script> <script>hljs.initHighlightingOnLoad();</script></head><body><pre > <code class="yaml"> spring:#
application: name: id-server
</code></pre></body></html>1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.引入一个风格样式和highlight.js库,键生再加一个初始化脚本就完成了 。成配然后在<pre><code>中编写带缩进的置原代码就可以了,注意code标签要加上对应语言或者脚本的权服class类 ,出来就是键生这样的高防服务器效果:

到这里思路就很明确了 ,把参数项的成配值动态化就可以了,我期望达到这样的效果:
复制<pre > <code class="yaml"> spring:#
application: name: ${ appName} </code></pre>1.2.3.4.5.6.7.8.但事实上我大意了 ,我用了thymeleaf模板,我没有找到thymeleaf可以固化配置项到页面的办法 ,所以这个带缩进的格式需要后端生成 ,然后按照thymeleaf的要求渲染,于是我写了一个非常复杂的建站模板方法:
复制 @GetMapping("/system/client/yaml/{ id}") public String yaml(Model model, @PathVariable String id) { OAuth2Client oauth2Client = clientRepository.findClientById(id); String clientName = oauth2Client.getClientName(); String clientId = oauth2Client.getClientId(); Set<RedirectUri> redirectUris = oauth2Client.getRedirectUris(); String uris = redirectUris.stream() .map(RedirectUri::getRedirectUri) .collect(Collectors.joining(",")); Set<OAuth2GrantType> authorizationGrantTypes = oauth2Client.getAuthorizationGrantTypes(); String types = authorizationGrantTypes.stream() .map(OAuth2GrantType::getGrantTypeName) .collect(Collectors.joining(",")); String method = oauth2Client.getClientAuthenticationMethods().stream() .map(ClientAuthMethod::getClientAuthenticationMethod) .collect(Collectors.joining(",")); String scopes = Stream.concat( oauth2Client.getScopes().stream() .map(OAuth2Scope::getScope), Stream.of(OidcScopes.OPENID)) .collect(Collectors.joining(",")); LinkedHashMap<String, Object> client = new LinkedHashMap<>(); LinkedHashMap<String, Object> clientRegistration = new LinkedHashMap<>(); clientRegistration.put("client-id", clientId); clientRegistration.put("client-secret", "请填写你的OAuth2客户端密码"); clientRegistration.put("redirect-uri", "请从" + uris + "指定一个"); clientRegistration.put("authorization-grant-type", "请从 " + types + " 指定一个"); clientRegistration.put("client-authentication-method", method); clientRegistration.put("scope", scopes); client.put("registration", Collections.singletonMap(clientName, clientRegistration)); client.put("provider", Collections.singletonMap(clientName, Collections.singletonMap("issuer-uri", "http://localhost:9000"))); Map<String, Object> spring = Collections.singletonMap("spring", Collections.singletonMap("security", Collections.singletonMap("oauth2", Collections.singletonMap("client", client)))); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(dumperOptions); String dump = yaml.dump(spring); model.addAttribute("yaml", dump); return "/system/client/yaml"; }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.效果自然是有的 ,但是非常差强人意。

无法生成注释 ,而且换行不受控制 ,尤其套了9个Map让我抓狂。
优化是不是我把问题想得太复杂了呢?于是最终我把yaml的格式模板这样做了:
复制 String yml = "spring:\n" + " security:\n" + " oauth2:\n" + " client:\n" + " registration:\n" + " # 这里为客户端名称可自行更改\n" + " " + clientName + ":\n" + " client-id: " + clientId + "\n" + " # 密码为注册客户端时的模板下载密码\n" + " client-secret: 请填写您记忆的OAuth2客户端密码\n" + " # 只能选择一个\n" + " redirect-uri: 请从" + uris + "指定一个\n" + " # 只能选择一个\n" + " authorization-grant-type: " + types + "三选一\n" + " client-authentication-method: " + method + "\n" + " scope: " + scopes + "\n" + " provider:\n" + " " + clientName + ":\n" + " # 要保证授权服务器地址可以被客户端访问\n" + " issuer-uri: http://localhost:9000"; model.addAttribute("yaml", yml);1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.当然这是为了兼容Java8,如果换了Java17直接就用字符串模板了,甚至这里我还能写注释 ,最终的效果是这样的:

效果比上一个方案好了很多 ,当然或许你还有更好的方案,让我们集思广益。
关于Id Server仓库地址:https://github.com/NotFound403/id-server 欢迎star 。
Id Server是免费模板一个基于Spring Authorization Server的开源的授权服务器,大大降低OAuth2授权服务器的学习使用难度 ,提供UI控制台,动态权限控制,方便OAuth2客户端管理,可以一键生成Spring Security配置,开箱即用,少量配置修改就可部署,代码开源 ,方便二次开发 ,支持OAuth2四种客户端认证方式和三种授权模式 。源码下载
Tags:
转载:欢迎各位朋友分享到网络,但转载请说明文章出处“信息技术视野”。http://www.bziz.cn/html/179a299818.html
相关文章
勒索软件谈判的注意事项
IT资讯勒索软件是组织在过去几年中面临的最具破坏性的恶意软件威胁之一,而且没有迹象表明攻击者会很快停止。这对他们来说太有利可图了。赎金要求从数万美元增长到数百万甚至数千万美元,因为攻击者了解到许多组织愿意支付 ...
【IT资讯】
阅读更多普洛斯数据中心荣膺“2024年中国算力中心服务商十强”
IT资讯近日,在2024全球数字经济大会“智能算力创新发展论坛”上,中国信息通讯研究院发布《中国算力中心服务商分析报告2024年)》以下简称《报告》),并公布“2024年中国算力中心服务商十强”榜单。普洛斯数 ...
【IT资讯】
阅读更多推动下一代数据中心向400G和800G迈进
IT资讯在大数据、云计算和物联网(IoT)时代,对更快、更高效的数据中心的需求不断增长。 数据中心需要支持更高的网络速度和带宽,以处理大量数据流入和处理。 因此,向400G以太网(400 ...
【IT资讯】
阅读更多
热门文章
友情链接
- 第二届“长城杯”信息安全铁人三项赛(防护赛)总决赛圆满收官
- Apache Roller 曝出高危漏洞(CVSS 10.0):密码修改后会话仍持续有效
- 盘点2024年生成式AI带来的五大新型安全威胁
- 新型 MassJacker 剪贴板恶意软件,捆绑在盗版软件中偷窃加密货币钱包
- 无需拆机!Windows 11 BitLocker加密文件被破解
- 研究人员利用 AI 越狱技术大量窃取 Chrome 信息
- 2025 年 CISO 最青睐的五大安全框架
- 网络安全技术:防火墙、VPN、入侵检测基础
- 约22000名WAB客户受到针对第三方供应商的零日攻击的影响
- 2024年综述:热门数据泄露事件和行业趋势 源码库亿华云网站建设b2b信息平台云服务器香港物理机企业服务器