Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理
token处理之一基本参数配置
处理token时间、存储策略,客户端配置等

以前的都是spring security oauth默认的token生成策略,token默认在org.springframework.security.oauth2.provider.token.DefaultTokenServices 类里生成的,感兴趣可以看看
/**
* 认证服务器
* ClassName: ImoocAuthenticationServerConfig
* @Description: TODO
* @author lihaoyang
* @date 2018年3月12日
*/
@Configuration
@EnableAuthorizationServer //这个注解就是实现了一个认证服务器
public class ImoocAuthenticationServerConfig { }
是uuid,现在做自定义token,token是在认证服务器里生成的,需要在认证服务器也就是ImoocAuthenticationServerConfig 里写代码。

认证服务器配置:
/**
* 认证服务器
* ClassName: ImoocAuthenticationServerConfig
* @Description:
* extends AuthorizationServerConfigurerAdapter 自定义token生成
* @author lihaoyang
* @date 2018年3月12日
*/
@Configuration
@EnableAuthorizationServer //这个注解就是实现了一个认证服务器
public class ImoocAuthenticationServerConfig extends AuthorizationServerConfigurerAdapter{ /*
* 不继承AuthorizationServerConfigurerAdapter,这些bean会自己找,配了,就要自己实现
*/ @Autowired
private AuthenticationManager authenticationManager; @Autowired
private UserDetailsService userDetailsService; //配置文件
@Autowired
private SecurityProperties securityProperties; //token存在redis,默认是在内存
@Autowired
private TokenStore tokenStore; /**
* 配置TokenEndpoint 是 /oauth/token处理的入口点
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
} /**
* 功能:认证服务器会给哪些第三方应用发令牌
* 覆盖了该方法,application.properties里配置的
* security.oauth2.client.clientId = imooc
* security.oauth2.client.clientSecret = imoocsecret
* 就失效了
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//1,写死
// clients.jdbc(dataSource)就是qq场景用的,有第三方公司注册过来,目前场景是给自己的应用提供接口,所以用内存就行
// clients.inMemory()
// //~========================== 在这里配置和写配置文件一样================
// .withClient("imooc") //第三方应用用户名
// .secret("imoocsecret") //密码
// .accessTokenValiditySeconds(7200)//token有效期
// .authorizedGrantTypes("password","refresh_token") //支持的授权模式
// .scopes("all","read","write") //相当于oauth的权限,这里配置了,请求里的必须和这里匹配
// //~=======如果有多个client,这里继续配置
// .and()
// .withClient("xxxxx"); //2,读取配置文件
InMemoryClientDetailsServiceBuilder builder = clients.inMemory();
//判断是否配置了客户端
if(ArrayUtils.isNotEmpty(securityProperties.getOauth2().getClients())){
for (OAuth2ClientProperties config : securityProperties.getOauth2().getClients()) {
builder.withClient(config.getClientId())
.secret(config.getClientSecret())
.accessTokenValiditySeconds(config.getAccessTokenValiditySeconds())
.authorizedGrantTypes("password","refresh_token") //这些也可以配置也可以写死,看心情
.scopes("all","read","write");
}
} }
}
Token默认是存在内存的,这样服务器重启后,就需要重新登录,所以存在redis,(先有个redis,我是装了个windows版的)比存在数据库好,需要配置:
/**
* token存储到redis,默认是在内存不行
* ClassName: TokenStoreConfig
* @Description: token存储策略
* @author lihaoyang
* @date 2018年3月15日
*/
@Configuration
public class TokenStoreConfig { @Autowired
private RedisConnectionFactory redisConnectionFactory; @Bean
public TokenStore redisTokenStore(){
return new RedisTokenStore(redisConnectionFactory);
} }
OAuth相关配置类:
package com.imooc.security.core.properties; /**
* 接口授权客户端配置 ClassName: OAuth2ClientProperties
*
* @Description: 接口授权客户端配置
* @author lihaoyang
* @date 2018年3月15日
*/
public class OAuth2ClientProperties { private String clientId; private String clientSecret; private int accessTokenValiditySeconds = 3600; //没配置就用默认值 // xxxxx在这里扩展配置 public String getClientId() {
return clientId;
} public void setClientId(String clientId) {
this.clientId = clientId;
} public String getClientSecret() {
return clientSecret;
} public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
} public int getAccessTokenValiditySeconds() {
return accessTokenValiditySeconds;
} public void setAccessTokenValiditySeconds(int accessTokenValiditySeconds) {
this.accessTokenValiditySeconds = accessTokenValiditySeconds;
} }
package com.imooc.security.core.properties; /**
* 多个接口客户端,是数组,只有一个的话就不用这个了
* ClassName: OAuth2Properties
* @Description: TODO
* @author lihaoyang
* @date 2018年3月15日
*/
public class OAuth2Properties { private OAuth2ClientProperties[] clients = {}; public OAuth2ClientProperties[] getClients() {
return clients;
} public void setClients(OAuth2ClientProperties[] clients) {
this.clients = clients;
} }
这样只需要在application.properties里配置客户端,可以配置fuo
#第三方应用client_id
imooc.security.oauth2.clients[0].clientId = imooc
imooc.security.oauth2.clients[0].clientSecret = imoocsecret
#token失效时间
imooc.security.oauth2.clients[0].accessTokenValiditySeconds = 3600
imooc.security.oauth2.clients[1].clientId = test
imooc.security.oauth2.clients[1].clientSecret = test
启动demo项目,测试:
redis是空的:

