简单的cas单点登录+加密 SSM

楼主新萌一枚,写了一些小的demo 拿出来分析一下,如果有那些问题 欢迎大家前来指点,废话不多说啦,下面是代码

~~~~~~~~~~~~~~~~~~~~华丽的分割线~~~~~~~~~~~~~~~~~~~

/*楼主redis 的xml配置一直失败,然后就放弃了,改成了编码形式 

第一次发帖,害羞胆怯

登陆方法

*/

@Override

    public Customer loginSelect(String username, String password, HttpServletResponse response) throws Exception {
        Customer customer = loginDao.loginSelect(username, Md5Utils.encryptPassword(password, "加盐"));
         有数据的话 ,就保存在session中
        if (null != customer) {
             更新最后登陆时间
            loginDao.updateLdate(format.format(new Date()), customer.getC_id());
             将对象装换为JSON
            String str = JSON.json(new Cust(customer.getC_id(), customer.getC_nickname()));
            String token = UUID.randomUUID().toString().replaceAll(-, );
             生成token 放入redis中
            Jedis jedis = RedisPoll.getJedis();
            jedis.set(token, str);
             设置15分钟
            jedis.expire(token, 60  15);
            RedisPoll.returnResource(jedis);
             放入cookie
            Cookie cookie = new Cookie(token, token);
            cookie.setPath();
            cookie.setMaxAge(60  15);
            response.addCookie(cookie);
            return customer;
        }
        return null;
    }
    
    
    /**
     * 单点登陆认证中心
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getCookie.action")
    @ResponseBody
    public String dandian(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 获得到客户端的cookies
        Cookie[] cookies = request.getCookies();
        if (null != cookies && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                // 拿到token
                if (cookie.getName().equals("token")) {
                    // 有
                    Jedis jedis = RedisPoll.getJedis();
                    // 得到token的value
                    String value = cookie.getValue();
                    String customer = jedis.get(value);
                    // 判断redis中是否有这个字
                    if (!"null".equals(customer)) {
                        // 更新在redis中的有效时间
                        jedis.expire(value, 60 * 15);
                        // 更新cookie有效时间
                        cookie.setMaxAge(60 * 15);
                        response.addCookie(cookie);
                        // 关闭redis
                        RedisPoll.returnResource(jedis);
                        return customer;
                    }
                    // 关闭redis
                    RedisPoll.returnResource(jedis);
                }
            }
        }
        return "null";
    }
    
    
    
    /**
     * 退出登陆
     *
     * @param request
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("/outlogin.action")
    @ResponseBody
    public String outLoign(HttpServletRequest request) throws IOException {
        // 获得到cookies
        Cookie[] cookies = request.getCookies();
        if (null != cookies && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                // 得到token
                if (cookie.getName().equals("token")) {
                    // 有
                    Jedis jedis = RedisPoll.getJedis();
                    // 得到token的value
                    String value = cookie.getValue();
                    String customer = jedis.get(value);
                    // 判断redis中是否有这个字
                    if (!"null".equals(customer)) {
                        // 删除这个redis
                        jedis.del(value);                   
                        // 关闭redis
                        RedisPoll.returnResource(jedis);
                        return "null";
                    }
                    // 关闭redis
                    RedisPoll.returnResource(jedis);
                }
            }
        }
        return "null";
    }
    
    
    
    
    
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值