当前位置:首页 > 开发 > 软件应用 > 正文

[转]Java模拟登录新浪微博

发表于: 2014-06-13   作者:alleni123   来源:转载   浏览:
摘要: 登录的类主要有3个,BigIntegerRSA.java加密类、SinaSSOEncoder密码加密类、SinaLogonDog登录类。 1、SinaLogonDog.java代码如下: package com.crawler.sina.login; import java.io.ByteArrayOutputStream; import java.io.IOException;
登录的类主要有3个,BigIntegerRSA.java加密类、SinaSSOEncoder密码加密类、SinaLogonDog登录类。
1、SinaLogonDog.java代码如下:
package com.crawler.sina.login;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Date;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author TonyJ
 * @Time 2013-7-1 上午02:08:49
 * 
 */
public class SinaLogonDog {
	
	private static final Log logger = LogFactory.getLog(SinaLogonDog.class);
	

//http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.15)&_=1403086131206
//这个public key是通过上面的地址请求获取的。
	static String SINA_PK = "EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D24"
		+ "5A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD39"
		+ "93CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE"
		+ "1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443";
	
	MultiThreadedHttpConnectionManager connectionManager =new MultiThreadedHttpConnectionManager();
	HttpClient client = new HttpClient(connectionManager);  
	
	public HttpClient logonAndValidate(String userName,String pwd) throws HttpException, IOException {
		String su = encodeAccount(userName);
		String initPageURL = "http://login.sina.com.cn/sso/prelogin.php?entry=sso&"
				+ "callback=sinaSSOController.preloginCallBack&su=" + su
				+ "&rsakt=mod&client=ssologin.js(v1.4.5)" + "&_=" + getServerTime();
		GetMethod getMethod = newGetMethod(initPageURL);

		client.executeMethod(getMethod);
		InputStream is = getMethod.getResponseBodyAsStream();

		String response = inputStreamToString(is, null);

		String jsonBody = StringUtils.substringBetween(response, "(", ")");

		String nonce = "";
		long servertime = 0L;
		String rsakv = "";
		nonce = StringUtils.substringBetween(jsonBody, "nonce\":\"", "\"");
		rsakv = StringUtils.substringBetween(jsonBody, "rsakv\":\"", "\"");
		servertime = Long.parseLong(StringUtils.substringBetween(jsonBody, "servertime\":", ","));

		getMethod.releaseConnection();

		String logonPageURL = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)";
		PostMethod postMethod2Logon = newPostMethod(logonPageURL);
		String pwdString = servertime + "\t" + nonce + "\n" + pwd;
		String sp = "";
		try {
			sp = new BigIntegerRSA().rsaCrypt(SINA_PK, "10001", pwdString);
		} catch (InvalidKeyException e) {
			logger.error("AES加密密钥大于128!", e);
		} catch (IllegalBlockSizeException e) {
			logger.error(e);
		} catch (BadPaddingException e) {
			logger.error(e);
		} catch (NoSuchAlgorithmException e) {
			logger.error(e);
		} catch (InvalidKeySpecException e) {
			logger.error(e);
		} catch (NoSuchPaddingException e) {
			logger.error(e);
		}
		// SinaSSOEncoder sso = new SinaSSOEncoder();
		// String sp = sso.encode(account.getPasswd(), servertime, nonce);
		logger.info("su=" + su);
		logger.info("servertime=" + servertime);
		logger.info("nonce=" + nonce);
		logger.info("sp=" + sp);
		logger.info("su=" + su + "&servertime=" + servertime + "&nonce=" + nonce + "&sp=" + sp);
		postMethod2Logon
				.setRequestBody(new NameValuePair[] {
						new NameValuePair("entry", "weibo"),
						new NameValuePair("gateway", "1"),
						new NameValuePair("from", ""),
						new NameValuePair("savestate", "7"),
						new NameValuePair("useticket", "1"),
						new NameValuePair("ssosimplelogin", "1"),
						new NameValuePair("useticket", "1"),
						new NameValuePair("vsnf", "1"),
						new NameValuePair("vsnval", ""),
						new NameValuePair("su", su),
						new NameValuePair("service", "miniblog"),
						new NameValuePair("servertime", servertime + ""),
						new NameValuePair("nonce", nonce),
						new NameValuePair("pwencode", "rsa2"),
						new NameValuePair("rsakv", rsakv),
						new NameValuePair("sp", sp),
						new NameValuePair("encoding", "UTF-8"),
						new NameValuePair("prelt", "115"),
						new NameValuePair("url",
								"http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"),
						new NameValuePair("returntype", "META") });
		int statusCode = client.executeMethod(postMethod2Logon);
		if (statusCode == HttpStatus.SC_OK) {
			System.out.println("ok");
		}
		postMethod2Logon.releaseConnection();

