SSM - 사용자 모듈 (2) 암호를 잊고 수정하고 사용자 정보를 얻기

7926 단어 SSM

다오층
/**
     *         
     * @param username
     * @return
     */
    String selectQuestionByUsername(String username);

    /**
     *           
     * @param username
     * @param question
     * @param answer
     * @return
     */
    int checkAnswer(@Param("username") String username,@Param("question") String question,@Param("answer") String answer);

    /**
     *           
     * @param username
     * @param passwordNew
     * @return
     */
    int updatePasswordByUsername(@Param("username") String username,@Param("passwordNew") String passwordNew);

    /**
     *            
     * @param password
     * @return
     */
    int checkPassword(@Param("password")String password,@Param("userId") Integer userId);

 
 
mybatis 


  
  

  
    update mmall_user
    set password=#{passwordNew},update_time=now()
    where username=#{username}
  

  

 
 
업무층 인터페이스
/**
     *     
     * @param username
     * @return
     */
    ServerResponse selectQuestion(String username);

    /**
     *             
     * @param username
     * @param question
     * @param answer
     * @return
     */
    ServerResponse checkAnswer(String username,String question,String answer);

    /**
     *          
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
    ServerResponse forgetRestPassword(String username,String passwordNew,String forgetToken);

    /**
     *         
     * @param passwordOld
     * @param passwordNew
     * @param user
     * @return
     */
    ServerResponse resetPassword(String passwordOld,String passwordNew,User user);

 
비즈니스 구현 인터페이스
public ServerResponse selectQuestion(String username){

        ServerResponse validResponse=this.checkValid(username,Const.USERNAME);

        if(validResponse.isSuccess()){
            //     

            return ServerResponse.createByErrorMessage("     ");
        }

        String question=userMapper.selectQuestionByUsername(username);

        if(StringUtils.isNotBlank(question)){
            return ServerResponse.createBySuccess(question);
        }

        return ServerResponse.createByErrorMessage("          ");
    }

//    public static void main(String[] args) {
//        System.out.println(UUID.randomUUID().toString());
//    }

    public ServerResponse checkAnswer(String username,String question,String answer){
        int resultCount=userMapper.checkAnswer(username,question,answer);
        if(resultCount >0 ){
            //               ,      
            String forgetToken= UUID.randomUUID().toString();
            TokenCache.setKey(TokenCache.TOKEN_PREFIX+username,forgetToken);

            return ServerResponse.createBySuccess(forgetToken);
        }

        return ServerResponse.createByErrorMessage("       ");


    }

    public ServerResponse forgetRestPassword(String username,String passwordNew,String forgetToken){
        if(StringUtils.isBlank(forgetToken)){
            return ServerResponse.createByErrorMessage("    ,token    ");
        }
        ServerResponse validResponse=this.checkValid(username,Const.USERNAME);

        if(validResponse.isSuccess()){
            //     
            return ServerResponse.createByErrorMessage("     ");
        }

        String token=TokenCache.getKey(TokenCache.TOKEN_PREFIX+username);

        if(StringUtils.isBlank(token)){
            return ServerResponse.createByErrorMessage("token      ");
        }

        if(StringUtils.equals(forgetToken,token)){
            String md5Password=MD5Util.MD5EncodeUtf8(passwordNew);
            int rowCount=userMapper.updatePasswordByUsername(username,md5Password);
            if(rowCount>0){
                return ServerResponse.createBySuccessMessage("      ");
            }
        }else {
            return ServerResponse.createByErrorMessage("token  ,          token");
        }
        return ServerResponse.createByErrorMessage("      ");
    }


    public ServerResponse resetPassword(String passwordOld,String passwordNew,User user){

        //      ,             ,          ,         count(1),     id,      true count>0;
        int resultCount=userMapper.checkPassword(MD5Util.MD5EncodeUtf8(passwordOld),user.getId());
        if(resultCount ==0){
            return ServerResponse.createByErrorMessage("     ");
        }

        user.setPassword(MD5Util.MD5EncodeUtf8(passwordNew));
        int updateCount=userMapper.updateByPrimaryKeySelective(user);
        if(updateCount>0){
            return ServerResponse.createBySuccessMessage("      ");
        }
        return ServerResponse.createByErrorMessage("      ");
    }

 
controller층
 /**
     *       
     * @param session
     * @return
     */
    @RequestMapping(value = "get_user_info.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getUserInfo(HttpSession session){
        User user=(User) session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("     ");
    }

    /**
     *       
     * @param username
     * @return
     */
    @RequestMapping(value = "forget_get_question.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse forgetGetQuestion(String username){
        return iUserService.selectQuestion(username);
    }


    /**
     *       
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value = "forget_check_answer.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse forgetCheckAnswer(String username,String question,String answer){
        return iUserService.checkAnswer(username,question,answer);
    }


    /**
     *           
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
    @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse forgetRestPassword(String username,String passwordNew,String forgetToken){
        return iUserService.forgetRestPassword(username,passwordNew,forgetToken);
    }

    /**
     *        
     * @param session
     * @param passwordOld
     * @param passwordNew
     * @return
     */
    @RequestMapping(value = "reset_password.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse resetPassword(HttpSession session,String passwordOld,String passwordNew){

        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("     ");
        }

        return iUserService.resetPassword(passwordOld,passwordNew,user);
    }

좋은 웹페이지 즐겨찾기