## 流程梳理 ![](https://box.kancloud.cn/67e561ea8cb7eeab5f62d74f4956a5ab_1064x532.png) * 以演示环境为例 ![](https://box.kancloud.cn/ec4be5556a4d1de6ff5079e2c7045ea8_1866x848.png) * 指定后端网关地址 ![](https://img.kancloud.cn/55/36/553650a8d560c06fba0b3a16f1ec0746_1760x562.png) * 切换到back-center的login.html ![](https://img.kancloud.cn/98/12/9812ce1d67104c7cb4e3609671461ee1_1693x703.png) * 切换到auth-server的源码 ![](https://box.kancloud.cn/fd7f05ad1a511b14741f6a47903e761a_1704x630.png) * 切换到auth-server的配置 ![](https://img.kancloud.cn/a6/f6/a6f6fcd23303cb4e1377e17d45bc35a5_1703x306.png) * 票据 redis持久化 ![](https://img.kancloud.cn/13/77/13771ecfeff91758232d460ffddb5ea6_1692x576.png) * 接收前端请求,分配token ![](https://box.kancloud.cn/b84b3f5fc1d4b784aed893532e9a4961_1919x865.png) * 认证登录后,用户信息存储在redis中 ![](https://box.kancloud.cn/aeef42bcdfd6d8f00578af25ff3867f5_1915x815.png) ![](https://box.kancloud.cn/9f780a3c935889000b060a70a2536fe6_1911x763.png) * 以访问用户中心为例(其他业务中心都是此套逻辑) * 前端携带access_token ![](https://box.kancloud.cn/e18a58cd38cdb614d7e834a4a369e23a_1746x748.png) * fiddler抓包 ![](https://box.kancloud.cn/0cc0833fb3eccb5be422a8909fcd4427_1919x692.png) * 后端用户中心必须引入 ![](https://img.kancloud.cn/a4/20/a4205da807759257498df5f298115990_766x214.png) * 后端用户中心的配置 ![](https://img.kancloud.cn/4e/f8/4ef83180a14f9723dbc6e50f596c837a_1707x346.png) * 票据 redis校验机制 ![](https://img.kancloud.cn/41/d6/41d632727f7e5c264bf067df541dfa7c_1695x604.png) * 这样通过uaa-client-spring-boot-starter中的一下流程 ![](https://box.kancloud.cn/7eea410bb58d317640e825e7a3e7c20e_588x703.png) * 完成验证token有效性处理 * 再通过 ![](https://box.kancloud.cn/53e8ffda6d12e4d48acdc6231bdafc4e_1408x773.png) * 完成验证权限标识符 ## Spring Security Oauth基本设计 ![](https://img.kancloud.cn/f3/4f/f34faab4ebaa42a7b18e895474f54332_1101x517.png) * 访问者(Accessor)需要访问某个资源(Resource)是这个场景最原始的需求,但并不是谁都可以访问资源,也不是任何资源都允许任何人来访问,所以中间我们要加入一些检查和防护 * 在访问资源的所经之路上,可能遇到细菌,病毒,不管怎么样,对于要防护的资源来说最好的方法就是设关卡点,对于上图的FilterSecurityInvation,MethodIncation,Jointpoint,这些在spring security oauth中统称SecuredObjects * 我们知道在哪里设置关卡点最合适,下一步就是设置关卡,对应FileSecurityInterceptor,MethodSecurityInterceptor,AspectSecurityInterceptor, 这些关卡统一的抽象类是AbstractSecurityInterceptor * 有关卡点,关卡了以后,到底谁该拦截谁不应该呢,spring security oauth中由 AccessDecisionManager控制 * 最后一个问题,这个谁怎么定义,我们总得知道当前这个访问者是谁才能告诉AccessDecisionManager拦截还是放行,在spring security oauth框架中AuthenticationManager将解决访问者身份认证问题,只有确定你在册了,才可以给授权访问。AuthenticationManager,AccessDecisionManager,AbstractSecurityInterceptor属于spring security框架的基础铁三角。 * 有了以上骨架,真正执行防护任务的其实是SecurityFilterChain中定于的一系列Filter,其中ExceptionTranslationFilter,它负责接待或者送客,如果访问者来访,对方没有报上名来,那么,它就会让访客去登记认证(找AuthenticationManager做认证),如果对方报上名了,但认证失败,那么请重新认证送客,送客的方式是抛出相应的Exception,所以名字叫做ExceptionTranslationFilter。 * 最后,这个filter序列中可能不满足我们的需求,比如增加验证码,所以我们需要在其中穿插自己的Filter实现类,为定制和扩展Spring Security Oauth的防护体系。 * spring security内置的filter序列 ![](https://img.kancloud.cn/5f/30/5f3006bb1d84ed61dbd12c71ec4c5099_1811x888.png) * 执行过滤链 ![](https://img.kancloud.cn/22/bc/22bcdefc6698fc9f5a5e7535566aa2dc_1270x348.png)