		logger.info(postMethod2Logon.getResponseHeaders());
		// 验证是否登录成功
		Cookie cookie = new Cookie("weibo.com", "wvr", "3.6", "/", new Date(2099, 12, 31), false);
		client.getState().addCookie(cookie);

		Cookie[] cookies = client.getState().getCookies();
		boolean containsSue = false;
		boolean containsSup = false;
		for (Cookie singleCookie : cookies) {
			String domain = singleCookie.getDomain();
			logger.info(domain);
			if (domain.equals(".sina.com.cn")) {
				singleCookie.setDomain("weibo.com");
			}
			logger.info(singleCookie);
			if (singleCookie.getName().equals("SUE")) {
				containsSue = true;
			}
			if (singleCookie.getName().equals("SUP")) {
				containsSup = true;
			}
		}
		return client;
	}

	
	public void init(){
		MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
		client = new HttpClient(connectionManager);
		client.getParams().setCookiePolicy("compatibility");
		client.getParams().setParameter("http.protocol.single-cookie-header",
				Boolean.valueOf(true));
		client.getParams().setSoTimeout(20000);
		client.getHttpConnectionManager().getParams().setConnectionTimeout(
				20000);
		
	}
	
	// inputstream转化为String类型
	public static String inputStreamToString(InputStream is, String charset) throws IOException {
		int i = -1;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((i = is.read()) != -1) {
			baos.write(i);
		}
		if (null == charset) {
			return baos.toString();
		} else {
			return baos.toString(charset);
		}
	}

	protected PostMethod newPostMethod(String url) {
		PostMethod postMethod = new PostMethod(url);
		postMethod
				.setRequestHeader(
						"Accept",
						"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		postMethod.setRequestHeader("Referer", "http://weibo.com/");
		postMethod.setRequestHeader("Accept-Language", "zh-cn");
		postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate");
		postMethod
				.setRequestHeader(
						"User-Agent",
						"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; InfoPath.2)");
		postMethod.setRequestHeader("Connection", "Keep-Alive");
		postMethod.setRequestHeader("Cache-Control", "no-cache");
		return postMethod;
	}

	public static String decode(String string) {
		return string.replaceAll("%3A", ":").replaceAll("%2F", "/").replaceAll("%3D", "=")
				.replaceAll("%26", "&");
	}

	// 用户名编码
	@SuppressWarnings("deprecation")
	private String encodeAccount(String account) {
		return (new sun.misc.BASE64Encoder()).encode(URLEncoder.encode(account).getBytes());
	}

	// 六位随机数nonce的产生(不用)
	public String makeNonce(int len) {
		String x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String str = "";
		for (int i = 0; i < len; i++) {
			str += x.charAt((int) (Math.ceil(Math.random() * 1000000) % x.length()));
		}
		return str;
	}

	// servertime的产生(不用)
	public String getServerTime() {
		long servertime = new Date().getTime() / 1000;
		return String.valueOf(servertime);
	}
	
	protected GetMethod newGetMethod(String url) {
		GetMethod getMethod = new GetMethod(url);
		getMethod.setRequestHeader("Accept","image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		getMethod.setRequestHeader("Accept-Language", "zh-CN");
		getMethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
		getMethod.setRequestHeader("Accept-Encoding", "deflate");
		getMethod.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; CIBA; MAXTHON 2.0)");
		getMethod.setRequestHeader("Connection", "Keep-Alive");
		getMethod.setRequestHeader("Cache-Control", "no-cache");
		return getMethod;
	}
}





2、BigIntegerRSA.java相关的加密算法类

package com.crawler.sina.login;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.apache.commons.codec.binary.Hex;

/**
 * @author TonyJ
 * @Time 2013-7-1 上午02:12:46
 * 
 */
public class BigIntegerRSA {
	
