Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
3aa5ac43
提交
3aa5ac43
authored
3月 09, 2021
作者:
吴德鹏
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化
上级
7295a930
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
76 行增加
和
55 行删除
+76
-55
CartController.java
...a/com/example/afrishop_v3/controllers/CartController.java
+4
-1
OrderController.java
.../com/example/afrishop_v3/controllers/OrderController.java
+9
-0
AuthTokenFilter.java
...com/example/afrishop_v3/security/jwt/AuthTokenFilter.java
+3
-0
JwtUtils.java
...n/java/com/example/afrishop_v3/security/jwt/JwtUtils.java
+57
-42
AuthenticationUser.java
...ple/afrishop_v3/security/services/AuthenticationUser.java
+3
-12
没有找到文件。
src/main/java/com/example/afrishop_v3/controllers/CartController.java
浏览文件 @
3aa5ac43
...
...
@@ -16,6 +16,7 @@ import com.example.afrishop_v3.util.ValidateUtils;
import
com.example.afrishop_v3.util.WordposHelper
;
import
net.sf.json.JSONArray
;
import
net.sf.json.JSONObject
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.annotation.*
;
...
...
@@ -66,12 +67,14 @@ public class CartController extends Controller {
}
//Add single item to the cart
// @Async
@PostMapping
public
Result
addToCart
(
@RequestBody
TbCfCartRecordR
itemDetail
)
{
TbCfUserInfo
user
=
this
.
user
.
user
();
if
(
user
==
null
)
{
return
new
Result
(
ResultCodeEnum
.
UN_LOGIN
.
getCode
(),
"need login"
);
}
String
userId
=
user
.
getUserId
();
if
(
itemDetail
==
null
)
{
...
...
src/main/java/com/example/afrishop_v3/controllers/OrderController.java
浏览文件 @
3aa5ac43
...
...
@@ -278,6 +278,9 @@ public class OrderController extends Controller {
@RequestParam
(
value
=
"open"
,
required
=
false
)
boolean
open
)
throws
ParseException
{
TbCfUserInfo
user
=
this
.
user
.
user
();
if
(
user
==
null
)
{
return
new
Result
(
ResultCodeEnum
.
UN_LOGIN
.
getCode
(),
"need login"
);
}
String
userId
=
user
.
getUserId
();
int
v_code
=
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
();
...
...
@@ -490,6 +493,9 @@ public class OrderController extends Controller {
@RequestParam
(
value
=
"name"
,
required
=
false
)
String
name
)
{
TbCfUserInfo
user
=
this
.
user
.
user
();
if
(
user
==
null
)
{
return
new
Result
(
ResultCodeEnum
.
UN_LOGIN
.
getCode
(),
"need login"
);
}
Page
<
OrderCount
>
list
;
PageRequest
of
=
PageRequest
.
of
(
pageNum
,
pageSize
,
sort
(
sort
));
String
userId
=
user
.
getUserId
();
...
...
@@ -565,6 +571,9 @@ public class OrderController extends Controller {
logger
.
info
(
"上传files:"
,
comment
);
TbCfUserInfo
user
=
this
.
user
.
user
();
if
(
user
==
null
)
{
return
new
Result
(
ResultCodeEnum
.
UN_LOGIN
.
getCode
(),
"need login"
);
}
String
userId
=
user
.
getUserId
();
...
...
src/main/java/com/example/afrishop_v3/security/jwt/AuthTokenFilter.java
浏览文件 @
3aa5ac43
...
...
@@ -42,6 +42,9 @@ 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
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"Cannot set user authentication: {}"
,
e
);
...
...
src/main/java/com/example/afrishop_v3/security/jwt/JwtUtils.java
浏览文件 @
3aa5ac43
...
...
@@ -2,10 +2,14 @@ package com.example.afrishop_v3.security.jwt;
import
java.util.Date
;
import
com.example.afrishop_v3.models.TbCfUserInfo
;
import
com.example.afrishop_v3.security.services.UserDetailsImpl
;
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.AuthenticationManager
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.stereotype.Component
;
...
...
@@ -13,46 +17,57 @@ import io.jsonwebtoken.*;
@Component
public
class
JwtUtils
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
JwtUtils
.
class
);
@Value
(
"${bezkoder.app.jwtSecret}"
)
private
String
jwtSecret
;
@Value
(
"${bezkoder.app.jwtExpirationMs}"
)
private
int
jwtExpirationMs
;
public
String
generateJwtToken
(
Authentication
authentication
)
{
UserDetailsImpl
userPrincipal
=
(
UserDetailsImpl
)
authentication
.
getPrincipal
();
return
Jwts
.
builder
()
.
setSubject
((
userPrincipal
.
getId
()))
.
setIssuedAt
(
new
Date
())
.
setExpiration
(
new
Date
((
new
Date
()).
getTime
()
+
(
jwtExpirationMs
*
365
)))
.
signWith
(
SignatureAlgorithm
.
HS512
,
jwtSecret
)
.
compact
();
}
public
String
getUserNameFromJwtToken
(
String
token
)
{
return
Jwts
.
parser
().
setSigningKey
(
jwtSecret
).
parseClaimsJws
(
token
).
getBody
().
getSubject
();
}
public
boolean
validateJwtToken
(
String
authToken
)
{
try
{
Jwts
.
parser
().
setSigningKey
(
jwtSecret
).
parseClaimsJws
(
authToken
);
return
true
;
}
catch
(
SignatureException
e
)
{
logger
.
error
(
"Invalid JWT signature: {}"
,
e
.
getMessage
());
}
catch
(
MalformedJwtException
e
)
{
logger
.
error
(
"Invalid JWT token: {}"
,
e
.
getMessage
());
}
catch
(
ExpiredJwtException
e
)
{
logger
.
error
(
"JWT token is expired: {}"
,
e
.
getMessage
());
}
catch
(
UnsupportedJwtException
e
)
{
logger
.
error
(
"JWT token is unsupported: {}"
,
e
.
getMessage
());
}
catch
(
IllegalArgumentException
e
)
{
logger
.
error
(
"JWT claims string is empty: {}"
,
e
.
getMessage
());
}
return
false
;
}
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
JwtUtils
.
class
);
@Value
(
"${bezkoder.app.jwtSecret}"
)
private
String
jwtSecret
;
@Value
(
"${bezkoder.app.jwtExpirationMs}"
)
private
int
jwtExpirationMs
;
@Autowired
private
AuthenticationManager
authenticationManager
;
public
String
generateJwtToken
(
Authentication
authentication
)
{
UserDetailsImpl
userPrincipal
=
(
UserDetailsImpl
)
authentication
.
getPrincipal
();
//token设置过期时间为1年
return
Jwts
.
builder
()
.
setSubject
((
userPrincipal
.
getId
()))
.
setIssuedAt
(
new
Date
())
.
setExpiration
(
new
Date
(
new
Date
().
getTime
()
+
3600
l
*
1000
*
24
*
365
))
.
signWith
(
SignatureAlgorithm
.
HS512
,
jwtSecret
)
.
compact
();
}
public
String
refreshToken
(
TbCfUserInfo
userInfo
)
{
Authentication
authentication
=
authenticationManager
.
authenticate
(
new
UsernamePasswordAuthenticationToken
(
userInfo
.
getUserId
(),
userInfo
.
getPassword
()));
String
jwt
=
this
.
generateJwtToken
(
authentication
);
return
jwt
;
}
public
String
getUserNameFromJwtToken
(
String
token
)
{
return
Jwts
.
parser
().
setSigningKey
(
jwtSecret
).
parseClaimsJws
(
token
).
getBody
().
getSubject
();
}
public
boolean
validateJwtToken
(
String
authToken
)
{
try
{
Jwts
.
parser
().
setSigningKey
(
jwtSecret
).
parseClaimsJws
(
authToken
);
return
true
;
}
catch
(
SignatureException
e
)
{
logger
.
error
(
"Invalid JWT signature: {}"
,
e
.
getMessage
());
}
catch
(
MalformedJwtException
e
)
{
logger
.
error
(
"Invalid JWT token: {}"
,
e
.
getMessage
());
}
catch
(
ExpiredJwtException
e
)
{
logger
.
error
(
"JWT token is expired: {}"
,
e
.
getMessage
());
}
catch
(
UnsupportedJwtException
e
)
{
logger
.
error
(
"JWT token is unsupported: {}"
,
e
.
getMessage
());
}
catch
(
IllegalArgumentException
e
)
{
logger
.
error
(
"JWT claims string is empty: {}"
,
e
.
getMessage
());
}
return
false
;
}
}
src/main/java/com/example/afrishop_v3/security/services/AuthenticationUser.java
浏览文件 @
3aa5ac43
...
...
@@ -26,24 +26,15 @@ public class AuthenticationUser implements IAuthenticationFacade {
public
TbCfUserInfo
user
()
{
String
name
=
getAuthentication
().
getName
();
// UserDetailsImpl principal = (UserDetailsImpl) getAuthentication().getPrincipal();
//
// System.out.println("principal");
// System.out.println(principal);
//
//// logger.info("登录的用户:" + name);
// System.out.println("name");
// System.out.println(name);
Optional
<
TbCfUserInfo
>
user
=
repository
.
findById
(
name
);
// logger.info("用户:" + user);
return
user
.
orElse
(
null
);
}
public
String
userId
()
{
TbCfUserInfo
user
=
user
(
);
if
(
user
!=
null
)
{
return
user
().
getUserId
();
Optional
<
TbCfUserInfo
>
userOptional
=
Optional
.
ofNullable
(
user
()
);
if
(
user
Optional
.
isPresent
()
)
{
return
user
Optional
.
get
().
getUserId
();
}
return
null
;
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论