实现忘记密码功能,以及完善部分单元测试
This commit is contained in:
@@ -16,6 +16,7 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@@ -133,9 +134,42 @@ public class DataController {
|
||||
return userService.addUserQue(userque);
|
||||
}
|
||||
|
||||
/*@GetMapping("/finduserque")
|
||||
public TbUserque findUserQue(){
|
||||
TbUserque userque = userService;
|
||||
/**
|
||||
* 判断密保问题是否正确,正确返回true,错误返回false,其余反馈异常对象
|
||||
* @param name
|
||||
* @param question
|
||||
* @param answer
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@GetMapping("/finduserque")
|
||||
public boolean findUserQue(@RequestParam(value = "name")String name
|
||||
,@RequestParam(value = "question")String question
|
||||
,@RequestParam(value = "answer")String answer)throws Exception{
|
||||
Optional<VUserque> userque = userService.findUserQueByName(name);
|
||||
if (!userque.isPresent()){
|
||||
throw new UserException(ResultEnum.EMPTY_QUESTION);
|
||||
}else if(question.equals(userque.get().getQuestion())){
|
||||
if(new BigInteger(CodingUtil.encryptSHA(answer.getBytes())).toString(32).equals(userque.get().getAnswer()))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}else{
|
||||
throw new UserException(ResultEnum.QUESTION_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
}*/
|
||||
@PostMapping("/resetPass")
|
||||
public boolean resetPass(@RequestParam(value = "name") String name
|
||||
, @RequestParam(value = "password") String password,
|
||||
@RequestParam(value = "question") String question
|
||||
, @RequestParam(value = "answer") String answer) throws Exception {
|
||||
log.info("name:{}, password:{}, question:{}, answer:{}",name,password,question,answer);
|
||||
if (findUserQue(name,question,answer)){
|
||||
//service方法内含有对密码加密的操作
|
||||
return userService.updateColpasswordByColname(password,name);
|
||||
}else {
|
||||
throw new UserException(ResultEnum.ILLEGAL_ACCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
src/main/java/com/fjy/spring/domain/VUserque.java
Normal file
30
src/main/java/com/fjy/spring/domain/VUserque.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.fjy.spring.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
import org.hibernate.annotations.Subselect;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Immutable
|
||||
@Subselect("SELECT * FROM v_userque")
|
||||
public class VUserque {
|
||||
|
||||
@Id
|
||||
@Column(name = "coluserid")
|
||||
private Integer userid;
|
||||
|
||||
@Column(name = "colquestion")
|
||||
private String question;
|
||||
|
||||
@Column(name = "colanswer")
|
||||
private String answer;
|
||||
|
||||
@Column(name = "colname")
|
||||
private String name;
|
||||
|
||||
}
|
||||
@@ -12,6 +12,9 @@ public enum ResultEnum {
|
||||
WRONG_FORM(607,"表单错误"),
|
||||
EMPTY_DATA(608,"无数据"),
|
||||
ID_NULLPOINT(609,"id为空"),
|
||||
EMPTY_QUESTION(610,"该用户未设置密保问题"),
|
||||
QUESTION_ERROR(611,"问题与答案不匹配"),
|
||||
|
||||
;
|
||||
private Integer code;
|
||||
private String msg;
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.fjy.spring.repository;
|
||||
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -9,4 +11,7 @@ public interface TbUserRepository extends JpaRepository<TbUser,Integer> {
|
||||
|
||||
public Optional<TbUser> findByColname(String name);
|
||||
|
||||
@Modifying
|
||||
@Query("UPDATE TbUser u SET u.colpassword=?1 WHERE u.colname = ?2")
|
||||
public int updateColpasswordByColname(String password,String name);//返回更新的行数
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.fjy.spring.repository;
|
||||
|
||||
import com.fjy.spring.domain.VUserque;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public interface VUserqueRepository extends JpaRepository<VUserque,Integer> {
|
||||
|
||||
public Optional<VUserque> findByName(String name);
|
||||
|
||||
}
|
||||
@@ -3,14 +3,19 @@ package com.fjy.spring.service;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.domain.TbUserque;
|
||||
import com.fjy.spring.domain.VUserinfo;
|
||||
import com.fjy.spring.domain.VUserque;
|
||||
import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.repository.TbUserRepository;
|
||||
import com.fjy.spring.repository.TbUserqueRepository;
|
||||
import com.fjy.spring.repository.VUserinfoRepository;
|
||||
import com.fjy.spring.repository.VUserqueRepository;
|
||||
import com.fjy.spring.untils.CodingUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -24,62 +29,69 @@ public class UserService {
|
||||
@Autowired
|
||||
private TbUserqueRepository userqueRepository;
|
||||
|
||||
@Autowired
|
||||
private VUserqueRepository vUserqueRepository;
|
||||
|
||||
@Autowired
|
||||
private VUserinfoRepository vUserinfoRepository;
|
||||
|
||||
|
||||
public TbUser doLoginService(String name,String password){
|
||||
public TbUser doLoginService(String name, String password) {
|
||||
//TbUser user = (TbUser)tbUserRepository.findByColname(name).get();
|
||||
Optional<TbUser> tbUser = tbUserRepository.findByColname(name);
|
||||
TbUser user = new TbUser();
|
||||
if (tbUser.isPresent()){
|
||||
user = (TbUser)tbUser.get();
|
||||
}else {
|
||||
if (tbUser.isPresent()) {
|
||||
user = (TbUser) tbUser.get();
|
||||
} else {
|
||||
throw new UserException(ResultEnum.EMPTY_DATA);
|
||||
}
|
||||
|
||||
if (user!=null){
|
||||
if (password.equals(user.getColpassword())){
|
||||
if (user != null) {
|
||||
if (password.equals(user.getColpassword())) {
|
||||
return user;
|
||||
}else {
|
||||
} else {
|
||||
throw new UserException(ResultEnum.WRONGPASS);
|
||||
}
|
||||
}else {
|
||||
} else {
|
||||
throw new UserException(ResultEnum.USER_NOTEXIST);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean doRegisterService(TbUser tbUser){
|
||||
public boolean doRegisterService(TbUser tbUser) {
|
||||
|
||||
TbUser user = tbUserRepository.save(tbUser);
|
||||
if (user!=null){
|
||||
if (user != null) {
|
||||
//throw new UserException(ResultEnum.SUCCESS);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<TbUser> findAllUser(){
|
||||
public List<TbUser> findAllUser() {
|
||||
return tbUserRepository.findAll();
|
||||
}
|
||||
|
||||
public VUserinfo findUserInfo(Integer coluserid){
|
||||
public VUserinfo findUserInfo(Integer coluserid) {
|
||||
return vUserinfoRepository.findById(coluserid).get();
|
||||
}
|
||||
|
||||
public Optional<TbUser> findByColname(String name){
|
||||
public Optional<TbUser> findByColname(String name) {
|
||||
return tbUserRepository.findByColname(name);
|
||||
}
|
||||
|
||||
public boolean addUserQue(TbUserque userque){
|
||||
public boolean addUserQue(TbUserque userque) {
|
||||
TbUserque tbUserque = userqueRepository.save(userque);
|
||||
if (tbUserque!=null)
|
||||
if (tbUserque != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/*public TbUserque findUserQue(String question){
|
||||
return userqueRepository
|
||||
}*/
|
||||
public Optional<VUserque> findUserQueByName(String name) {
|
||||
return vUserqueRepository.findByName(name);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean updateColpasswordByColname(String password, String name) throws Exception {
|
||||
return tbUserRepository.updateColpasswordByColname(new BigInteger(CodingUtil.encryptSHA(password.getBytes())).toString(32), name) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user