Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zlt2000
microservices-platform
提交
8bc688b8
microservices-platform
项目概览
zlt2000
/
microservices-platform
9 个月 前同步成功
通知
16
Star
4
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8bc688b8
编写于
5月 15, 2022
作者:
Z
zhult13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #I545XE
上级
9020eb9f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
70 addition
and
1 deletion
+70
-1
zlt-commons/zlt-auth-client-spring-boot-starter/pom.xml
zlt-commons/zlt-auth-client-spring-boot-starter/pom.xml
+5
-0
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/WcAuthConfigurator.java
.../com/central/oauth2/common/config/WcAuthConfigurator.java
+34
-0
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/util/AuthUtils.java
...c/main/java/com/central/oauth2/common/util/AuthUtils.java
+27
-0
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/SpringUtil.java
...re/src/main/java/com/central/common/utils/SpringUtil.java
+2
-0
zlt-commons/zlt-common-core/src/main/resources/META-INF/spring.factories
...-common-core/src/main/resources/META-INF/spring.factories
+2
-1
未找到文件。
zlt-commons/zlt-auth-client-spring-boot-starter/pom.xml
浏览文件 @
8bc688b8
...
...
@@ -42,5 +42,10 @@
<artifactId>
javax.servlet-api
</artifactId>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
org.apache.tomcat.embed
</groupId>
<artifactId>
tomcat-embed-websocket
</artifactId>
<optional>
true
</optional>
</dependency>
</dependencies>
</project>
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/WcAuthConfigurator.java
0 → 100644
浏览文件 @
8bc688b8
package
com.central.oauth2.common.config
;
import
com.central.oauth2.common.util.AuthUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.websocket.server.ServerEndpointConfig
;
/**
* webSocket鉴权配置
*
* @author zlt
* @version 1.0
* @date 2022/5/8
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Slf4j
public
class
WcAuthConfigurator
extends
ServerEndpointConfig
.
Configurator
{
@Override
public
boolean
checkOrigin
(
String
originHeaderValue
)
{
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
try
{
//检查token有效性
AuthUtils
.
checkAccessToken
(
servletRequestAttributes
.
getRequest
());
}
catch
(
Exception
e
)
{
log
.
error
(
"WebSocket-auth-error"
,
e
);
return
false
;
}
return
super
.
checkOrigin
(
originHeaderValue
);
}
}
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/util/AuthUtils.java
浏览文件 @
8bc688b8
...
...
@@ -3,11 +3,15 @@ package com.central.oauth2.common.util;
import
com.central.common.constant.CommonConstant
;
import
com.central.common.constant.SecurityConstants
;
import
com.central.common.model.SysUser
;
import
com.central.common.utils.SpringUtil
;
import
com.central.oauth2.common.token.CustomWebAuthenticationDetails
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.oauth2.common.OAuth2AccessToken
;
import
org.springframework.security.oauth2.common.exceptions.InvalidTokenException
;
import
org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException
;
import
org.springframework.security.oauth2.provider.OAuth2Authentication
;
import
org.springframework.security.oauth2.provider.token.TokenStore
;
import
javax.servlet.http.HttpServletRequest
;
import
java.nio.charset.StandardCharsets
;
...
...
@@ -66,6 +70,29 @@ public class AuthUtils {
return
null
;
}
/**
* 校验accessToken
*/
public
static
void
checkAccessToken
(
HttpServletRequest
request
)
{
String
accessToken
=
extractHeaderToken
(
request
);
checkAccessToken
(
accessToken
);
}
public
static
void
checkAccessToken
(
String
accessTokenValue
)
{
TokenStore
tokenStore
=
SpringUtil
.
getBean
(
TokenStore
.
class
);
OAuth2AccessToken
accessToken
=
tokenStore
.
readAccessToken
(
accessTokenValue
);
if
(
accessToken
==
null
||
accessToken
.
getValue
()
==
null
)
{
throw
new
InvalidTokenException
(
"Invalid access token: "
+
accessTokenValue
);
}
else
if
(
accessToken
.
isExpired
())
{
tokenStore
.
removeAccessToken
(
accessToken
);
throw
new
InvalidTokenException
(
"Access token expired: "
+
accessTokenValue
);
}
OAuth2Authentication
result
=
tokenStore
.
readAuthentication
(
accessToken
);
if
(
result
==
null
)
{
throw
new
InvalidTokenException
(
"Invalid access token: "
+
accessTokenValue
);
}
}
/**
* *从header 请求中的clientId:clientSecret
*/
...
...
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/SpringUtil.java
浏览文件 @
8bc688b8
...
...
@@ -2,6 +2,7 @@ package com.central.common.utils;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.core.env.Environment
;
import
org.springframework.stereotype.Component
;
...
...
@@ -11,6 +12,7 @@ import org.springframework.stereotype.Component;
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Component
@Order
(
0
)
public
class
SpringUtil
implements
ApplicationContextAware
{
private
static
ApplicationContext
applicationContext
=
null
;
...
...
zlt-commons/zlt-common-core/src/main/resources/META-INF/spring.factories
浏览文件 @
8bc688b8
...
...
@@ -3,4 +3,5 @@ com.central.common.config.BannerInitializer
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.common.feign.fallback.UserServiceFallbackFactory,\
com.central.common.lock.LockAspect
\ No newline at end of file
com.central.common.lock.LockAspect,\
com.central.common.utils.SpringUtil
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录