Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
84a65048
Commit
84a65048
authored
Dec 01, 2019
by
dqjdda
Browse files
修改邮箱与修改密码传输的密码通过rsa加密传输
parent
98f2db21
Changes
3
Hide whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/exception/handler/GlobalExceptionHandler.java
View file @
84a65048
...
@@ -8,6 +8,7 @@ import me.zhengjie.utils.ThrowableUtil;
...
@@ -8,6 +8,7 @@ import me.zhengjie.utils.ThrowableUtil;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.access.AccessDeniedException
;
import
org.springframework.security.access.AccessDeniedException
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
...
@@ -32,6 +33,17 @@ public class GlobalExceptionHandler {
...
@@ -32,6 +33,17 @@ public class GlobalExceptionHandler {
return
buildResponseEntity
(
ApiError
.
error
(
e
.
getMessage
()));
return
buildResponseEntity
(
ApiError
.
error
(
e
.
getMessage
()));
}
}
/**
* BadCredentialsException
*/
@ExceptionHandler
(
BadCredentialsException
.
class
)
public
ResponseEntity
badCredentialsException
(
BadCredentialsException
e
){
// 打印堆栈信息
String
message
=
"坏的凭证"
.
equals
(
e
.
getMessage
())
?
"用户名或密码不正确"
:
e
.
getMessage
();
log
.
error
(
message
);
return
buildResponseEntity
(
ApiError
.
error
(
message
));
}
/**
/**
* 处理自定义异常
* 处理自定义异常
*/
*/
...
...
eladmin-system/src/main/java/me/zhengjie/modules/system/rest/UserController.java
View file @
84a65048
package
me.zhengjie.modules.system.rest
;
package
me.zhengjie.modules.system.rest
;
import
cn.hutool.crypto.asymmetric.KeyType
;
import
cn.hutool.crypto.asymmetric.RSA
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
me.zhengjie.aop.log.Log
;
import
me.zhengjie.aop.log.Log
;
...
@@ -16,12 +18,11 @@ import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
...
@@ -16,12 +18,11 @@ import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
import
me.zhengjie.service.VerificationCodeService
;
import
me.zhengjie.service.VerificationCodeService
;
import
me.zhengjie.utils.*
;
import
me.zhengjie.utils.*
;
import
me.zhengjie.modules.system.service.UserService
;
import
me.zhengjie.modules.system.service.UserService
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.access.prepost.PreAuthorize
;
import
org.springframework.security.access.prepost.PreAuthorize
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.security.core.userdetails.UserDetailsService
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.util.ObjectUtils
;
...
@@ -42,6 +43,8 @@ import java.util.stream.Collectors;
...
@@ -42,6 +43,8 @@ import java.util.stream.Collectors;
@RequestMapping
(
"/api/users"
)
@RequestMapping
(
"/api/users"
)
public
class
UserController
{
public
class
UserController
{
@Value
(
"${rsa.private_key}"
)
private
String
privateKey
;
private
final
PasswordEncoder
passwordEncoder
;
private
final
PasswordEncoder
passwordEncoder
;
private
final
UserService
userService
;
private
final
UserService
userService
;
private
final
DataScope
dataScope
;
private
final
DataScope
dataScope
;
...
@@ -106,6 +109,8 @@ public class UserController {
...
@@ -106,6 +109,8 @@ public class UserController {
@PreAuthorize
(
"@el.check('user:add')"
)
@PreAuthorize
(
"@el.check('user:add')"
)
public
ResponseEntity
create
(
@Validated
@RequestBody
User
resources
){
public
ResponseEntity
create
(
@Validated
@RequestBody
User
resources
){
checkLevel
(
resources
);
checkLevel
(
resources
);
// 默认密码 123456
resources
.
setPassword
(
passwordEncoder
.
encode
(
"123456"
));
return
new
ResponseEntity
<>(
userService
.
create
(
resources
),
HttpStatus
.
CREATED
);
return
new
ResponseEntity
<>(
userService
.
create
(
resources
),
HttpStatus
.
CREATED
);
}
}
...
@@ -150,14 +155,18 @@ public class UserController {
...
@@ -150,14 +155,18 @@ public class UserController {
@ApiOperation
(
"修改密码"
)
@ApiOperation
(
"修改密码"
)
@PostMapping
(
value
=
"/updatePass"
)
@PostMapping
(
value
=
"/updatePass"
)
public
ResponseEntity
updatePass
(
@RequestBody
UserPassVo
passVo
){
public
ResponseEntity
updatePass
(
@RequestBody
UserPassVo
passVo
){
// 密码解密
RSA
rsa
=
new
RSA
(
privateKey
,
null
);
String
oldPass
=
new
String
(
rsa
.
decrypt
(
passVo
.
getOldPass
(),
KeyType
.
PrivateKey
));
String
newPass
=
new
String
(
rsa
.
decrypt
(
passVo
.
getNewPass
(),
KeyType
.
PrivateKey
));
UserDto
user
=
userService
.
findByName
(
SecurityUtils
.
getUsername
());
UserDto
user
=
userService
.
findByName
(
SecurityUtils
.
getUsername
());
if
(!
passwordEncoder
.
matches
(
passVo
.
getO
ldPass
()
,
user
.
getPassword
())){
if
(!
passwordEncoder
.
matches
(
o
ldPass
,
user
.
getPassword
())){
throw
new
BadRequestException
(
"修改失败,旧密码错误"
);
throw
new
BadRequestException
(
"修改失败,旧密码错误"
);
}
}
if
(
passwordEncoder
.
matches
(
passVo
.
getN
ewPass
()
,
user
.
getPassword
())){
if
(
passwordEncoder
.
matches
(
n
ewPass
,
user
.
getPassword
())){
throw
new
BadRequestException
(
"新密码不能与旧密码相同"
);
throw
new
BadRequestException
(
"新密码不能与旧密码相同"
);
}
}
userService
.
updatePass
(
user
.
getUsername
(),
passwordEncoder
.
encode
(
passVo
.
getN
ewPass
()
));
userService
.
updatePass
(
user
.
getUsername
(),
passwordEncoder
.
encode
(
n
ewPass
));
return
new
ResponseEntity
(
HttpStatus
.
OK
);
return
new
ResponseEntity
(
HttpStatus
.
OK
);
}
}
...
@@ -172,8 +181,11 @@ public class UserController {
...
@@ -172,8 +181,11 @@ public class UserController {
@ApiOperation
(
"修改邮箱"
)
@ApiOperation
(
"修改邮箱"
)
@PostMapping
(
value
=
"/updateEmail/{code}"
)
@PostMapping
(
value
=
"/updateEmail/{code}"
)
public
ResponseEntity
updateEmail
(
@PathVariable
String
code
,
@RequestBody
User
user
){
public
ResponseEntity
updateEmail
(
@PathVariable
String
code
,
@RequestBody
User
user
){
// 密码解密
RSA
rsa
=
new
RSA
(
privateKey
,
null
);
String
password
=
new
String
(
rsa
.
decrypt
(
user
.
getPassword
(),
KeyType
.
PrivateKey
));
UserDto
userDto
=
userService
.
findByName
(
SecurityUtils
.
getUsername
());
UserDto
userDto
=
userService
.
findByName
(
SecurityUtils
.
getUsername
());
if
(!
passwordEncoder
.
matches
(
user
.
getP
assword
()
,
userDto
.
getPassword
())){
if
(!
passwordEncoder
.
matches
(
p
assword
,
userDto
.
getPassword
())){
throw
new
BadRequestException
(
"密码错误"
);
throw
new
BadRequestException
(
"密码错误"
);
}
}
VerificationCode
verificationCode
=
new
VerificationCode
(
code
,
ElAdminConstant
.
RESET_MAIL
,
"email"
,
user
.
getEmail
());
VerificationCode
verificationCode
=
new
VerificationCode
(
code
,
ElAdminConstant
.
RESET_MAIL
,
"email"
,
user
.
getEmail
());
...
...
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java
View file @
84a65048
...
@@ -38,11 +38,8 @@ import java.util.stream.Collectors;
...
@@ -38,11 +38,8 @@ import java.util.stream.Collectors;
public
class
UserServiceImpl
implements
UserService
{
public
class
UserServiceImpl
implements
UserService
{
private
final
UserRepository
userRepository
;
private
final
UserRepository
userRepository
;
private
final
UserMapper
userMapper
;
private
final
UserMapper
userMapper
;
private
final
RedisUtils
redisUtils
;
private
final
RedisUtils
redisUtils
;
private
final
UserAvatarRepository
userAvatarRepository
;
private
final
UserAvatarRepository
userAvatarRepository
;
@Value
(
"${file.avatar}"
)
@Value
(
"${file.avatar}"
)
...
@@ -81,17 +78,12 @@ public class UserServiceImpl implements UserService {
...
@@ -81,17 +78,12 @@ public class UserServiceImpl implements UserService {
@CacheEvict
(
allEntries
=
true
)
@CacheEvict
(
allEntries
=
true
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
UserDto
create
(
User
resources
)
{
public
UserDto
create
(
User
resources
)
{
if
(
userRepository
.
findByUsername
(
resources
.
getUsername
())!=
null
){
if
(
userRepository
.
findByUsername
(
resources
.
getUsername
())!=
null
){
throw
new
EntityExistException
(
User
.
class
,
"username"
,
resources
.
getUsername
());
throw
new
EntityExistException
(
User
.
class
,
"username"
,
resources
.
getUsername
());
}
}
if
(
userRepository
.
findByEmail
(
resources
.
getEmail
())!=
null
){
if
(
userRepository
.
findByEmail
(
resources
.
getEmail
())!=
null
){
throw
new
EntityExistException
(
User
.
class
,
"email"
,
resources
.
getEmail
());
throw
new
EntityExistException
(
User
.
class
,
"email"
,
resources
.
getEmail
());
}
}
// 默认密码 123456,此密码是加密后的字符
resources
.
setPassword
(
"e10adc3949ba59abbe56e057f20f883e"
);
return
userMapper
.
toDto
(
userRepository
.
save
(
resources
));
return
userMapper
.
toDto
(
userRepository
.
save
(
resources
));
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment