Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
7209ef6d
提交
7209ef6d
authored
11月 13, 2020
作者:
Whispa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commit commit
上级
d35bd969
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
225 行增加
和
89 行删除
+225
-89
AuthController.java
...a/com/example/afrishop_v3/controllers/AuthController.java
+211
-89
PasswordModel.java
...in/java/com/example/afrishop_v3/models/PasswordModel.java
+14
-0
没有找到文件。
src/main/java/com/example/afrishop_v3/controllers/AuthController.java
浏览文件 @
7209ef6d
...
...
@@ -6,10 +6,7 @@ import com.example.afrishop_v3.config.DomainProperties;
import
com.example.afrishop_v3.enums.ResultCodeEnum
;
import
com.example.afrishop_v3.enums.SexEnum
;
import
com.example.afrishop_v3.enums.UserTypeEnum
;
import
com.example.afrishop_v3.models.Network
;
import
com.example.afrishop_v3.models.TbCfCoupon
;
import
com.example.afrishop_v3.models.TbCfToicoupon
;
import
com.example.afrishop_v3.models.TbCfUserInfo
;
import
com.example.afrishop_v3.models.*
;
import
com.example.afrishop_v3.payload.request.LoginRequest
;
import
com.example.afrishop_v3.repository.NetworkRepository
;
import
com.example.afrishop_v3.repository.TbCfCouponRepository
;
...
...
@@ -17,27 +14,27 @@ import com.example.afrishop_v3.repository.TbCfToicouponRepository;
import
com.example.afrishop_v3.repository.UserRepository
;
import
com.example.afrishop_v3.security.jwt.JwtUtils
;
import
com.example.afrishop_v3.security.services.UserDetailsImpl
;
import
com.example.afrishop_v3.util.IdUtil
;
import
com.example.afrishop_v3.util.*
;
import
org.apache.http.Consts
;
import
org.hashids.Hashids
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.authority.AuthorityUtils
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.web.bind.annotation.*
;
import
javax.persistence.EntityManager
;
import
javax.persistence.Query
;
import
java.
text.ParseException
;
import
java.
net.URLDecoder
;
import
java.text.SimpleDateFormat
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
@CrossOrigin
(
origins
=
"*"
,
maxAge
=
3600
)
@RestController
...
...
@@ -46,6 +43,7 @@ public class AuthController extends Controller {
private
final
AuthenticationManager
authenticationManager
;
private
final
UserRepository
userRepository
;
private
final
EmailHelper
emailHelper
;
private
final
TbCfCouponRepository
couponRepository
;
private
final
TbCfToicouponRepository
toicouponRepository
;
private
final
NetworkRepository
networkRepository
;
...
...
@@ -56,10 +54,12 @@ public class AuthController extends Controller {
private
final
DomainProperties
domainProperties
;
private
final
JwtUtils
jwtUtils
;
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
AuthController
.
class
);
public
AuthController
(
AuthenticationManager
authenticationManager
,
UserRepository
userRepository
,
TbCfCouponRepository
couponRepository
,
TbCfToicouponRepository
toicouponRepository
,
NetworkRepository
networkRepository
,
EntityManager
entityManager
,
PasswordEncoder
encoder
,
DomainProperties
domainProperties
,
JwtUtils
jwtUtils
)
{
public
AuthController
(
AuthenticationManager
authenticationManager
,
UserRepository
userRepository
,
EmailHelper
emailHelper
,
TbCfCouponRepository
couponRepository
,
TbCfToicouponRepository
toicouponRepository
,
NetworkRepository
networkRepository
,
EntityManager
entityManager
,
PasswordEncoder
encoder
,
DomainProperties
domainProperties
,
JwtUtils
jwtUtils
)
{
this
.
authenticationManager
=
authenticationManager
;
this
.
userRepository
=
userRepository
;
this
.
emailHelper
=
emailHelper
;
this
.
couponRepository
=
couponRepository
;
this
.
toicouponRepository
=
toicouponRepository
;
this
.
networkRepository
=
networkRepository
;
...
...
@@ -91,7 +91,7 @@ public class AuthController extends Controller {
// byAccount = Optional.of(userInfo);
// } else return notFound;
// } else
return
notFound
;
return
notFound
;
}
Authentication
authentication
;
...
...
@@ -156,8 +156,8 @@ public class AuthController extends Controller {
String
validatePassword
=
validatePassword
(
password
);
if
(
validatePassword
!=
null
)
{
return
new
Result
<>(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
validatePassword
);
if
(
validatePassword
!=
null
)
{
return
new
Result
<>(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
validatePassword
);
}
boolean
byEmail
=
userRepository
.
existsByFirebaseUid
(
email
);
...
...
@@ -200,7 +200,7 @@ public class AuthController extends Controller {
}
private
String
validatePassword
(
String
password
){
private
String
validatePassword
(
String
password
)
{
if
(
password
==
null
||
password
.
isEmpty
())
{
return
"Password is Empty"
;
...
...
@@ -253,8 +253,8 @@ public class AuthController extends Controller {
String
validatePassword
=
validatePassword
(
password
);
if
(
validatePassword
!=
null
)
{
return
new
Result
<>(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
validatePassword
);
if
(
validatePassword
!=
null
)
{
return
new
Result
<>(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
validatePassword
);
}
...
...
@@ -312,117 +312,114 @@ public class AuthController extends Controller {
// Check if firebase token is valid
boolean
isTokenValid
=
user
.
getToken
()
!=
null
&&
validateFirebaseToken
(
user
.
getToken
());
boolean
isTokenValid
=
user
.
getToken
()
!=
null
&&
validateFirebaseToken
(
user
.
getToken
());
String
firebaseUid
=
user
.
getFirebaseUid
();
String
firebaseUid
=
user
.
getFirebaseUid
();
// if valid do sign in if firebase Uid exist in database or register as new user
if
(
isTokenValid
)
{
//Query to find user from database by firebase uid
Optional
<
TbCfUserInfo
>
optional
=
userRepository
.
findByFirebaseUid
(
firebaseUid
);
if
(!
optional
.
isPresent
()
&&
user
.
getEmail
()
!=
null
&&
!
user
.
getEmail
().
isEmpty
()
&&
userRepository
.
existsByAccount
(
user
.
getEmail
()))
{
optional
=
userRepository
.
findFirstByAccount
(
user
.
getEmail
());
}
if
(!
optional
.
isPresent
()
&&
user
.
getPhone
()
!=
null
&&
!
user
.
getPhone
().
isEmpty
()
&&
userRepository
.
existsByAccount
(
user
.
getPhone
().
replace
(
"+"
,
""
)))
{
optional
=
userRepository
.
findFirstByAccount
(
user
.
getPhone
().
replace
(
"+"
,
""
));
}
if
(!
optional
.
isPresent
())
{
// if valid do sign in if firebase Uid exist in database or register as new user
if
(
isTokenValid
)
{
//Query to find user from database by firebase uid
Optional
<
TbCfUserInfo
>
optional
=
userRepository
.
findByFirebaseUid
(
firebaseUid
);
if
(!
optional
.
isPresent
()
&&
user
.
getEmail
()
!=
null
&&
!
user
.
getEmail
().
isEmpty
()
&&
userRepository
.
existsByAccount
(
user
.
getEmail
()))
{
optional
=
userRepository
.
findFirstByAccount
(
user
.
getEmail
());
}
String
userid
=
IdUtil
.
createIdbyUUID
();
if
(!
optional
.
isPresent
()
&&
user
.
getPhone
()
!=
null
&&
!
user
.
getPhone
().
isEmpty
()
&&
userRepository
.
existsByAccount
(
user
.
getPhone
().
replace
(
"+"
,
""
)))
{
optional
=
userRepository
.
findFirstByAccount
(
user
.
getPhone
().
replace
(
"+"
,
""
));
}
// user.setPassword(encoder.encode(firebaseUid));
user
.
setUserId
(
userid
);
if
(!
optional
.
isPresent
())
{
fillUserNecessayInfo
(
user
);
user
=
userRepository
.
save
(
user
);
String
userid
=
IdUtil
.
createIdbyUUID
();
//赠送用户优惠券
fixCoupon
(
user
);
}
else
{
TbCfUserInfo
userInfo
=
optional
.
get
();
// user.setPassword(encoder.encode(firebaseUid));
user
.
setUserId
(
userid
);
if
(
user
.
hasFcm
())
{
userInfo
.
setFcm
(
user
.
getFcm
());
}
fillUserNecessayInfo
(
user
);
user
=
userRepository
.
save
(
user
);
if
(
userInfo
.
getFirebaseUid
()
==
null
)
{
userInfo
.
setFirebaseUid
(
firebaseUid
);
}
String
property
=
domainProperties
.
getProperty
(
"user.avatar"
);
//赠送用户优惠券
fixCoupon
(
user
);
}
else
{
TbCfUserInfo
userInfo
=
optional
.
get
();
property
=
property
==
null
?
""
:
property
;
if
(
user
.
hasFcm
())
{
userInfo
.
setFcm
(
user
.
getFcm
());
}
boolean
b
=
property
.
equals
(
userInfo
.
getAvatar
())
||
userInfo
.
getAvatar
()
==
null
||
userInfo
.
getAvatar
().
isEmpty
();
if
(
userInfo
.
getFirebaseUid
()
==
null
)
{
userInfo
.
setFirebaseUid
(
firebaseUid
);
}
String
property
=
domainProperties
.
getProperty
(
"user.avatar"
);
if
(
user
.
getAvatar
()
!=
null
&&
b
)
{
userInfo
.
setAvatar
(
user
.
getAvatar
());
}
property
=
property
==
null
?
""
:
property
;
boolean
b1
=
userInfo
.
getNick
()
==
null
||
userInfo
.
getNick
().
isEmpty
();
if
(
user
.
getNick
()
!=
null
&&
b1
)
{
userInfo
.
setNick
(
user
.
getNick
());
}
boolean
b
=
property
.
equals
(
userInfo
.
getAvatar
())
||
userInfo
.
getAvatar
()
==
null
||
userInfo
.
getAvatar
().
isEmpty
();
if
(
user
.
getEmail
()
!=
null
)
{
userInfo
.
setEmail
(
user
.
getEmail
());
}
if
(
user
.
getUserType
()
!=
null
)
{
userInfo
.
setUserType
(
user
.
getUserType
());
}
if
(
user
.
getAvatar
()
!=
null
&&
b
)
{
userInfo
.
setAvatar
(
user
.
getAvatar
());
}
if
(
user
.
getPhone
()
!=
null
)
{
userInfo
.
setPhone
(
user
.
getPhone
());
}
boolean
b1
=
userInfo
.
getNick
()
==
null
||
userInfo
.
getNick
().
isEmpty
();
if
(
user
.
getNick
()
!=
null
&&
b1
)
{
userInfo
.
setNick
(
user
.
getNick
());
}
user
=
userInfo
;
if
(
user
.
getEmail
()
!=
null
)
{
userInfo
.
setEmail
(
user
.
getEmail
());
}
}
if
(
user
.
getUserType
()
!=
null
)
{
userInfo
.
setUserType
(
user
.
getUserType
());
}
if
(
user
.
getPhone
()
!=
null
)
{
userInfo
.
setPhone
(
user
.
getPhone
());
}
user
=
userInfo
;
}
fixCode
(
user
);
//addToNetwork
(user);
fixCode
(
user
);
user
.
setLastLoginTime
(
new
Date
()
);
//addToNetwork(user
);
if
(
user
.
getAccount
()
==
null
){
user
.
setAccount
(
user
.
getEmail
()
==
null
?
user
.
getEmail
()
:
user
.
getPhone
());
}
user
.
setLastLoginTime
(
new
Date
());
userRepository
.
save
(
user
);
//注册成功 创建token
if
(
user
.
getAccount
()
==
null
)
{
user
.
setAccount
(
user
.
getEmail
()
==
null
?
user
.
getEmail
()
:
user
.
getPhone
());
}
userRepository
.
save
(
user
);
//注册成功 创建token
Authentication
authentication
;
try
{
authentication
=
new
UsernamePasswordAuthenticationToken
(
UserDetailsImpl
.
build
(
user
),
null
);
}
catch
(
BadCredentialsException
e
)
{
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Invalid username or password"
);
}
SecurityContextHolder
.
getContext
().
setAuthentication
(
authentication
);
String
jwt
=
jwtUtils
.
generateJwtToken
(
authentication
);
Authentication
authentication
;
try
{
authentication
=
new
UsernamePasswordAuthenticationToken
(
UserDetailsImpl
.
build
(
user
),
null
);
}
catch
(
BadCredentialsException
e
)
{
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Invalid username or password"
);
}
user
.
setToken
(
jwt
);
SecurityContextHolder
.
getContext
().
setAuthentication
(
authentication
);
String
jwt
=
jwtUtils
.
generateJwtToken
(
authentication
);
return
new
Result
<>(
user
);
user
.
setToken
(
jwt
);
return
new
Result
<>(
user
);
}
else
{
return
new
Result
<>(
ResultCodeEnum
.
ILLEGAL_ARGUMENT
.
getCode
(),
ResultCodeEnum
.
ILLEGAL_ARGUMENT
.
getDesc
());
}
}
else
{
return
new
Result
<>(
ResultCodeEnum
.
ILLEGAL_ARGUMENT
.
getCode
(),
ResultCodeEnum
.
ILLEGAL_ARGUMENT
.
getDesc
());
}
}
...
...
@@ -519,4 +516,129 @@ public class AuthController extends Controller {
tbCfUserInfoVo
.
setUserType
(
UserTypeEnum
.
UN_KNOW
.
getCode
());
tbCfUserInfoVo
.
setEmailFlag
(
StateConstant
.
INVALID
);
}
@PostMapping
(
"/resetPassword/{account}"
)
public
Result
resetPassword
(
@PathVariable
(
"account"
)
String
account
,
@RequestBody
PasswordModel
model
)
{
Optional
<
TbCfUserInfo
>
firstByAccount
=
userRepository
.
findFirstByAccount
(
account
);
if
(!
firstByAccount
.
isPresent
())
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"User not found !!!"
);
if
(
model
==
null
)
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Empty body"
);
String
code
=
model
.
getCode
();
if
(
code
==
null
||
code
.
isEmpty
())
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Verification Code is required !!"
);
String
password
=
model
.
getPassword
();
if
(
password
==
null
||
password
.
isEmpty
())
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"New password is required !!"
);
String
validatePassword
=
validatePassword
(
password
);
if
(
validatePassword
!=
null
){
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
validatePassword
);
}
TbCfUserInfo
user
=
firstByAccount
.
get
();
if
(
user
.
getVerificationCode
()
!=
null
&&
user
.
getVerificationCode
().
equals
(
code
))
{
user
.
setPassword
(
encoder
.
encode
(
password
));
userRepository
.
save
(
user
);
return
new
Result
(
"Password reset successfully !"
);
}
else
{
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Verification code don't match !"
);
}
}
@GetMapping
(
"/forgotPassword/{account}"
)
public
Result
getUserPhoneCode
(
@PathVariable
(
"account"
)
String
account
)
{
Optional
<
TbCfUserInfo
>
byAccount
=
userRepository
.
findFirstByAccount
(
account
);
if
(!
byAccount
.
isPresent
())
return
new
Result
<>(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Account not found !!!"
);
TbCfUserInfo
user
=
byAccount
.
get
();
if
(
isPhoneValid
(
account
))
return
sendPhoneCode
(
user
,
account
);
if
(
isEmailValid
(
account
))
return
getEmailCode
(
user
,
account
);
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Invalid phone number or email"
);
}
private
Result
getEmailCode
(
TbCfUserInfo
user
,
String
email
)
{
try
{
EmailTemplateBo
emailTemplateBo
=
new
EmailTemplateBo
();
String
name
=
URLDecoder
.
decode
(
email
,
Consts
.
UTF_8
.
name
());
Integer
randomCode
=
RandomCodeHelper
.
producedRandomCode
(
6
);
emailTemplateBo
.
setNick
(
name
);
emailTemplateBo
.
setIdentifyCode
(
randomCode
);
Integer
identifyCode
=
emailHelper
.
sendIdentifyEmail
(
email
,
EmailTemplateConstant
.
REGISTER
,
emailTemplateBo
);
user
.
setVerificationCode
(
String
.
valueOf
(
identifyCode
));
userRepository
.
save
(
user
);
return
new
Result
(
"Verification code has been sent"
);
}
catch
(
Exception
e
)
{
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
e
.
getMessage
());
}
}
private
Result
sendPhoneCode
(
TbCfUserInfo
user
,
String
phone
)
{
Result
result
=
new
Result
();
try
{
phone
=
"+"
+
phone
.
trim
();
Integer
sentCount
=
user
.
getSentCount
();
if
(
sentCount
==
null
)
{
user
.
setSentCount
(
3
);
sentCount
=
3
;
}
Date
date
=
new
Date
();
Date
sentTime
=
user
.
getCodeSentTime
();
long
diff
=
date
.
getTime
()
-
(
sentTime
==
null
?
0
:
sentTime
.
getTime
());
long
diffMinutes
=
diff
/
(
60
*
1000
)
%
60
;
if
(
diffMinutes
>
15
&&
sentCount
>=
3
)
{
sentCount
=
-
1
;
user
.
setSentCount
(
sentCount
);
}
boolean
canSend
=
sentCount
<
3
;
if
(
canSend
)
{
result
.
setMessage
(
"Verification code has been sent"
);
//发送验证码
String
code
=
SMSUtil
.
yzCode
(
phone
);
user
.
setCodeSentTime
(
date
);
user
.
setVerificationCode
(
code
);
user
.
setSentCount
(
user
.
getSentCount
()
+
1
);
userRepository
.
save
(
user
);
logger
.
info
(
"用户["
+
phone
+
"]获取验证码成功,"
+
"验证码:"
+
code
);
}
else
{
//用户频繁操作
result
.
setCode
(
ResultCodeEnum
.
SERVICE_ERROR
.
getCode
());
result
.
setMessage
(
"Frequent operation, please try again after 15 minutes"
);
logger
.
info
(
"用户["
+
phone
+
"]获取验证码失败,操作频繁,15分钟后重试"
);
}
}
catch
(
Exception
e
)
{
result
.
setCode
(
ResultCodeEnum
.
SERVICE_ERROR
.
getCode
());
result
.
setMessage
(
e
.
toString
());
logger
.
info
(
"用户["
+
phone
+
"]获取验证码发生异常--->>>"
+
e
.
toString
());
}
return
result
;
}
}
src/main/java/com/example/afrishop_v3/models/PasswordModel.java
0 → 100644
浏览文件 @
7209ef6d
package
com
.
example
.
afrishop_v3
.
models
;
public
class
PasswordModel
{
private
String
code
;
private
String
password
;
public
String
getPassword
()
{
return
password
;
}
public
String
getCode
()
{
return
code
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论