Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
iBiz4j Spring R7
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
iBiz-R7后台标准模板
iBiz4j Spring R7
提交
d6893424
提交
d6893424
编写于
2月 05, 2021
作者:
zhouweidong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
oauth2
上级
e5ddce25
变更
2
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
191 行增加
和
0 行删除
+191
-0
pom.xml.ftl
SLN/%PUBPRJ%-dependencies/pom.xml.ftl
+26
-0
OAuth2ResourceServer.java.ftl
...%SYS_PKGPATH%/util/security/OAuth2ResourceServer.java.ftl
+165
-0
未找到文件。
SLN/%PUBPRJ%-dependencies/pom.xml.ftl
浏览文件 @
d6893424
...
...
@@ -24,6 +24,19 @@ TARGET=PSSYSTEM
</#list>
</#if>
</#list>
<#assign oauth2Enable=false>
<#comment>服务接口微服务平台配置</#comment>
<#if sys.getAllPSDevSlnMSDepAPIs()??>
<#list sys.getAllPSDevSlnMSDepAPIs() as depSysApi>
<#if depSysApi.getPSDCMSPlatform()?? >
<#assign sysApiPlatform=depSysApi.getPSDCMSPlatform()>
<#if sysApiPlatform.getUserParam("ibiz.oauth2.enable","")?? && sysApiPlatform.getUserParam("ibiz.oauth2.enable","")!="">
<#assign oauth2Enable=true>
<#break >
</#if>
</#if>
</#list>
</#if>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
...
...
@@ -458,6 +471,19 @@ TARGET=PSSYSTEM
<version>${r'${rocketmq.version}'}</version>
</dependency>
</#if>
<#if oauth2Enable>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.0.10.RELEASE</version>
</dependency>
</#if>
<#comment>引用组件包</#comment>
<#if pub.getPSSysSFPubPkgs?? && pub.getPSSysSFPubPkgs()??>
...
...
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/security/OAuth2ResourceServer.java.ftl
0 → 100644
浏览文件 @
d6893424
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
<#
assign
oauth2Enable
=
false
>
<#
if
sysrun
??
&&
sysrun
.
getPSDevSlnMSDepAPI
()??
>
<#
assign
depSysApi
=
sysrun
.
getPSDevSlnMSDepAPI
()>
<#
if
depSysApi
.
getPSDCMSPlatformNode
()??>
<#
assign
depSysApiPlatformNode
=
depSysApi
.
getPSDCMSPlatformNode
()>
<#
assign
depSysApiPlatform
=
depSysApi
.
getPSDCMSPlatform
()>
<#
if
depSysApiPlatform
.
getUserParam
(
"ibiz.oauth2.enable"
,
""
)??
&&
depSysApiPlatform
.
getUserParam
(
"ibiz.oauth2.enable"
,
""
)
!="">
<#
assign
oauth2Enable
=
true
>
</#
if
>
</#
if
>
</#
if
>
<#
if
oauth2Enable
>
package
${
pub
.
getPKGCodeName
()}.
core
.
util
;
import
lombok
.
extern
.
slf4j
.
Slf4j
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Value
;
import
org
.
springframework
.
context
.
annotation
.
Bean
;
import
org
.
springframework
.
context
.
annotation
.
Configuration
;
import
org
.
springframework
.
security
.
config
.
annotation
.
web
.
builders
.
HttpSecurity
;
import
org
.
springframework
.
security
.
core
.
Authentication
;
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
;
import
org
.
springframework
.
security
.
oauth2
.
provider
.
authentication
.
OAuth2AuthenticationDetails
;
import
org
.
springframework
.
security
.
oauth2
.
provider
.
authentication
.
TokenExtractor
;
import
org
.
springframework
.
security
.
oauth2
.
provider
.
token
.
TokenStore
;
import
org
.
springframework
.
security
.
oauth2
.
provider
.
token
.
store
.
JwtAccessTokenConverter
;
import
org
.
springframework
.
security
.
oauth2
.
provider
.
token
.
store
.
JwtTokenStore
;
import
org
.
springframework
.
security
.
web
.
authentication
.
preauth
.
PreAuthenticatedAuthenticationToken
;
import
org
.
springframework
.
security
.
web
.
util
.
matcher
.
RequestMatcher
;
import
org
.
springframework
.
util
.
StringUtils
;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
java
.
util
.
Enumeration
;
/**
*
oauth
资源服务器配置
*/
@
Configuration
@
EnableResourceServer
@
Slf4j
public
class
OAuth2ResourceServer
extends
ResourceServerConfigurerAdapter
{
@
Value
(
"${r'${ibiz.jwt.oauth2.header:oauth2authorization}'}"
)
private
String
authorization
;
@
Value
(
"${r'${ibiz.jwt.oauth2.signkey:oauth2}'}"
)
private
String
SignKey
;
/**
*
token
存储、解析,校验
*
@
param
resources
*/
@
Override
public
void
configure
(
ResourceServerSecurityConfigurer
resources
)
{
resources
.
tokenExtractor
(
new
OAuth2TokenExtractor
());
resources
.
tokenStore
(
getTokenStore
()).
stateless
(
true
);
}
/**
*
校验策略,只校验
oauth2
颁发的
token
(特定请求头)
*
@
param
http
*
@
throws
Exception
*/
@
Override
public
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
.
requestMatcher
(
new
OAuth2RequestMatcher
());
http
.
authorizeRequests
().
anyRequest
().
authenticated
();
}
/**
*
token
存储器
*
@
return
*/
@
Bean
public
TokenStore
getTokenStore
()
{
return
new
JwtTokenStore
(
accessTokenConverter
());
}
/**
*
jwt
密签
*
@
return
*/
@
Bean
public
JwtAccessTokenConverter
accessTokenConverter
()
{
final
JwtAccessTokenConverter
converter
=
new
JwtAccessTokenConverter
();
converter
.
setSigningKey
(
SignKey
);
return
converter
;
}
/**
*
匹配特定
oauth2
请求头
token
,其余请求交由
spring
security
处理
*/
public
class
OAuth2RequestMatcher
implements
RequestMatcher
{
@
Override
public
boolean
matches
(
HttpServletRequest
request
)
{
String
header
=
request
.
getHeader
(
authorization
);
if
(
!StringUtils.isEmpty(header)) {
return
true
;
}
else
{
return
false
;
}
}
}
/**
*
token
请求头处理
*/
public
class
OAuth2TokenExtractor
implements
TokenExtractor
{
@
Override
public
Authentication
extract
(
HttpServletRequest
request
)
{
String
tokenValue
=
this
.
extractToken
(
request
);
if
(
tokenValue
!= null) {
PreAuthenticatedAuthenticationToken
authentication
=
new
PreAuthenticatedAuthenticationToken
(
tokenValue
,
""
);
return
authentication
;
}
else
{
return
null
;
}
}
protected
String
extractToken
(
HttpServletRequest
request
)
{
String
token
=
this
.
extractHeaderToken
(
request
);
if
(
token
==
null
)
{
log
.
debug
(
"Token not found in headers. Trying request parameters."
);
token
=
request
.
getParameter
(
"access_token"
);
if
(
token
==
null
)
{
log
.
debug
(
"Token not found in request parameters. Not an OAuth2 request."
);
}
else
{
request
.
setAttribute
(
OAuth2AuthenticationDetails
.
ACCESS_TOKEN_TYPE
,
"Bearer"
);
}
}
return
token
;
}
protected
String
extractHeaderToken
(
HttpServletRequest
request
)
{
Enumeration
headers
=
request
.
getHeaders
(
authorization
);
String
value
;
do
{
if
(
!headers.hasMoreElements()) {
return
null
;
}
value
=
(
String
)
headers
.
nextElement
();
}
while
(
!value.toLowerCase().startsWith("Bearer".toLowerCase()));
String
authHeaderValue
=
value
.
substring
(
"Bearer"
.
length
()).
trim
();
request
.
setAttribute
(
OAuth2AuthenticationDetails
.
ACCESS_TOKEN_TYPE
,
value
.
substring
(
0
,
"Bearer"
.
length
()).
trim
());
int
commaIndex
=
authHeaderValue
.
indexOf
(
44
);
if
(
commaIndex
>
0
)
{
authHeaderValue
=
authHeaderValue
.
substring
(
0
,
commaIndex
);
}
return
authHeaderValue
;
}
}
}
</#
if
>
\ No newline at end of file
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录