package com.lch.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.DefaultHttpParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;
public class DominoLogin {
public static void main(String[] args) throws HttpException, IOException {
Protocol myhttps = new Protocol("https", new MySecureProtocolSocketFactory (), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("127.0.0.1", 8443, myhttps);
// HttpMethod method = getGetMethod();
HttpMethod method = getPostMethod();
//method.addRequestHeader("Content-Type","text/html; charset=gb2312");
method.getParams().setContentCharset("utf-8");// 设置字符编码
// method.addRequestHeader("(Request-Line)", "POST /cas/login;jsessionid=29BF7E8BBDDB367B845F574EA11DFE0F HTTP/1.1");
// method.addRequestHeader("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/QVOD, application/QVOD, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*");
// method.addRequestHeader("Accept-Encoding", "gzip, deflate");
// method.addRequestHeader("Accept-Language", "zh-cn");
// method.addRequestHeader("Cache-Control", "no-cache");
// method.addRequestHeader("Connection", "Keep-Alive");
// method.addRequestHeader("Content-Length", "151");
// method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// method.addRequestHeader("Cookie", "JSESSIONID=29BF7E8BBDDB367B845F574EA11DFE0F");
// method.addRequestHeader("Host", "localhost:8443");
// method.addRequestHeader("Referer", "https://localhost:8443/cas/login");
// method.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB6.5)");
DefaultHttpParams.getDefaultParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
int statuscode = client.executeMethod(method);
System.out.println(method.getStatusLine());
if((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER)||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)){
System.out.println(statuscode);
Header header = method.getResponseHeader("location");
System.out.println(header);
if(header != null){
String newuri = header.getValue();
if((newuri != null) || (!newuri.equals(""))){
newuri = "/";
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString() );
System.out.println("***************************");
System.out.println(redirect.getResponseBodyAsString());
redirect.releaseConnection();
}else{
System.out.println("Invalid redirect");
}
}
}
method.setRequestHeader("charset", "utf-8");
// String response = new
// String(method.getResponseBodyAsString().getBytes("UTF-8"));
String response = method.getResponseBodyAsString();
System.out.println(response);
System.out.println("*********************************************");
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
for(int i=0;i <cookies.length;i++)
{
System.out.print(cookies[i].toString());
}
method.releaseConnection();
}
private static HttpMethod getPostMethod() {
PostMethod post = new PostMethod("/cas/login");
NameValuePair username = new NameValuePair("username", "admin");
NameValuePair password = new NameValuePair("password", "admin");
NameValuePair lt = new NameValuePair("lt", "_cD3B3A7E7-A323-F1FB-340B-18A914EE256D_k1CCD75E1-36E0-C3EC-B56A-21B0AB07C99E");
NameValuePair _eventId = new NameValuePair("_eventId", "submit");
NameValuePair locale = new NameValuePair("locale", "zh_CN");
NameValuePair jsessionid = new NameValuePair("jsessionid", "C9E3DF7018D7D5FF2384A7F990C552A9");
NameValuePair warn = new NameValuePair("warn", "true");
post.setRequestBody(new NameValuePair[] { username, password, lt, _eventId, locale, jsessionid, warn});
return post;
}
// private static HttpMethod getGetMethod() {
// return new GetMethod("/search.asp?action=mobile&mobile=1368359");
// }
}
package com.lch.httpclient;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {
static{
System.out.println(">>>>in MySecureProtocolSocketFactory>>");
}
private SSLContext sslcontext = null;
private SSLContext createSSLContext() {
SSLContext sslcontext=null;
try {
sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslcontext;
}
private SSLContext getSSLContext() {
if (this.sslcontext == null) {
this.sslcontext = createSSLContext();
}
return this.sslcontext;
}
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
socket,
host,
port,
autoClose
);
}
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
host,
port
);
}
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
}
public Socket createSocket(String host, int port, InetAddress localAddress,
int localPort, HttpConnectionParams params) throws IOException,
UnknownHostException, ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (timeout == 0) {
return socketfactory.createSocket(host, port, localAddress, localPort);
} else {
Socket socket = socketfactory.createSocket();
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
socket.connect(remoteaddr, timeout);
return socket;
}
}
//自定义私有类
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
}
package com.lch.test;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class DominoLT {
public static void main(String[] args) throws HttpException, IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("******", 808, "http");
//HttpMethod method = getGetMethod();
HttpMethod method = getPostMethod();
method.getParams().setContentCharset("GB2312");
int statuscode = client.executeMethod(method);
System.out.println(method.getStatusLine());
method.setRequestHeader("charset", "gb2312");
//String response = new String(method.getResponseBodyAsString().getBytes("UTF-8"));
String response = method.getResponseBodyAsString();
System.out.println(response);
if((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER)||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)){
System.out.println(statuscode);
Header header = method.getResponseHeader("location");
System.out.println(header);
String newuri = header.getValue();
PostMethod redirect = new PostMethod(newuri);
client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString() );
System.out.println("************0***************");
System.out.println(redirect.getResponseBodyAsString());
System.out.println("************1***************");
PostMethod Red = new PostMethod("http://*******/cxoanew/index.nsf/SForm01?OpenForm");
client.executeMethod(Red);
System.out.println(Red.getResponseBodyAsString());
System.out.println("************2***************");
redirect.releaseConnection();
}
method.releaseConnection();
}
private static HttpMethod getPostMethod() {
PostMethod post = new PostMethod("/names.nsf?Login");
NameValuePair username = new NameValuePair("username","****");
NameValuePair password = new NameValuePair("password","****");
post.setRequestBody(new NameValuePair[] {username, password});
return post;
}
private static HttpMethod getGetMethod() {
return new GetMethod("/names.nsf?Login?username=*****&password=****");
}
}
分享到:
相关推荐
无https验证的CAS单点登录,统一认证
CAS客户端跳过https验证
包含单点登录tomcat部署,cas服务器部署,项目demo,可更好的理解sso-cas登录
cas 单点登录 修改https访问协议为http
cas-server-webapp-4.0.0.war--cas server去掉https验证.下载后直接部署tomcat即可,建议下载后将名称改为cas.war
#cas.logout.url=https://zhang.com:8443/CasServer/logout?server=http://localhost:8080/NX_IMP cas.logout.url=http://127.0.0.1:8088/CasServer/logout?server=http://127.0.0.1:8080/NX_IMP 分别是cas 服务的...
<param-value>https://www.test.com:8443/cas</param-value> <!-- LOCAL web URL --> <param-name>serverName <param-value>http://www.testd.com:8080</param-value> <!-- if validation ...
CAS 服务端代码,去除https验证模式,拖到tomcat 6.0+就可以用,免配置,cas-server-webapp-3.5.2\WEB-INF\deployerConfigContext.xml下修改数据库验证模板,直接套用模板就好,so easy
cas协议3.0 中文版教程详解 这是CAS协议 1.0,2.0和3.0版本的官方规范。 Central Authentication Service(以下简称CAS)是一个网络的单点登陆(SSO)/单点登出(SLO)协议。它允许用户在访问多个应用程序时,仅需向中央...
cas 6.3 可执行war包,可直接在各个操作系统上执行,内置启动脚本 1、支持外部配置文件 2、支持http 3、支持mysql的jdbc配置
CAS Server 是一套基于 Java 实现的服务,该服务以一个 Java Web Application 单独部署在与 servlet2.3 兼容的 Web 服务器上,另外,由于 Client 与 CAS Server 之间的交互采用 Https 协议,因此部署 CAS Server 的...
discuz集合了cas单点登录,先安装install下面的discuz.sql,修改config.inc.php文件,将logging.php里面的cas指定到你的单点登录服务器上就行,目前做的是http的单点登录,如果要改为https的,请将CAS文件夹下的...
spring+springmvc+shiro+cas单点登录实例 springmvc+spring+shiro+cas单点登录实例 加入了登录验证码认证,修改了下首页样式,不过样式...github :https://github.com/xiaofeifeia/spring-springmvc-shiro-cas-.git
GitHub地址:https://github.com/X-rapido/CAS_SSO_Record > 以下内容根据自己的理解编写,有不正确的地方,还望朋友们及时告知,并给出解决方案,感激不尽! 框架说明 Maven 或 Gradle springboot 1.5.10 cas-...
统一身份认证-CAS配置实现 SSO单点登录Spring-Security+&+CAS+使用手册 统一身份认证-CAS配置实现 CAS登录验证(密码MD5、SHA加密后_再进行Base64加密实现代码)_与Liferay的用户身份验证对应
cas 单点登录 据演示需求,用修改hosts 文件的方法添加域名最简单方便(这个非常重要,因为CAS单点登录系统是基于JAVA安全证书的 https 访问, 要使用CAS单点登录必须要配置域名, cas是不能通过ip访问的.),编辑...
单点登录CAS方案,同时提供CAS+HTTP+Oracle和CAS+HTTPS+Oracle两种方案,亲自测试OK,客户端修改方案自行百度,太简单
cas单点登陆的完整demo,java实现,支持https,包含cas服务器和2个客户端代码,解压即用,运行之前请阅读注意说明,记得添加证书至jdk信任,如果实在无法运行可以参考我的博客相关文章
NULL 博文链接:https://yuzhwe.iteye.com/blog/831892
配置CAS Server应用服务器的Https 协议 5 ●生成服务器证书 5 ●配置Tomcat的https 8 步骤2.部署CAS Server 8 ●扩展认证接口 8 ●JDBC 认证方法 11 五、部署CAS Client 15 步骤1.与 CAS Server 建立信任关系 15 ...