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