ssm+redis实现单点登录

1.redis.xml配置文件


        
    
    
        
        
     

2.cookie工具类

//cookie生成类
public class RequestUtils {
    //获取
    public static String  getCSESSIONID(HttpServletRequest request,HttpServletResponse response){
        //1:取出Cookie
        Cookie[] cookies = request.getCookies();
        if(null != cookies && cookies.length > 0){
            for (Cookie cookie : cookies) {
                //2: 判断COokie中是否有CSESSIONID
                if("CSESSIONID".equals(cookie.getName())){
                    //3:有  直接使用
                    return cookie.getValue();
                }
            }
        }
        //4:没有  创建一个CSESSIONID   并保存到COOKIE中  同时 把此COOKIe写回浏览器  使用此生成的CSESSIONID 
        String csessionid = UUID.randomUUID().toString().replaceAll("-", "");
        Cookie cookie = new Cookie("CSESSIONID",csessionid);
        //设置 存活时间      -1  0   >0
        cookie.setMaxAge(-1);
        //设置路径
        cookie.setPath("/");
        //设置跨域  localhost == www.babasport.com   www.jd.com search.jd.com  item.jd.com
        //cookie.setDomain(".jd.com");
        response.addCookie(cookie);
        return csessionid;
        
    }
}

3.redis存session接口

public interface SessionProvider {
    //先行提供接口
    //保存用户名到Redis中
    public void setAttribuerForUsername(String name,String value);
    
    //取用户名从Redis中
    public String getAttributeForUsername(String name);
    
    //验证码
    
    //退出登陆
}

4.接口实现类

/**
 * 实现类
 * 保存用户名或验证码到Redis中
 * Session共享
 */
public class SessionProviderImpl implements SessionProvider{

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

    @Override
    public void setAttribuerForUsername(String name, String value) {
        // TODO Auto-generated method stub
        //保存用户名到Redis中
        // K : CSESSIONID:Constants.USER_NAME   == name
        jedis.set(name + ":" +  Constants.USER_NAME, value);
        //时间 
        jedis.expire(name + ":" +  Constants.USER_NAME, 60*exp);
    }

    @Override
    public String getAttributeForUsername(String name) {
        // TODO Auto-generated method stub
        //fbb2016
        String value = jedis.get(name + ":" +  Constants.USER_NAME);
        if(null != value){
            //时间 
            jedis.expire(name + ":" +  Constants.USER_NAME, 60*exp);
        }
        return value;
    }
    
}
  //去登陆页面
    @RequestMapping(value = "/login",method=RequestMethod.GET)
    public String login(){
        return "login";
    }
    
    
    //判断用户是否登陆 (跨域)
    @RequestMapping(value = "/isLogin")
    public @ResponseBody
    MappingJacksonValue isLogin(String callback,HttpServletRequest request,HttpServletResponse response){
        Integer result  = 0;
        //判断用户是否已经登陆
        String username = sessionProvider.getAttributeForUsername(RequestUtils.getCSESSIONID(request, response));
        if(null != username){
            result = 1;
        }
        MappingJacksonValue  mjv = new MappingJacksonValue(result);
        mjv.setJsonpFunction(callback);
        return mjv;
        
    }

//提交登陆
    @RequestMapping(value = "/login.aspx",method=RequestMethod.POST)
    public String login(String username,String password,String returnUrl,
            HttpServletRequest request,HttpServletResponse response, Model model){
        //1:用户名不能为空
        if(null != username){
            //2:密码不能为空
            if(null != password){
                //3:用户名必须正确
                Buyer buyer = buyerService.selectBuyerByUsername(username);
                if(null != buyer){
                    //4:密码必须正确
                    if(buyer.getPassword().equals(encodePassword(password))){
                        //5:保存用户名到Session中(Redis中)                                        //sessionID                                 //用户名
                        sessionProvider.setAttribuerForUsername(RequestUtils.getCSESSIONID(request, response), buyer.getUsername());
                        //6:跳转到之前访问页面
                        return "redirect:" + returnUrl;
                    }else{
                        model.addAttribute("error", "密码必须正确");
                    }
                    
                }else{
                    model.addAttribute("error", "用户名必须正确");
                }
                
            }else{
                model.addAttribute("error", "密码不能为空");
            }
            
        }else{
            model.addAttribute("error", "用户名不能为空");
        }
        return "login";
    }
$(function(){
    //去判断用户是否登陆  0  1
    $.ajax({
        url : "http://localhost:8082/isLogin.aspx",
        type : "post",
        dataType : "jsonp",
        success : function(data){
            //判断   0  1 
            //alert(data);
            if(data){
                $("#login").hide();
                $("#regist").hide();
            }else{
                $("#logout").hide();
                $("#myOrder").hide();
            }
        }
    });
})
//去登陆页面
function login(){
    window.location.href = "http://localhost:8082/login.aspx?returnUrl=" + encodeURIComponent(window.location.href);
}

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