	public String rsaCrypt(String modeHex, String exponentHex, String messageg)
			throws IllegalBlockSizeException, BadPaddingException,
			NoSuchAlgorithmException, InvalidKeySpecException,
			NoSuchPaddingException, InvalidKeyException,
			UnsupportedEncodingException {
		KeyFactory factory = KeyFactory.getInstance("RSA");

		BigInteger m = new BigInteger(modeHex, 16); /* public exponent */
		BigInteger e = new BigInteger(exponentHex, 16); /* modulus */
		RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);

		RSAPublicKey pub = (RSAPublicKey) factory.generatePublic(spec);
		Cipher enc = Cipher.getInstance("RSA");
		enc.init(Cipher.ENCRYPT_MODE, pub);

		byte[] encryptedContentKey = enc.doFinal(messageg.getBytes("GB2312"));

		return new String(Hex.encodeHex(encryptedContentKey));
	}
}




3、SinaSSOEncoder.java密码加密的类


package com.crawler.sina.login;

public class SinaSSOEncoder {
	  private boolean i=false;

	    private int g=8;

	    public SinaSSOEncoder(){

	    }

	    public String encode(String psw,String servertime,String nonce){
	        String password;
	        password=hex_sha1(""+hex_sha1(hex_sha1(psw))+servertime+nonce);
	        return password;
	    }
	    private String hex_sha1(String j) {
	        return h(b(f(j,j.length()*g), j.length() * g));
	    }

	    private String h(int[] l){
	        String k = i ? "0123456789ABCDEF" : "0123456789abcdef";
	        String m = "";
	        for (int j = 0; j < l.length * 4; j++) {
	            m += k.charAt((l[j >> 2] >> ((3 - j % 4) * 8 + 4)) & 15) + "" + k.charAt((l[j >> 2] >> ((3 - j % 4) * 8)) & 15);
	        }
	        return m;
	    }

	    private int[] b(int[] A,int r){
	        A[r>>5]|=128<<(24-r%32);
	        A[((r+64>>9)<<4)+15]=r;
	        int[] B = new int[80];
	        int z = 1732584193;
	        int y = -271733879;
	        int v = -1732584194;
	        int u = 271733878;
	        int s = -1009589776;
	        for (int o = 0; o < A.length; o += 16) {
	            int q = z;
	            int p = y;
	            int n = v;
	            int m = u;
	            int k = s;
	            for (int l = 0; l < 80; l++) {
	                if (l < 16) {
	                    B[l] = A[o + l];
	                } else {
	                    B[l] = d(B[l - 3] ^ B[l - 8] ^ B[l - 14] ^ B[l - 16], 1);
	                }
	                int C = e(e(d(z, 5), a(l, y, v, u)), e(e(s, B[l]), c(l)));
	                s = u;
	                u = v;
	                v = d(y, 30);
	                y = z;
	                z = C;
	            }
	            z = e(z, q);
	            y = e(y, p);
	            v = e(v, n);
	            u = e(u, m);
	            s = e(s, k);
	        }
	        return new int[]{z,y,v,u,s};
	    }

	    private int a(int k,int j,int m,int l){
	        if(k<20){return(j&m)|((~j)&l);};
	        if(k<40){return j^m^l;};
	        if(k<60){return(j&m)|(j&l)|(m&l);};
	        return j^m^l;
	    }

	    private int c(int j){
	        return(j<20)?1518500249:(j<40)?1859775393:(j<60)?-1894007588:-899497514;
	    }

	    private int e(int j, int m) {
	        int l = (j & 65535) + (m & 65535);
	        int k = (j >> 16) + (m >> 16) + (l >> 16);
	        return (k << 16) | (l & 65535);
	    }

	    private int d(int j,int k){
	        return(j<<k)|(j>>>(32-k));
	    }

	    private int[] f(String m,int r){
	        int[] l;
	        int j = (1<<this.g)-1;
	        int len=((r+64>>9)<<4)+15;
	        int k;
	        for(k=0;k<m.length()*g;k+=g){
	            len = k>>5>len?k>>5:len;
	        }
	        l = new int[len+1];
	        for(k=0;k<l.length;k++){
	            l[k]=0;
	        }
	        for(k=0;k<m.length()*g;k+=g){
	            l[k>>5]|=(m.charAt(k/g)&j)<<(24-k%32);
	        }
	        return l;
	    }
}

[转]Java模拟登录新浪微博

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号