设置-账户安全模块: 1.换绑邮箱 2.修改密码功能

This commit is contained in:
x ronger 2020-11-17 00:18:54 +08:00
parent 21c50a5521
commit ea8164e7e7
13 changed files with 131 additions and 63 deletions

View File

@ -81,7 +81,7 @@ public class BaseExceptionHandler {
}else {
ModelAndView mv = new ModelAndView();
FastJsonJsonView view = new FastJsonJsonView();
Map<String, Object> attributes = new HashMap();
Map<String, Object> attributes = new HashMap(2);
if (ex instanceof BaseApiException){
attributes.put("code", "401");
attributes.put("message", "用户未登录");
@ -128,7 +128,7 @@ public class BaseExceptionHandler {
private boolean isAjax(HttpServletRequest request) {
String requestedWith = request.getHeader("x-requested-with");
if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
if (requestedWith != null && "XMLHttpRequest".equalsIgnoreCase(requestedWith)) {
return true;
} else {
return false;

View File

@ -9,6 +9,9 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;
/**
* @author ronger
*/
public class BaseSessionManager extends DefaultWebSessionManager {
private static final String AUTHORIZATION = "Authorization";

View File

@ -1,39 +0,0 @@
package com.rymcu.vertical.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author ronger
*/
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.rymcu.vertical.web.api"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Vertical项目接口文档")
.description("Vertical项目相关接口的文档")
.termsOfServiceUrl("http://www.rymcu.com")
.version("1.0")
.build();
}
}

View File

@ -4,7 +4,8 @@ import org.apache.shiro.authc.AuthenticationException;
/**
* 验证码错误异常类
*
*
* @author ronger
*/
public class CaptchaException extends AuthenticationException
{

View File

@ -5,6 +5,7 @@ import com.rymcu.vertical.core.result.ResultCode;
/**
* 服务业务异常如 账号或密码错误 该异常只做INFO级别的日志记录 @see WebMvcConfigurer
* @author ronger
*/
public class ServiceException extends Exception {
private int code;

View File

@ -0,0 +1,17 @@
package com.rymcu.vertical.dto;
import lombok.Data;
/**
* @author ronger
*/
@Data
public class ChangeEmailDTO {
private Integer idUser;
private String email;
private String code;
}

View File

@ -0,0 +1,15 @@
package com.rymcu.vertical.dto;
import lombok.Data;
/**
* @author ronger
*/
@Data
public class UpdatePasswordDTO {
private Integer idUser;
private String password;
}

View File

@ -7,6 +7,8 @@ import com.rymcu.vertical.dto.UserInfoDTO;
import com.rymcu.vertical.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
/**
* @author ronger
*/
@ -120,4 +122,20 @@ public interface UserMapper extends Mapper<User> {
* @return
*/
Integer updateLastLoginTime(@Param("idUser") Integer idUser);
/**
* 更换邮箱
* @param idUser
* @param email
* @return
*/
Integer updateEmail(@Param("idUser") Integer idUser, @Param("email") String email);
/**
* 更新密码
* @param idUser
* @param password
* @return
*/
Integer updatePasswordById(@Param("idUser") Integer idUser, @Param("password") String password);
}

View File

@ -1,9 +1,7 @@
package com.rymcu.vertical.service;
import com.rymcu.vertical.core.service.Service;
import com.rymcu.vertical.dto.Author;
import com.rymcu.vertical.dto.UserDTO;
import com.rymcu.vertical.dto.UserInfoDTO;
import com.rymcu.vertical.dto.*;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.entity.UserExtend;
import org.apache.ibatis.exceptions.TooManyResultsException;
@ -123,4 +121,18 @@ public interface UserService extends Service<User> {
* @return
*/
UserExtend selectUserExtendByNickname(String nickname);
/**
* 更换邮箱
* @param changeEmailDTO
* @return
*/
Map updateEmail(ChangeEmailDTO changeEmailDTO);
/**
* 更新密码
* @param updatePasswordDTO
* @return
*/
Map updatePassword(UpdatePasswordDTO updatePasswordDTO);
}

View File

@ -2,10 +2,7 @@ package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.core.service.AbstractService;
import com.rymcu.vertical.core.service.redis.RedisService;
import com.rymcu.vertical.dto.Author;
import com.rymcu.vertical.dto.TokenUser;
import com.rymcu.vertical.dto.UserDTO;
import com.rymcu.vertical.dto.UserInfoDTO;
import com.rymcu.vertical.dto.*;
import com.rymcu.vertical.entity.Role;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.entity.UserExtend;
@ -47,7 +44,8 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
@Resource
private UserExtendMapper userExtendMapper;
private final static String avatarSvgType = "1";
private final static String AVATAR_SVG_TYPE = "1";
private final static String DEFAULT_AVATAR = "https://static.rymcu.com/article/1578475481946.png";
@Override
public User findByAccount(String account) throws TooManyResultsException{
@ -59,22 +57,23 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
public Map register(String email, String password, String code) {
Map map = new HashMap(2);
map.put("message","验证码无效!");
String vcode = redisService.get(email);
if(StringUtils.isNotBlank(vcode)){
if(vcode.equals(code)){
String vCode = redisService.get(email);
if(StringUtils.isNotBlank(vCode)){
if(vCode.equals(code)){
User user = userMapper.findByAccount(email);
if(user != null){
map.put("message","该邮箱已被注册!");
} else {
user = new User();
user.setAccount(email);
String nickname = email.split("@")[0];
nickname = checkNickname(nickname);
user.setNickname(nickname);
user.setAccount(nickname);
user.setEmail(email);
user.setPassword(Utils.entryptPassword(password));
user.setCreatedTime(new Date());
user.setUpdatedTime(user.getCreatedTime());
user.setAvatarUrl(DEFAULT_AVATAR);
userMapper.insertSelective(user);
user = userMapper.findByAccount(email);
Role role = roleMapper.selectRoleByInputCode("user");
@ -100,9 +99,7 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
@Override
public Map login(String account, String password) {
Map map = new HashMap(1);
User user = new User();
user.setAccount(account);
user = userMapper.selectOne(user);
User user = userMapper.findByAccount(account);
if(user != null){
if(Utils.comparePwd(password, user.getPassword())){
userMapper.updateLastLoginTime(user.getIdUser());
@ -191,7 +188,7 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
map.put("message", "该昵称已使用!");
return map;
}
if (StringUtils.isNotBlank(user.getAvatarType()) && avatarSvgType.equals(user.getAvatarType())) {
if (StringUtils.isNotBlank(user.getAvatarType()) && AVATAR_SVG_TYPE.equals(user.getAvatarType())) {
String avatarUrl = UploadController.uploadBase64File(user.getAvatarUrl(), 0);
user.setAvatarUrl(avatarUrl);
user.setAvatarType("0");
@ -242,4 +239,31 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
public UserExtend selectUserExtendByNickname(String nickname) {
return userExtendMapper.selectUserExtendByNickname(nickname);
}
@Override
public Map updateEmail(ChangeEmailDTO changeEmailDTO) {
Map map = new HashMap(2);
map.put("message","验证码无效!");
Integer idUser = changeEmailDTO.getIdUser();
String email = changeEmailDTO.getEmail();
String code = changeEmailDTO.getCode();
String vCode = redisService.get(email);
if(StringUtils.isNotBlank(vCode)){
if(vCode.equals(code)){
userMapper.updateEmail(idUser, email);
map.put("message","更新成功!");
map.put("email", email);
}
}
return map;
}
@Override
public Map updatePassword(UpdatePasswordDTO updatePasswordDTO) {
Map map = new HashMap(1);
String password = Utils.entryptPassword(updatePasswordDTO.getPassword());
userMapper.updatePasswordById(updatePasswordDTO.getIdUser(), password);
map.put("message", "更新成功!");
return map;
}
}

View File

@ -14,7 +14,6 @@ import com.rymcu.vertical.service.PortfolioService;
import com.rymcu.vertical.service.UserService;
import com.rymcu.vertical.util.UserUtils;
import com.rymcu.vertical.util.Utils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@ -39,7 +38,6 @@ public class CommonApiController {
@Resource
private PortfolioService portfolioService;
@ApiOperation(value = "获取邮件验证码")
@GetMapping("/get-email-code")
public GlobalResult<Map<String, String>> getEmailCode(@RequestParam("email") String email) throws MessagingException {
Map<String, String> map = new HashMap<>(1);
@ -56,7 +54,6 @@ public class CommonApiController {
return GlobalResultGenerator.genSuccessResult(map);
}
@ApiOperation(value = "获取找回密码邮件")
@GetMapping("/get-forget-password-email")
public GlobalResult<Map<Object, Object>> getForgetPasswordEmail(@RequestParam("email") String email) throws MessagingException {
Map<Object, Object> map = new HashMap<>(1);

View File

@ -2,7 +2,8 @@ package com.rymcu.vertical.web.api.user;
import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.dto.UserInfoDTO;
import com.rymcu.vertical.dto.*;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.entity.UserExtend;
import com.rymcu.vertical.service.UserService;
import org.springframework.web.bind.annotation.*;
@ -44,4 +45,16 @@ public class UserInfoController {
return GlobalResultGenerator.genSuccessResult(map);
}
@PatchMapping("/update-email")
public GlobalResult updateEmail(@RequestBody ChangeEmailDTO changeEmailDTO) {
Map map = userService.updateEmail(changeEmailDTO);
return GlobalResultGenerator.genSuccessResult(map);
}
@PatchMapping("/update-password")
public GlobalResult updatePassword(@RequestBody UpdatePasswordDTO updatePasswordDTO) {
Map map = userService.updatePassword(updatePasswordDTO);
return GlobalResultGenerator.genSuccessResult(map);
}
}

View File

@ -68,9 +68,15 @@
<update id="updateLastLoginTime">
update vertical_user set last_login_time = sysdate() where id = #{idUser}
</update>
<update id="updateEmail">
update vertical_user set email = #{email} where id = #{idUser}
</update>
<update id="updatePasswordById">
update vertical_user set password = #{password} where id = #{idUser}
</update>
<select id="findByAccount" resultMap="BaseResultMap">
select id, nickname, account, password, status from vertical_user where account = #{account} and status = 0
select id, nickname, account, password, status, avatar_type, avatar_url from vertical_user where (account = #{account} or email = #{account} ) and status = 0
</select>
<select id="findUserInfoByAccount" resultMap="UserInfoResultMapper">
select id, nickname, sex, avatar_type, avatar_url, email, phone, account, status, signature, last_login_time from vertical_user where account = #{account}