cas用httpclient登录,并获取ticket,可以很方便扩展,如果要ajax登录可以基于这个接口,在resp种植castgc的cookie到浏览器

public static Map<String, String> validate(HttpServletRequest req) throws Exception {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        boolean rememberMe = false;
        if (StringUtils.isNotBlank(req.getParameter("rememberMe"))) {
            rememberMe = true;
        }
        return validate(loginName, password, rememberMe);
    }

    /**
     *
     * @param loginUrl
     *            https://mmh.cas.com:8443/cas/login
     * @param username
     *            张三
     * @param password
     *            111
     * @return
     * @throws Exception
     */
    public static Map<String, String> validate(String loginName, String password, boolean rememberMe) throws Exception {
        String loginUrl = ConfigUtil.getInstance().getProperty("cas.server.login.url");
        LOG.info("[{}]开始登录", loginName);
        HttpClient httpclient = new HttpClient();
        PostMethod method = new PostMethod();
        Map<String, String> params = getParams(loginUrl);
        String lt = params.get("lt");
        String execution = params.get("execution");
        String cookie = params.get("Set-Cookie");
        String reqCnt = "username=" + URLEncoder.encode(loginName,ENCODE) + "&password=" + URLEncoder.encode(password,ENCODE) + "<="
                + URLEncoder.encode(lt) + "&execution=" + execution + "&_eventId=submit&submit=LOGIN";
        if (rememberMe) {
            reqCnt = reqCnt + "&rememberMe=true";
        }
        method.setRequestBody(reqCnt);
        method.setFollowRedirects(false);
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        method.addRequestHeader(new Header("Cookie", cookie));
        method.setPath(loginUrl);
        httpclient.executeMethod(method);
        String casTgtCookie = null;
        Header tgtHead = method.getResponseHeader("Set-Cookie");
        if (tgtHead != null) {
            casTgtCookie = tgtHead.getValue();
        }
        Map<String, String> resp = Maps.newHashMap();
        resp.put("CASTGC", casTgtCookie);
        resp.put("msg", "登录成功");
        System.out.println(casTgtCookie);
        String respStr = method.getResponseBodyAsString();
        if (StringUtils.contains(respStr, "登录成功")) {
            LOG.info("loginName[{}]登录成功", loginName);
            IAuthenticationService aService = (IAuthenticationService) BdpHessianProxy
                    .getService(IAuthenticationService.class, "authenticationService");
            User user = aService.getUserByLoginName(loginName);
            String userCode = user.getCode().toString();
            String ticket = StringUtils.substringBetween(tgtHead.getValue(), "CASTGC=", "; Path=");
            resp.put("userCode", userCode);
            resp.put("ticket", ticket);
            cache.set(userCode, ticket, CookieUtil.TICKET_EXPIRE_TIME);
            return resp;
        }
        Document doc = Jsoup.parse(respStr);
        String msg = doc.select(".errors").text();
        resp.put("msg", msg);
        LOG.info("loginName[{}]登录失败[{}]", loginName, msg);

        return resp;

    }

    public static void main(String[] args) throws Exception {
        validate("张三", "111", false);
    }

    static Map<String, String> getParams(String loginUrl) throws Exception {
        Map<String, String> params = Maps.newHashMap();
        HttpClient httpclient = new HttpClient();
        HttpMethod method = new GetMethod();
        method.setPath(loginUrl);
        method.setFollowRedirects(false);
        httpclient.executeMethod(method);
        String cont = method.getResponseBodyAsString();
        Document doc = Jsoup.parse(cont);
        params.put("Set-Cookie", method.getResponseHeader("Set-Cookie").getValue());
        params.put("lt", doc.getElementsByAttributeValue("name", "lt").attr("value"));
        params.put("execution", doc.getElementsByAttributeValue("name", "execution").attr("value"));
        return params;
    }

jar包

//htmlparser

  compile "org.jsoup:jsoup:1.7.2"

  //httpclient

  compile "commons-httpclient:commons-httpclient:3.1"



cas 3.5.2


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值