Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
f271e5a1
提交
f271e5a1
authored
3月 09, 2021
作者:
吴德鹏
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化权限,设置token失效时间为1年
上级
3661ae7f
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
43 行增加
和
6 行删除
+43
-6
AuthController.java
...a/com/example/afrishop_v3/controllers/AuthController.java
+2
-0
TbCfUserInfo.java
...ain/java/com/example/afrishop_v3/models/TbCfUserInfo.java
+1
-0
WebSecurityConfig.java
...a/com/example/afrishop_v3/security/WebSecurityConfig.java
+1
-1
AuthTokenFilter.java
...com/example/afrishop_v3/security/jwt/AuthTokenFilter.java
+18
-3
JwtUtils.java
...n/java/com/example/afrishop_v3/security/jwt/JwtUtils.java
+21
-2
没有找到文件。
src/main/java/com/example/afrishop_v3/controllers/AuthController.java
浏览文件 @
f271e5a1
...
...
@@ -191,6 +191,7 @@ public class AuthController extends Controller {
Authentication
authentication
;
try
{
authentication
=
authenticationManager
.
authenticate
(
new
UsernamePasswordAuthenticationToken
(
userInfo
.
getUserId
(),
loginRequest
.
getPassword
()));
}
catch
(
BadCredentialsException
e
)
{
...
...
@@ -226,6 +227,7 @@ public class AuthController extends Controller {
return
new
Result
<>(
userInfo
);
}
//Verify sent user code
@GetMapping
(
"/verifyCode/{account}/{code}"
)
public
Result
<
Boolean
>
validateCode
(
@PathVariable
(
"account"
)
String
account
,
...
...
src/main/java/com/example/afrishop_v3/models/TbCfUserInfo.java
浏览文件 @
f271e5a1
...
...
@@ -65,6 +65,7 @@ public class TbCfUserInfo {
/**
* 密码
*/
@JsonIgnore
private
String
password
;
/**
* 上一次登录时间
...
...
src/main/java/com/example/afrishop_v3/security/WebSecurityConfig.java
浏览文件 @
f271e5a1
...
...
@@ -71,7 +71,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
"/goodsType/**"
,
"/home/**"
,
"/spider/**"
,
"/store/**"
,
"/shopify/**"
,
"/community/**"
,
"/version/**"
,
"/flutterwave/notify/**"
,
"/dpo/notify/**"
,
"/advertisement/**"
,
"/website/**"
,
"/paypal/**"
,
"/discover/bonus/**"
,
"/problem/**"
,
"/cube/**"
,
"/activity/**"
,
"/attributes/**"
,
"/stripe/**"
,
"/coupon/**"
,
"/logistics/freeShippingThreshold"
,
"/visit/**"
,
"/search/**"
,
"/country/**"
,
"/term/**"
,
"/
order/**"
,
"/
api/upload"
,
"/cart/**"
,
"/address/**"
).
permitAll
()
"/visit/**"
,
"/search/**"
,
"/country/**"
,
"/term/**"
,
"/api/upload"
,
"/cart/**"
,
"/address/**"
).
permitAll
()
.
antMatchers
(
"/api/test/**"
).
permitAll
()
.
anyRequest
().
authenticated
();
...
...
src/main/java/com/example/afrishop_v3/security/jwt/AuthTokenFilter.java
浏览文件 @
f271e5a1
package
com
.
example
.
afrishop_v3
.
security
.
jwt
;
import
java.io.IOException
;
import
java.util.Date
;
import
javax.crypto.SecretKey
;
import
javax.servlet.FilterChain
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
com.example.afrishop_v3.security.services.UserDetailsServiceImpl
;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.JwtBuilder
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.SignatureAlgorithm
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.core.userdetails.UserDetails
;
...
...
@@ -23,6 +30,9 @@ public class AuthTokenFilter extends OncePerRequestFilter {
@Autowired
private
JwtUtils
jwtUtils
;
@Value
(
"${bezkoder.app.jwtSecret}"
)
private
String
jwtSecret
;
@Autowired
private
UserDetailsServiceImpl
userDetailsService
;
...
...
@@ -42,9 +52,12 @@ public class AuthTokenFilter extends OncePerRequestFilter {
authentication
.
setDetails
(
new
WebAuthenticationDetailsSource
().
buildDetails
(
request
));
SecurityContextHolder
.
getContext
().
setAuthentication
(
authentication
);
String
token
=
jwtUtils
.
generateJwtToken
(
authentication
);
logger
.
info
(
"token:==============="
+
token
);
response
.
setHeader
(
"Authorization"
,
"Bearer "
+
token
);
// String token = jwtUtils.generateJwtToken(authentication);
// logger.info("token:==============="+token);
Date
expire
=
jwtUtils
.
getExpirationDateFromToken
(
jwt
);
logger
.
info
(
"token过期时间:==============="
+
expire
);
// response.setHeader("Authorization","Bearer "+token);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"Cannot set user authentication: {}"
,
e
);
...
...
@@ -62,4 +75,6 @@ public class AuthTokenFilter extends OncePerRequestFilter {
return
null
;
}
}
src/main/java/com/example/afrishop_v3/security/jwt/JwtUtils.java
浏览文件 @
f271e5a1
...
...
@@ -15,6 +15,8 @@ import org.springframework.stereotype.Component;
import
io.jsonwebtoken.*
;
import
javax.crypto.SecretKey
;
@Component
public
class
JwtUtils
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
JwtUtils
.
class
);
...
...
@@ -32,11 +34,11 @@ public class JwtUtils {
UserDetailsImpl
userPrincipal
=
(
UserDetailsImpl
)
authentication
.
getPrincipal
();
//token设置过期时间为1年
//token设置过期时间为1年
3600l * 1000 * 24 * 365
return
Jwts
.
builder
()
.
setSubject
((
userPrincipal
.
getId
()))
.
setIssuedAt
(
new
Date
())
.
setExpiration
(
new
Date
(
new
Date
().
getTime
()
+
3600
l
*
1000
*
24
*
365
))
.
setExpiration
(
new
Date
(
new
Date
().
getTime
()
+
3600
l
*
1000
*
24
*
365
))
.
signWith
(
SignatureAlgorithm
.
HS512
,
jwtSecret
)
.
compact
();
}
...
...
@@ -70,4 +72,21 @@ public class JwtUtils {
return
false
;
}
/**
* 获取jwt失效时间
*/
public
Date
getExpirationDateFromToken
(
String
token
)
{
return
getClaimFromToken
(
token
).
getExpiration
();
}
/**
* 获取jwt的payload部分
*/
public
Claims
getClaimFromToken
(
String
token
)
{
return
Jwts
.
parser
()
//得到DefaultJwtParser
.
setSigningKey
(
jwtSecret
)
//设置签名的秘钥
.
parseClaimsJws
(
token
.
replace
(
"jwt_"
,
""
))
.
getBody
();
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论