Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
起风了_ZXJ
oauth-demo-new
提交
f2b93d3b
O
oauth-demo-new
项目概览
起风了_ZXJ
/
oauth-demo-new
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
oauth-demo-new
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f2b93d3b
编写于
1月 26, 2022
作者:
起风了_ZXJ
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
spring-security-oauth2 使用 TokenEnhancer 自定义生成令牌
上级
3e636557
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
43 addition
and
5 deletion
+43
-5
src/main/java/com/example/oauth2/test2/AuthorizationServerConfig.java
...a/com/example/oauth2/test2/AuthorizationServerConfig.java
+3
-0
src/main/java/com/example/oauth2/test2/CustomTokenEnhancer.java
...in/java/com/example/oauth2/test2/CustomTokenEnhancer.java
+40
-0
src/main/java/com/example/oauth2/test2/UserDetailsServiceImpl.java
...java/com/example/oauth2/test2/UserDetailsServiceImpl.java
+0
-5
未找到文件。
src/main/java/com/example/oauth2/test2/AuthorizationServerConfig.java
浏览文件 @
f2b93d3b
...
...
@@ -40,6 +40,8 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
private
ClientDetailsService
clientDetailsService
;
@Resource
private
TokenStore
tokenStore
;
@Resource
private
CustomTokenEnhancer
customTokenEnhancer
;
/**
...
...
@@ -63,6 +65,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
service
.
setSupportRefreshToken
(
true
);
service
.
setClientDetailsService
(
clientDetailsService
);
service
.
setTokenStore
(
tokenStore
);
service
.
setTokenEnhancer
(
customTokenEnhancer
);
service
.
setAccessTokenValiditySeconds
(
7200
);
// 令牌默认有效期2小时
//service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天
return
service
;
...
...
src/main/java/com/example/oauth2/test2/CustomTokenEnhancer.java
0 → 100644
浏览文件 @
f2b93d3b
package
com.example.oauth2.test2
;
import
org.springframework.security.oauth2.common.DefaultOAuth2AccessToken
;
import
org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken
;
import
org.springframework.security.oauth2.common.OAuth2AccessToken
;
import
org.springframework.security.oauth2.common.OAuth2RefreshToken
;
import
org.springframework.security.oauth2.provider.OAuth2Authentication
;
import
org.springframework.security.oauth2.provider.token.TokenEnhancer
;
import
org.springframework.stereotype.Component
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.UUID
;
@Component
public
class
CustomTokenEnhancer
implements
TokenEnhancer
{
@Override
public
OAuth2AccessToken
enhance
(
OAuth2AccessToken
accessToken
,
OAuth2Authentication
authentication
)
{
if
(
accessToken
instanceof
DefaultOAuth2AccessToken
)
{
DefaultOAuth2AccessToken
token
=
((
DefaultOAuth2AccessToken
)
accessToken
);
token
.
setValue
(
getNewToken
());
OAuth2RefreshToken
refreshToken
=
token
.
getRefreshToken
();
if
(
refreshToken
instanceof
DefaultOAuth2RefreshToken
)
{
token
.
setRefreshToken
(
new
DefaultOAuth2RefreshToken
(
getNewToken
()));
}
Map
<
String
,
Object
>
additionalInformation
=
new
HashMap
<
String
,
Object
>();
//自定义返回的拓展字段
additionalInformation
.
put
(
"client_id"
,
authentication
.
getOAuth2Request
().
getClientId
());
additionalInformation
.
put
(
"custom_key"
,
"custom_key"
);
additionalInformation
.
put
(
"username"
,
authentication
.
getOAuth2Request
().
getRequestParameters
().
get
(
"username"
));
token
.
setAdditionalInformation
(
additionalInformation
);
return
token
;
}
return
accessToken
;
}
private
String
getNewToken
()
{
return
"自定义token"
+
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
);
}
}
src/main/java/com/example/oauth2/test2/UserDetailsServiceImpl.java
浏览文件 @
f2b93d3b
...
...
@@ -10,8 +10,6 @@ import org.springframework.security.core.userdetails.UserDetails;
import
org.springframework.security.core.userdetails.UserDetailsService
;
import
org.springframework.security.core.userdetails.UsernameNotFoundException
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.oauth2.common.OAuth2AccessToken
;
import
org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
...
...
@@ -33,9 +31,6 @@ public class UserDetailsServiceImpl implements UserDetailsService {
*/
@Override
public
UserDetails
loadUserByUsername
(
String
username
)
throws
UsernameNotFoundException
{
OAuth2AccessToken
oAuth2AccessToken
=
new
RedisTokenStore
(
redisConnectionFactory
).
readAccessToken
(
"7a99cc45-42ce-4447-acbe-f30756dde928"
);
System
.
out
.
println
(
oAuth2AccessToken
);
//登录账号
logger
.
info
(
"当前登录用户:username:{} 登录时间:{}"
,
username
,
new
Date
());
// 根据账号去数据库查询...
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录