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

来源:互联网 发布:网络运营管理岗 面试 编辑:程序博客网 时间:2024/06/05 09:51

楼主新萌一枚,写了一些小的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";
    }