diff --git a/src/main/java/com/fjy/spring/controller/DataController.java b/src/main/java/com/fjy/spring/controller/DataController.java index 9be2a96..072b034 100644 --- a/src/main/java/com/fjy/spring/controller/DataController.java +++ b/src/main/java/com/fjy/spring/controller/DataController.java @@ -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 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); + } + } } diff --git a/src/main/java/com/fjy/spring/domain/VUserque.java b/src/main/java/com/fjy/spring/domain/VUserque.java new file mode 100644 index 0000000..708b134 --- /dev/null +++ b/src/main/java/com/fjy/spring/domain/VUserque.java @@ -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; + +} diff --git a/src/main/java/com/fjy/spring/enums/ResultEnum.java b/src/main/java/com/fjy/spring/enums/ResultEnum.java index 75f05ce..78999c8 100644 --- a/src/main/java/com/fjy/spring/enums/ResultEnum.java +++ b/src/main/java/com/fjy/spring/enums/ResultEnum.java @@ -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; diff --git a/src/main/java/com/fjy/spring/repository/TbUserRepository.java b/src/main/java/com/fjy/spring/repository/TbUserRepository.java index bfcd308..1af7f9d 100644 --- a/src/main/java/com/fjy/spring/repository/TbUserRepository.java +++ b/src/main/java/com/fjy/spring/repository/TbUserRepository.java @@ -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 { public Optional findByColname(String name); + @Modifying + @Query("UPDATE TbUser u SET u.colpassword=?1 WHERE u.colname = ?2") + public int updateColpasswordByColname(String password,String name);//返回更新的行数 } diff --git a/src/main/java/com/fjy/spring/repository/VUserqueRepository.java b/src/main/java/com/fjy/spring/repository/VUserqueRepository.java new file mode 100644 index 0000000..f84715d --- /dev/null +++ b/src/main/java/com/fjy/spring/repository/VUserqueRepository.java @@ -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 { + + public Optional findByName(String name); + +} diff --git a/src/main/java/com/fjy/spring/service/UserService.java b/src/main/java/com/fjy/spring/service/UserService.java index 854f44f..7114ab8 100644 --- a/src/main/java/com/fjy/spring/service/UserService.java +++ b/src/main/java/com/fjy/spring/service/UserService.java @@ -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 = 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 findAllUser(){ + public List findAllUser() { return tbUserRepository.findAll(); } - public VUserinfo findUserInfo(Integer coluserid){ + public VUserinfo findUserInfo(Integer coluserid) { return vUserinfoRepository.findById(coluserid).get(); } - public Optional findByColname(String name){ + public Optional 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 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; + } } diff --git a/src/main/resources/static/js/LoginStyle.js b/src/main/resources/static/js/LoginStyle.js index 046874c..71543bf 100644 --- a/src/main/resources/static/js/LoginStyle.js +++ b/src/main/resources/static/js/LoginStyle.js @@ -1,6 +1,8 @@ +let outSideThis = this; var Main = { data() { var checkName = (rule, value, callback) => { + let that= this; if (!value) { return callback(new Error('用户名不能为空')); }else { @@ -20,7 +22,42 @@ var Main = { }) .catch(function (error) { console.log(error); - this.errorNotify(error.message); + that.errorNotify(error.message); + }); + } + }; + var checkQuestion = (rule, value, callback) => { + if (!value) { + return callback(new Error('问题不能为空')); + } else { + callback() + } + }; + var checkAnswer = (rule, value, callback) => { + let that = this; + if (!value) { + return callback(new Error('答案不能为空')); + } else { + axios.get(getRootPath_web() + '/finduserque', { + params: { + name :outSideThis.findpass.colname.value, + question:outSideThis.findpass.question.value, + answer: value + } + }) + .then(function (response) { + console.log(response.data); + if (response.data === true) { + callback() + } else if (response.data === false){ + return callback(new Error('答案错误')); + }else { + return callback(new Error(response.data.message)); + } + }) + .catch(function (error) { + console.log(error); + that.errorNotify("未知错误"); }); } }; @@ -99,6 +136,25 @@ var Main = { callback(); } }; + var validatePass3 = (rule, value, callback) => { + if (value === '') { + callback(new Error('请输入密码')); + } else { + if (this.ruleForm3.checkPass !== '') { + this.$refs.ruleForm3.validateField('checkPass'); + } + callback(); + } + }; + var validatePass4 = (rule, value, callback) => { + if (value === '') { + callback(new Error('请再次输入密码')); + } else if (value !== this.ruleForm3.colpassword) { + callback(new Error('两次输入密码不一致!')); + } else { + callback(); + } + }; return { ruleForm1: { colname: '', @@ -112,6 +168,30 @@ var Main = { colrealname: '', colemail: '' }, + ruleForm3: { + colname:'', + question: '', + answer: '', + colpassword: '', + checkPass: '' + }, + rules3: { + colname: [ + {required: true,validator: checkName1, trigger: 'blur'} + ], + question: [ + {required: true,validator: checkQuestion, trigger: 'blur'} + ], + answer: [ + {required: true,validator: checkAnswer, trigger: 'blur'} + ], + colpassword: [ + {required: true, validator: validatePass3, trigger: 'blur'} + ], + checkPass: [ + {required: true, validator: validatePass4, trigger: 'blur'} + ] + }, rules1: { colpassword: [ {required: true,validator: validatePass, trigger: 'blur'} @@ -155,12 +235,63 @@ var Main = { message: content }) }, - submitForm(formName) { + openNotiSuccess(title, content) { + this.$notify({ + title: title, + message: content, + type: 'success' + }); + }, + openNotiError(title, content) { + this.$notify.error({ + title: title, + message: content + }); + }, + submitForm(formName, url) { this.$refs[formName].validate((valid) => { - if (valid) { - alert('submit!'); + var that = this; + if (valid) {//此处暂时去除校验 + axios({ + url: getRootPath_web()+'/' + url, + method: 'post', + data: { + name :outSideThis.findpass.colname.value, + password:outSideThis.findpass.colpassword.value, + question:outSideThis.findpass.question.value, + answer:outSideThis.findpass.answer.value, + }, + transformRequest: [function (data) { + // Do whatever you want to transform the data + let ret = '' + for (let it in data) { + ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' + } + return ret + }], + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }).then(function (response) { + console.log(response.data); + if (response.data===true){ + that.openNotiSuccess("成功", "修改成功,请切换至登录选项!"); + }else if (response.data===false){ + that.openNotiError("失败", "修改失败!"); + }else { + that.openNotiError("错误", response.data.message); + } + }).catch(function (error) { + console.log(error); + that.openNotiError("错误", "服务器错误!"); + }); + //console.log(this.$refs.content.value) + //this.openNotiSuccess("成功", "修改成功!") + //this.$options.methods.openNotiSuccess.bind(this)(); + //alert('submit!'); } else { console.log('error submit!!'); + that.openNotiError("错误", "表单填写错误!"); return false; } }); diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index f73760f..9f9b054 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -92,6 +92,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + 提交 + + 重置 + + + diff --git a/src/test/java/com/fjy/spring/controller/DataControllerTest.java b/src/test/java/com/fjy/spring/controller/DataControllerTest.java index 4fc64e3..9c41a1f 100644 --- a/src/test/java/com/fjy/spring/controller/DataControllerTest.java +++ b/src/test/java/com/fjy/spring/controller/DataControllerTest.java @@ -33,4 +33,62 @@ public class DataControllerTest { .param("answer","YHM")) .andExpect(MockMvcResultMatchers.content().string("true")); } + + /** + *测试找回密码 + * @throws Exception + */ + @Test + public void findUserQue() throws Exception{ + //测试问题和答案均正确 + mvc.perform(MockMvcRequestBuilders.get("/finduserque") + .param("name","root") + .param("question","您配偶的姓名是?") + .param("answer","abc")) + .andExpect(MockMvcResultMatchers.content().string("true")); + + //测试问题错误 + mvc.perform(MockMvcRequestBuilders.get("/finduserque") + .param("name","root") + .param("question","您配偶的姓名是") + .param("answer","abc")) + .andExpect(MockMvcResultMatchers.content().json("{\n" + + " \"code\": 611,\n" + + " \"message\": \"问题与答案不匹配\",\n" + + " \"data\": null\n" + + "}")); + + //测试问题正确,答案错误 + mvc.perform(MockMvcRequestBuilders.get("/finduserque") + .param("name","root") + .param("question","您配偶的姓名是?") + .param("answer","a")) + .andExpect(MockMvcResultMatchers.content().string("false")); + + //未设置问题 + mvc.perform(MockMvcRequestBuilders.get("/finduserque") + .param("name","roo") + .param("question","您配偶的姓名是?") + .param("answer","a")) + .andExpect(MockMvcResultMatchers.content().json("{\n" + + " \"code\": 610,\n" + + " \"message\": \"该用户未设置密保问题\",\n" + + " \"data\": null\n" + + "}")); + } + + /** + * 测试忘记密码操作 + * @throws Exception + */ + @Test + @Transactional + public void resetPass() throws Exception{ + mvc.perform(MockMvcRequestBuilders.post("/resetPass") + .param("name","root") + .param("question","您配偶的姓名是?") + .param("answer","abc") + .param("password","admin")) + .andExpect(MockMvcResultMatchers.content().string("true")); + } } \ No newline at end of file