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
    评论
在使用 HttpClient 登录接口后,获取登录接口的出参中的 token,可以通过以下步骤将 token 放到下一个接口的请求头中。 1. 首先,使用 HttpClient 发送登录请求并获取登录接口的出参。可以通过 HttpClient 发送 POST 请求,将用户名密码提交给登录接口,并接收登录接口返回的响应。 2. 在登录接口的出参中,找到包含 token 的字段。根据登录接口的响应格式,可能需要将响应转换为 JSON 对象或 XML 对象,然后从中获取到 token 的值。 3. 在发送下一个接口的请求时,修改 HttpClient 的请求头。可以通过设置请求头的方式将 token 添加到请求头中。具体可以使用 HttpClient 对象的 setHeader 方法,将键值对形式的 token 添加到请求头中。 4. 接下来,使用 HttpClient 发送下一个接口的请求。可以根据需要设置请求方法、URL、请求体等相关信息。在发送请求前,可以通过 HttpClient 对象的 addHeader 方法,将 token 添加到请求头中。 5. 最后,使用 HttpClient 执行下一个接口的请求,并接收返回的响应结果。可以通过解析响应结果,获取接口的出参,完成后续的业务逻辑。 需要注意的是,在实际应用中,可能会有多个接口需要使用同一个 token,所以建议将获取 token 和设置请求头的过程封装为一个方法或类,方便复用和维护。同时,为了保证安全性,可能需要对 token 进行加密或加密传输等处理。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值