redis实现单点登录

    //前端访问拦截去登陆页面 
    @RequestMapping(value = "/shopping/login.aspx",method=RequestMethod.GET)
    public String login(){
        return "login";
    }
//提交表单的登陆
    @RequestMapping(value = "/shopping/login.aspx",method=RequestMethod.POST)
    public String login(String username,String password,String returnUrl,Model model
            ,HttpServletRequest request,HttpServletResponse response){
        
        //判断用户名不能为空  "" "  "  Converter转换器
        if(null != username){
            //判断密码不能为空
            if(null != password){
                //用户名必须正确
                Buyer buyer = buyerService.selectBuyerByUsername(username);
                if(null != buyer){
                    //密码必须正确
                    if(encodePassowrd(password).equals(buyer.getPassword())){
                        //保存用户名到Session中  csessionid
                        sessionProvider.setAttribute(RequestUtils.getCSESSIONID(request, response), buyer.getUsername());
                        //回跳回之前访问页面
                        return "redirect:" + returnUrl;
                    }else{
                        model.addAttribute("error", "密码必须正确");
                    }
                }else{
                    model.addAttribute("error", "用户名必须正确");
                }
            }else{
                
                model.addAttribute("error", "密码不能为空");
            }
            
        }else{
            model.addAttribute("error", "用户名不能为空");
        }
        
        return "login";
    }
/     /判断用户是否登陆
    @RequestMapping(value = "/shopping/isLogin.aspx")
    public @ResponseBody
    MappingJacksonValue isLogin(String callback, HttpServletRequest request,HttpServletResponse response){
        String result = "0";
        //用户名
        String username = sessionProvider.getAttibute(RequestUtils.getCSESSIONID(request, response));
        if(null != username){
            result = "1";
        }
        //jsonp返回值
        MappingJacksonValue mjv = new MappingJacksonValue(result);
        mjv.setJsonpFunction(callback);
        return mjv;
    }
public class SessionProviderImpl implements SessionProvider{

    @Autowired
    private Jedis jedis;
    private Integer exp = 30;
    public void setExp(Integer exp) {
        this.exp = exp;
    }

    //保存用户名到redis  key == jsessionid  name: admin  
    @Override
    public void setAttribute(String key, String name) {
        // TODO Auto-generated method stub
        //保存
        jedis.set(key + ":" + Constants.BUYER_SESSION, name);
        //时间  30分钟 
        jedis.expire(key + ":" + Constants.BUYER_SESSION, 60*exp);

    }
    //取出用户名
    @Override
    public String getAttibute(String key) {
        // TODO Auto-generated method stub
        String username = jedis.get(key + ":" + Constants.BUYER_SESSION);
        if(null != username){
            //时间  30分钟 
            jedis.expire(key + ":" + Constants.BUYER_SESSION, 60*exp);
            return username;
        }
        return null;
    }
    //退出
    public void logout(String key){
        jedis.del(key + ":" + Constants.BUYER_SESSION);
    }
/**
 * 处理Request 生成CSESSIONID
 *
 */
public class RequestUtils {
    public static String getCSESSIONID(HttpServletRequest request,HttpServletResponse response){
        //1:获取Cookie
        Cookie[] cookies = request.getCookies();
        //jsession
        //购物车
        if(null != cookies && cookies.length > 0){
            for (Cookie cookie : cookies) {
                //2:从Cookie中获取CSESSIONID
                if(cookie.getName().equals("CSESSIONID")){
                    //3:如果有 直接使用
                    return cookie.getValue();
                }
            }
        }
        //4:判断如果 没有  创建一个CSESSIONID  保存CSESSIONID到Cookie中  保存COokie写回浏览器 
        String csessionid = UUID.randomUUID().toString().replaceAll("-", "");
        //Cookie
        Cookie cookie = new Cookie("CSESSIONID",csessionid);
        //设置路径
        cookie.setPath("/");
        //设置Cookie的存活时间    立即消失 0         关闭浏览器 消失-1    到时间再消失>0    前提:没有清理Cooke
        cookie.setMaxAge(-1);
        response.addCookie(cookie);
        
        return csessionid;
    }
}

你可能感兴趣的:(redis实现单点登录)