突然被问,你是做技术的怎么不走技术路线呢?是啊~仔细想想至今做了这么多年的技术,研发过的系统&产品五花八门,涉及到的领域各行各业:政府、军队、公安、国安、石油&石化、金融、教育、华为等.传统行业——互联网——物联网.在此过程中不断地切换不同的角色,面对不同的客户,体验不同的洗礼...常常跟朋友聊听说其实我们是"演员".继续在乐不疲惫中砥砺前行,或许是因为真正的热爱吧!

少年已去,岁月已走.也许,平庸的人要实现梦想,就是好好地活着,活得比别人久一些吧!

开始之旅

官网的SpringSecurity介绍,翻译:Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架.它是用于保护基于Spring的应用程序的实际标准.Spring Security是一个框架,致力于为Java应用程序提供身份验证和授权.与所有Spring项目一样,Spring Security的真正强大之处在于可以轻松扩展以满足自定义要求

官网OAuth介绍,翻译:OAuth 2.0是用于授权的行业标准协议.它致力于简化客户端开发人员的工作,同时为Web应用程序,桌面应用程序,移动电话和客厅设备提供特定的授权流程.

模块说明

study
├── study-api -- 各种API远程接口
├── study-auth -- 认证授权中心服务,端口3008
└── study-core -- 核心模块
     ├── study-common -- 公共类相关
     ├── study-data -- 缓存和数据相关
     ├── study-security -- 安全资源相关
├── study-sys --各种管理服务,端口8888
├── study-web -- LayUi,端口8866

终端配置(核心表)

drop table if exists `sys_oauth_client_details`;
create table `sys_oauth_client_details` (
  `id` int(11)  NOT NULL    AUTO_INCREMENT,
  `client_id`                  varchar(255)  not null          comment '终端编号',
  `client_secret`              varchar(255)  not null          comment '终端安全码',
  `resource_ids`               varchar(255)  default null      comment '资源ID标识',
  `scope`                      varchar(255)  not null          comment '终端授权范围',
  `authorized_grant_types`     varchar(255)  not null          comment '5种oauth授权方式',
  `web_server_redirect_uri`    varchar(255)  default null      comment '服务器回调地址',
  `authorities`                varchar(255)  default null      comment '访问资源所需权限',
  `access_token_validity`  int(11) default null comment 'access_token的有效时间值(秒)',
  `refresh_token_validity`  int(11) default null comment 'refresh_token的有效时间值(秒)',
  `additional_information` varchar(4096) default null      comment '附加信息',
  `autoapprove`            tinyint(4)  default null      comment '是否自动授权',
   primary key (`id`)
) engine=Innodb comment = '终端配置表' CHARSET=utf8mb4;

insert into `sys_oauth_client_details` values (1,'test','test', NULL,'server', 'password,refresh_token','',NULL,3600,7200,NULL,NULL);
insert into `sys_oauth_client_details` values (2,'simple','simple',NULL,'server', 'password,client_credentials,refresh_token','',NULL,3600,7200,NULL,NULL);

认证中心Token来龙去脉

  1. 配置终端

ENC(****)****等于终端表的client_id值即test,该技能后期在做讲解.

  1. 开启授权服务
@EnableAuthorizationServer //开启授权服务
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    
 //......此处省略
    
   /**
     * 配置身份认证器,配置认证方式,TokenStore,TokenGranter,OAuth2RequestFactory
     *
     * @param endpoints
     */
 public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
  endpoints
                //接收GET和POST
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
                //指定token存储方式
                .tokenStore(tokenStore)
                //令牌生成
                .tokenEnhancer(tokenEnhancer)
                //用户账号密码认证
                .userDetailsService(userDetailsService)
                // 指定认证管理器
                .authenticationManager(authenticationManagerBean)
                //reuseRefreshTokens是重用刷新令牌  false代表每次获取新的
                .reuseRefreshTokens(false)
                //自定义异常
                .exceptionTranslator(new CustomWebResponseExceptionTranslator());
}

此处的tokenStore可指定如下方式:

  1. 获取用户信息

通过实现UserDetailsService调用loadUserByUsername方法来获取数据中存在的用户权限信息等.其中remoteUserService即为远程服务调用

  1. 启动端口号分别为3008和8888服务

先配置client_id和client_secret,并使用Base64编码之后作为请求头.先拼接:client_id:client_secrettest:test 使用任意Base64工具编码:dGVzdDp0ZXN0,其中client_id和client_secret必须是在终端配置表中存在的数据

Postman中调用oauth/token接口

如果不带此client访问接口

带上client、用户名、密码、授权类型、授权范围请求接口

调用链分析

Spring Security OAuth核心源码

下回分解如何返回自定义Token~敬请关注!!!

我是天开易想,一位懂互联网研发&架构的户外、篮球老铁|致力于提升认知&上升空间&终生学习者|希望和大家一起成长!世界上最好的关系是相互成就,关注点赞转发,感谢感恩

posted on 2020-09-23 11:44  thinkYi  阅读(233)  评论(0编辑  收藏  举报