从零到企业级SSM电商项目实战教程(十九)获取用户登录信息,忘记密码,提示问题与答案,重置密码功能开发

UserController.java

/*
 * @Description: 获取用户登录信息
 *
 * @auther: Geekerstar(jikewenku.com)
 * @date: 2018/6/22 14:52
 * @param: 
 * @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.createByErrorMessgae("用户未登录,无法获取当前用户的信息");
}
/*
 * @Description: 忘记密码
 *
 * @auther: Geekerstar(jikewenku.com)
 * @date: 2018/6/22 14:52
 * @param: [username]
 * @return: com.mmall.common.ServerResponse
 */

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


/*
 * @Description: 检查问题答案
 *
 * @auther: Geekerstar(jikewenku.com)
 * @date: 2018/6/22 14:53
 * @param: 
 * @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);
}

IUserService.java

ServerResponse selectQuestion(String username);

ServerResponse checkAnswer(String username,String question,String answer);

UserServiceImpl.java

public ServerResponse selectQuestion(String username){
    ServerResponse validResponse = this.checkValid(username,Const.USERNAME);
    if(validResponse.isSuccess()){
        //用户不存在
        return ServerResponse.createByErrorMessgae("用户不存在");
    }
    String question = userMapper.selectQuestionByUsername(username);
    if(StringUtils.isNoneBlank(question)){
        return ServerResponse.createBySuccess(question);
    }
    return ServerResponse.createByErrorMessgae("找回密码的问题是空的");
}

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("token_"+username,forgetToken);
        return ServerResponse.createBySuccess(forgetToken);
    }
    return ServerResponse.createByErrorMessgae("问题的答案错误");
}

UserMapper.java

String selectQuestionByUsername(String username);

int checkAnswer(@Param("username") String username,@Param("question") String question,@Param("answer") String answer);

UserMapper.xml


 

TockenCache.java

package com.mmall.common;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;


/**
 * @Author: Geekerstar(jikewenku.com)
 * @Date: 2018/6/22 13:45
 * @Description:
 */
public class TokenCache {
    private static Logger logger = LoggerFactory.getLogger(TokenCache.class);
    //LRU算法
    private static LoadingCache localCache = CacheBuilder.newBuilder().initialCapacity(1000).maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS)
            .build(new CacheLoader() {
                //默认的数据加载实现,当调用get取值的时候,如果key没有对应的值,就调用和这个方法进行加载
                @Override
                public String load(String s) throws Exception {
                    return "null";
                }
            });


    public static void setKey(String key,String value){
        localCache.put(key,value);
    }

    public static String getKey(String key){
        String value = null;
        try{
            value = localCache.get(key);
            if("null".equals(value)){
                return null;
            }
            return value;
        }catch (Exception e){
            logger.error("localCache get error",e);
        }
        return null;
    }
}
本站所有文章均来自互联网,如有侵权,请联系站长删除。极客文库 » 从零到企业级SSM电商项目实战教程(十九)获取用户登录信息,忘记密码,提示问题与答案,重置密码功能开发
分享到:
赞(0)

评论抢沙发

评论前必须登录!