[TOC] ## uaa-client-spring-boot-starter 在前面的项目中,咱们使用了 [04.security-core模块](04.security-core%E6%A8%A1%E5%9D%97.md)来引用资源服务器,本模块重构security-core,改用spring.factories 装配。 ## 资源服务器 * 要访问资源服务器受保护的资源需要携带令牌(从授权服务器获得) * 客户端往往同时也是一个资源服务器,各个服务之间的通信(访问需要权限的资源)时需携带访问令牌 ## 重构后流程 ![](https://img.kancloud.cn/fe/7a/fe7ac9108d57ec9d64e8d7d55fe5763b_1006x660.png) ## 功能 * 通用资源服务器校验 * 通用的token校验机制(TokenStore) * 通用的访问控制(OpenAuthorizeConfigManager) * 通用的异常配置(SecurityHandlerConfig,ExceptionHandlerAdvice) ## com.open.capacity.uaa.client.UAAClientAutoConfig的作用 * 提供OAuth2AuthenticationProcessingFilter保护我们的API接口 * 提供白名单免除OAuth2AuthenticationProcessingFilter校验API接口 * 提供方法级权限校验 * 提供应用级API校验 ### 开启资源服务器 ![](https://img.kancloud.cn/d4/01/d4011c77e4fda181261578440f644e08_1686x747.png) * @EnableResourceServer引入OAuth2AuthenticationProcessingFilter过滤器 * 通过继承 ResourceServerConfigurerAdapter 类来配置资源服务器 * OAuth2AuthenticationProcessingFilter代码如何查看( sts中 (Ctrl +shift+T)) ![](https://box.kancloud.cn/a11aeff8aefd6fe6cdacf138e30bd923_839x493.png) ### ResourceServerSecurityConfigurer 可配置属性 * tokenServices:ResourceServerTokenServices 类的实例,用来实现令牌业务逻辑服务 * resourceId:这个资源服务的ID,这个属性是可选的,但是推荐设置并在授权服务中进行验证 * tokenExtractor 令牌提取器用来提取请求中的令牌 * 请求匹配器,用来设置需要进行保护的资源路径,默认的情况下是受保护资源服务的全部路径 * 受保护资源的访问规则,默认的规则是简单的身份验证(plain authenticated) * 其他的自定义权限保护规则通过 HttpSecurity 来进行配置 * 使用 DefaultTokenServices 在资源服务器本地配置令牌存储、解码、解析方式 ## uaa-client-spring-boot-starter的作用 > 包装资源服务器,作为独立单元为api-gateway,user-center等提供安全保障。 ## 开启是否强制校验 * 弱校验:不传token可以访问,传了token才会校验 * 强校验:api一定需要token ![](https://img.kancloud.cn/ab/08/ab08e54a288c10b9196b007ee3ed3682_1721x671.png) ## spring自动装配 ![](https://img.kancloud.cn/6b/22/6b22c9c44c18ceca5bae21231eb5c05e_1685x433.png) ## uaa-client-spring-boot-starter代码清单详解 认证授权分离架构的oauth资源服务器,需要在代码中设置一个门岗,验证认证中心的token ,门岗代码如下 ![](https://img.kancloud.cn/f5/96/f596ef7b0922d7c032bb927f256dad80_1719x609.png) 注意以下被注解,生产需要打开,配置用户的api权限,注释后有token即可访问 `// 开启spring security 注解 // @EnableGlobalMethodSecurity(prePostEnabled = true)` ### token票据验证 ![](https://box.kancloud.cn/7eea410bb58d317640e825e7a3e7c20e_588x703.png) 票据 redis持久化 ![](https://img.kancloud.cn/41/d6/41d632727f7e5c264bf067df541dfa7c_1695x604.png) ![](https://img.kancloud.cn/49/26/492652dfb773eca75d89ae59514f4b22_1581x531.png) 作为一个认证授权分离的架构,采用此种门岗+票据验证方式是一种效率最高的方式.读相关源码可以验证此结论,此方案已生产验证。 ![](https://box.kancloud.cn/1c347e7b86407be804f33384ee113f5f_963x218.png) ## @PreAuthorize 原理 [07.@PreAuthorize可以用来控制一个方法是否能够被调用。](03.PreAuthorize%E6%B3%A8%E8%A7%A3%E5%88%86%E6%9E%90.md) ## 网关api权限设计 ![](https://img.kancloud.cn/c1/68/c1685025219f816bb33f1840592b5599_855x351.png) 相同用户,不同应用的权限隔离 客户端模式 : 客户端A 申请的token ,可以访问/api-user/menu/current , 客户端B 申请的token,不让访问/api-user/menu/current 密码模式: 客户端模式 : 客户端A admin用户 申请的token ,可以访问/api-user/menu/current , 客户端B admin用户 申请的token,不让访问/api-user/menu/current 参考issue:[https://gitee.com/owenwangwen/open-capacity-platform/issues/IRG23]() 网关引依赖 ![](https://img.kancloud.cn/11/b9/11b9dd2d759c8a097a81c0e5eaace90b_1594x285.png) 网关是否开启基于应用隔离,代码注释了,只是基于token的合法性校验,按建议开启是否启用api接口服务权限 ![](https://img.kancloud.cn/5c/2f/5c2f210132172402df18b9a824192230_1686x699.png) 通过clientID隔离服务权限 ![](https://img.kancloud.cn/f6/16/f616866b31c57c5237dfe504060ae33b_1688x735.png) (最新代码从新封装到网关,RbacService适用于api-gateway的api权限设计 ![](https://img.kancloud.cn/64/c4/64c4b08dbde7005c43e6b5090e013188_1836x571.png)) 通过应用分配服务权限 ![](https://img.kancloud.cn/80/36/803611b8b8c3de222f87d60410699a32_1918x887.png)