Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
lic
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
inroi
lic
Commits
bf296e19
提交
bf296e19
authored
12月 12, 2022
作者:
inroi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
微调
上级
b7e6c299
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
96 行增加
和
2 行删除
+96
-2
BigOAuth2AuthenticationManager.java
...m/system/oauth/config/BigOAuth2AuthenticationManager.java
+29
-0
CustomAuthenticationEntryPoint.java
...m/system/oauth/config/CustomAuthenticationEntryPoint.java
+50
-0
OAuth2Config.java
...m/src/main/java/com/system/oauth/config/OAuth2Config.java
+7
-1
ResourceServerConfig.java
...in/java/com/system/oauth/config/ResourceServerConfig.java
+10
-1
没有找到文件。
lic-system/src/main/java/com/system/oauth/config/BigOAuth2AuthenticationManager.java
0 → 100644
浏览文件 @
bf296e19
package
com
.
system
.
oauth
.
config
;
import
org.springframework.security.authentication.AnonymousAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.security.core.authority.AuthorityUtils
;
import
org.springframework.security.oauth2.common.exceptions.InvalidTokenException
;
import
org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager
;
import
java.util.UUID
;
/**解决带过期token请求免登录接口被拦截
* 认证管理器
* 继承于原本的OAuth2认证管理器,替代原本的认证管理器
* 目的是做到如果原来的认证管理器如果提取token并认证失败报错的话,提供一个匿名的访问认证 {@link AnonymousAuthenticationToken}
* 最终效果是实现即时请求 无须认证 的接口时带着 过期 或 无效 的 token 时,可以正常继续请求而不会因为 过期或无效的token 导致请求失败
*/
public
class
BigOAuth2AuthenticationManager
extends
OAuth2AuthenticationManager
{
@Override
public
Authentication
authenticate
(
Authentication
authentication
)
throws
AuthenticationException
{
try
{
return
super
.
authenticate
(
authentication
);
}
catch
(
AuthenticationException
|
InvalidTokenException
e
)
{
return
new
AnonymousAuthenticationToken
(
UUID
.
randomUUID
().
toString
(),
"anonymousUser"
,
AuthorityUtils
.
createAuthorityList
(
"ROLE_ANONYMOUS"
));
}
}
}
\ No newline at end of file
lic-system/src/main/java/com/system/oauth/config/CustomAuthenticationEntryPoint.java
0 → 100644
浏览文件 @
bf296e19
package
com
.
system
.
oauth
.
config
;
import
com.system.framework.core.response.StarBosResult
;
import
com.system.utils.JsonUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.authentication.InsufficientAuthenticationException
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint
;
import
org.springframework.stereotype.Component
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
/**
* Oauth2异常信息返回处理
*
* @author ly_rical
*/
@Component
@Slf4j
public
class
CustomAuthenticationEntryPoint
extends
OAuth2AuthenticationEntryPoint
{
@Override
public
void
commence
(
HttpServletRequest
request
,
HttpServletResponse
response
,
AuthenticationException
e
)
throws
IOException
,
ServletException
{
log
.
error
(
e
.
getMessage
());
//如果是client_id和client_secret相关异常 返回自定义的数据格式
if
(
e
instanceof
BadCredentialsException
)
{
response
.
setStatus
(
HttpStatus
.
OK
.
value
());
response
.
setHeader
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
StarBosResult
<
Boolean
>
result
=
StarBosResult
.
fail
(
912
,
"INVALID_CLIENT"
);
result
.
setData
(
false
);
response
.
getWriter
().
write
(
JsonUtil
.
toString
(
result
));
}
else
if
(
e
instanceof
InsufficientAuthenticationException
)
{
//如果是没有携带token
InsufficientAuthenticationException
a
=
(
InsufficientAuthenticationException
)
e
;
response
.
setStatus
(
HttpStatus
.
UNAUTHORIZED
.
value
());
response
.
setHeader
(
"Content-Type"
,
"application/json;charset=UTF-8"
);
StarBosResult
<
Boolean
>
result
=
StarBosResult
.
fail
(
911
,
"未携带TOKEN或无效TOKEN"
);
result
.
setData
(
false
);
response
.
getWriter
().
write
(
JsonUtil
.
toString
(
result
));
}
else
{
super
.
commence
(
request
,
response
,
e
);
}
}
}
lic-system/src/main/java/com/system/oauth/config/OAuth2Config.java
浏览文件 @
bf296e19
...
...
@@ -14,6 +14,7 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.E
import
org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer
;
import
org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer
;
import
org.springframework.security.oauth2.provider.ClientDetailsService
;
import
org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter
;
import
org.springframework.security.oauth2.provider.client.JdbcClientDetailsService
;
import
org.springframework.security.oauth2.provider.code.AuthorizationCodeServices
;
import
org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices
;
...
...
@@ -77,7 +78,6 @@ public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
}
@Bean
public
AuthorizationServerTokenServices
tokenService
()
{
DefaultTokenServices
service
=
new
DefaultTokenServices
();
...
...
@@ -125,6 +125,12 @@ public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Override
public
void
configure
(
AuthorizationServerSecurityConfigurer
security
)
throws
Exception
{
// 过滤器添加Oauth2异常信息返回处理
ClientCredentialsTokenEndpointFilter
filter
=
new
ClientCredentialsTokenEndpointFilter
();
filter
.
setAuthenticationManager
(
authenticationManager
);
filter
.
setAuthenticationEntryPoint
(
new
CustomAuthenticationEntryPoint
());
filter
.
afterPropertiesSet
();
security
.
addTokenEndpointAuthenticationFilter
(
filter
);
security
.
allowFormAuthenticationForClients
();
// security.checkTokenAccess("isAuthenticated()");
// security.tokenKeyAccess("isAuthenticated()");
...
...
lic-system/src/main/java/com/system/oauth/config/ResourceServerConfig.java
浏览文件 @
bf296e19
...
...
@@ -6,6 +6,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import
org.springframework.security.config.http.SessionCreationPolicy
;
import
org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer
;
import
org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter
;
import
org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer
;
//
@Configuration
...
...
@@ -34,6 +35,14 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
// }
@Override
public
void
configure
(
ResourceServerSecurityConfigurer
resources
)
throws
Exception
{
//oauth2添加异常信息管理
resources
.
authenticationEntryPoint
(
new
CustomAuthenticationEntryPoint
());
//添加认证管理器 解决带过期token请求免登录接口被拦截
resources
.
authenticationManager
(
new
BigOAuth2AuthenticationManager
());
}
@Override
public
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
...
...
@@ -42,7 +51,7 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
.
antMatchers
(
"/auth/login"
).
permitAll
()
//登录接口
.
antMatchers
(
"/auth/getCaptcha"
).
permitAll
()
.
antMatchers
(
"/user/image"
).
permitAll
()
.
antMatchers
(
"/swagger-ui.html"
,
"/webjars/**"
,
"/swagger-resources/**"
,
"/v2/**"
,
"doc.html"
,
"/"
).
permitAll
()
.
antMatchers
(
"/swagger-ui.html"
,
"/webjars/**"
,
"/swagger-resources/**"
,
"/v2/**"
,
"doc.html"
,
"/"
).
permitAll
()
.
antMatchers
(
"/test"
).
permitAll
()
.
antMatchers
(
"/webSocketApi/**"
).
permitAll
()
.
antMatchers
(
"/websocket/**"
).
permitAll
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论