1,模拟获取手机验证码


2,手机验证码登录

响应:
{
"access_token": "ca7417ef-2792-4c81-b441-519f29046cae",
"token_type": "bearer",
"refresh_token": "f32a845b-5b2b-4761-ab0e-ec076c6c0280",
"expires_in": 3599,
"scope": "all read write"
}
redis中存了很多key:

拿token访问用户信息:

响应

总结:
1,认证服务器 ImoocAuthenticationServerConfig 需要继承 AuthorizationServerConfigurerAdapter ,来自定义token的存储、客户端的配置。
2,自定义TokenStoreConfig配置类,存储token在redis中。
2,token默认存在内存中,这样不行,一般存在redis中,所以需要一个redis
存在问题,token失效的时候,redis中的key还存在,怎么给删除了呢?
具体代码在github:https://github.com/lhy1234/spring-security
下一步自定义token的生成策略,用JWT
Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理的更多相关文章
- Spring Security构建Rest服务-1205-Spring Security OAuth开发APP认证框架之Token处理
token处理之二使用JWT替换默认的token JWT(Json Web Token) 特点: 1,自包含:jwt token包含有意义的信息 spring security oauth默认生成的t ...
- Spring Security构建Rest服务-1300-Spring Security OAuth开发APP认证框架之JWT实现单点登录
基于JWT实现SSO 在淘宝( https://www.taobao.com )上点击登录,已经跳到了 https://login.taobao.com,这是又一个服务器.只要在淘宝登录了,就能直接访 ...
- Spring Security构建Rest服务-1201-Spring Security OAuth开发APP认证框架之实现服务提供商
实现服务提供商,就是要实现认证服务器.资源服务器. 现在做的都是app的东西,所以在app项目写代码 认证服务器: 新建 ImoocAuthenticationServerConfig 类,@Ena ...
- Spring Security构建Rest服务-1200-SpringSecurity OAuth开发APP认证框架
基于服务器Session的认证方式: 前边说的用户名密码登录.短信登录.第三方登录,都是普通的登录,是基于服务器Session保存用户信息的登录方式.登录信息都是存在服务器的session(服务器的一 ...
- Spring Security构建Rest服务-1202-Spring Security OAuth开发APP认证框架之重构3种登录方式
SpringSecurityOAuth核心源码解析 蓝色表示接口,绿色表示类 1,TokenEndpoint 整个入口点,相当于一个controller,不同的授权模式获取token的地址都是 /oa ...
- Spring Security构建Rest服务-1203-Spring Security OAuth开发APP认证框架之短信验证码登录
浏览器模式下验证码存储策略 浏览器模式下,生成的短信验证码或者图形验证码是存在session里的,用户接收到验证码后携带过来做校验. APP模式下验证码存储策略 在app场景下里是没有cookie信息 ...
- Spring Cloud构建微服务架构(三)消息总线
注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...
- 构建微服务:Spring boot
构建微服务:Spring boot 在上篇文章构建微服务:Spring boot 提高篇中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jp ...
- Spring Cloud构建微服务架构(二)服务消费者
Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...
随机推荐
- java中的实例化
java中的new用于实例化一个对象 T1 a= new T1(); T2 b= new T1(); 区别: 问题1:不是实例化一个a,是实例化一个T1 T1 的一个 对象的引用 a 指向了堆空间里的 ...
- SPSS-两变量相关性分析
两个变量之间存在确定性:关系和不确定关系(会存在一定的波动范围),就好比你的亲生母亲绝对只有一个,而你的亲叔叔可能有好几个(可以在1叔—4叔之间波动) 相关性一般分为 1:强正相关关系 (一个值 ...
- Java(Android)线程池[转]
介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...
- Android绘图板的开发
>>继承自View >>使用Canvas绘图 每次View组件上的图形状态数据发生了改变,都应该通知View组件重写调用onDraw(Canvas canvas)方法重绘该组件 ...
- 发个招聘贴,魔都求手游C++后端,UNITY前端,开发实习生
上海游旺网络科技有限公司成立于2015年5月,是一家极具潜力的新创移动游戏公司.公司初创团队均来自腾讯,盛大,畅游,墨麟,蜗牛等知名互联网公司,公司创始人团队参与制作过<鬼吹灯><Q ...
- [leetcode] 15. Plus One
这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...
- 【 PLSQL Developer安装、tnsnames.ora配置 解答】
使用plsql远程连接数据库需要安装plsql工具+ oracle的远程客户端 在不登录的状态打开plsql: 点击工具---首选项:指定oracle客户端的安装路径: C:\javaSoft\PLS ...
- 构建NetCore应用框架之实战篇(七):BitAdminCore框架登录功能源码解读
本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.简介 1.登录功能完成后,框架的雏形已经形成,有必要进行复习. 2 ...
- poj3233 Matrix Power Series(矩阵快速幂)
题目要求的是 A+A2+...+Ak,而不是单个矩阵的幂. 那么可以构造一个分块的辅助矩阵 S,其中 A 为原矩阵,E 为单位矩阵,O 为0矩阵 将 S 取幂,会发现一个特性: Sk +1右上角 ...
- 四,mysql优化——sql语句优化之索引二
1,在什么列适合添加索引 (1)较频繁的作为查询条件字段应该添加索引 select * from emp where empid = 2; (2)唯一性太差的字段不适合添加索引,即时频繁作为查询条件. ...