• 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html
  • 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html

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

项目实战 Geekerstar 10个月前 (06-22) 433次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

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<User> 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<java.lang.String>
 */

@RequestMapping(value = "forget_get_question.do",method = RequestMethod.GET)
@ResponseBody
public ServerResponse<String> 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<String> forgetCheckAnswer(String username,String question,String answer){
    return iUserService.checkAnswer(username,question,answer);
}

IUserService.java

ServerResponse selectQuestion(String username);

ServerResponse<String> 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<String> 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

<select id="selectQuestionByUsername" resultType="string" parameterType="string">
   SELECT
   question
   from mmall_user
   where username = #{username}
 </select>
 
<select id="checkAnswer" resultType="int" parameterType="map">
   SELECT
   count(1)
   from mmall_user
   WHERE username=#{username}
   and question = #{question}
   and answer = #{answer}
</select>

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<String,String> localCache = CacheBuilder.newBuilder().initialCapacity(1000).maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS)
            .build(new CacheLoader<String, String>() {
                //默认的数据加载实现,当调用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;
    }
}

喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

您必须 登录 才能发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00