微信小程序登陆--java

首先要获取小程序的Appid,app secret,wxspSecret,授权:authorization_code

后台java代码:

//需要小程序端传入参数:code,rawData,signature,encryptedData,iv
// 小程序唯一标识 (在微信小程序管理后台获取)  
String wxspAppid = "";  
// 小程序的 app secret (在微信小程序管理后台获取)   

String wxspSecret = "";  
// 授权(必填)  
String grant_type = "authorization_code";  

Map map = new HashMap(  );
System.out.println("用户非敏感信息"+rawData);

JSONObject rawDataJson = JSON.parseObject( rawData );

System.out.println("签名"+signature);

/*通过请求code获取sessionkey和openid*/
// 请求参数  
String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" + code + "&grant_type="  
		+ grant_type;  
String sr = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session", params);
JSONObject SessionKeyOpenId = JSON.parseObject(sr);
System.out.println("post请求获取的SessionAndopenId="+SessionKeyOpenId);
Map myinfos = SessionKeyOpenId; 

//如果出错,返回错误信息
if(SessionKeyOpenId.containsKey("errcode")){
	map.put("userInfo", SessionKeyOpenId);
	return map;
}
String openid = SessionKeyOpenId.getString("openid" );
String sessionKey = SessionKeyOpenId.getString( "session_key" );
System.out.println("openid="+openid+",session_key="+sessionKey);
//uuid生成唯一key
String skey = UUID.randomUUID().toString();
System.out.println("随机生成的skey"+skey);

/*操作数据库代码--略*/

//把新的sessionKey和oppenid返回给小程序
map.put( "skey",skey );

/**
 * 对encryptedData加密数据进行AES解密
 */
try {  
	String result = AesCbcUtil.decrypt(encryptedData, sessionKey, iv, "UTF-8");  
	if (null != result && result.length() > 0) {  
		map.put("status", 1);  
		map.put("msg", "解密成功");  

		JSONObject userInfoJSON = JSON.parseObject(result);  
		/*Map userInfo = new HashMap();  
		userInfo.put("openId", userInfoJSON.get("openId"));  
		userInfo.put("nickName", userInfoJSON.get("nickName"));  
		userInfo.put("gender", userInfoJSON.get("gender"));  
		userInfo.put("city", userInfoJSON.get("city"));  
		userInfo.put("province", userInfoJSON.get("province"));  
		userInfo.put("country", userInfoJSON.get("country"));  
		userInfo.put("avatarUrl", userInfoJSON.get("avatarUrl"));  
		// 解密unionId & openId;  
		userInfo.put("unionId", userInfoJSON.get("unionId"));*/
		map.put("userInfo", userInfoJSON);  
	} else {  
		map.put("status", 0);  
		map.put("msg", "解密失败");  
	}  
} catch (Exception e) {  
	e.printStackTrace();  
} 
/** 
 * 向指定URL发送GET方法的请求 
 *  
 * @param url 
 *            发送请求的URL 
 * @param param 
 *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 
 * @return URL 所代表远程资源的响应结果 
 */  
public static String sendGet(String url, String param) {  
	String result = "";  
	BufferedReader in = null;  
	try {  
		String urlNameString = url + "?" + param;  
		URL realUrl = new URL(urlNameString);  
		// 打开和URL之间的连接  
		URLConnection connection = realUrl.openConnection();  
		// 设置通用的请求属性  
		connection.setRequestProperty("accept", "*/*");  
		connection.setRequestProperty("connection", "Keep-Alive");  
		connection.setRequestProperty("user-agent",  
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
		// 建立实际的连接  
		connection.connect();  
		// 获取所有响应头字段  
		Map> map = connection.getHeaderFields();  
		// 遍历所有的响应头字段  
		for (String key : map.keySet()) {  
			System.out.println(key + "key:--------------->" + map.get(key));  
		}  
		// 定义 BufferedReader输入流来读取URL的响应  
		in = new BufferedReader(new InputStreamReader(  
				connection.getInputStream()));  
		String line;  
		while ((line = in.readLine()) != null) {  
			result += line;  
		}  
	} catch (Exception e) {  
		System.out.println("发送GET请求出现异常!" + e);  
		e.printStackTrace();  
	}  
	// 使用finally块来关闭输入流  
	finally {  
		try {  
			if (in != null) {  
				in.close();  
			}  
		} catch (Exception e2) {  
			e2.printStackTrace();  
		}  
	}  
	return result;  
}  

/**  
 * 新增方法 
 * 向指定 URL 发送POST方法的请求  
 *   
 * @param url  
 *            发送请求的 URL  
 * @param param  
 *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。  
 * @return 所代表远程资源的响应结果  
 */  
public static String sendPost(String url, String param) {  
	PrintWriter out = null;  
	BufferedReader in = null;  
	String result = "";  
	try {  
		URL realUrl = new URL(url);  
		// 打开和URL之间的连接  
		URLConnection conn = realUrl.openConnection();  
		// 设置通用的请求属性  
		conn.setRequestProperty("accept", "*/*");  
		conn.setRequestProperty("connection", "Keep-Alive");  
		conn.setRequestProperty("user-agent",  
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
		// 发送POST请求必须设置如下两行  
		conn.setDoOutput(true);  
		conn.setDoInput(true);  
		// 获取URLConnection对象对应的输出流  
		out = new PrintWriter(conn.getOutputStream());  
		// 发送请求参数  
		out.print(param);  
		// flush输出流的缓冲  
		out.flush();  
		// 定义BufferedReader输入流来读取URL的响应  
		in = new BufferedReader(  
				new InputStreamReader(conn.getInputStream()));  
		String line;  
		while ((line = in.readLine()) != null) {  
			result += line;  
		}  
	} catch (Exception e) {  
		System.out.println("发送 POST 请求出现异常!"+e);  
		e.printStackTrace();  
	}  
	//使用finally块来关闭输出流、输入流  
	finally{  
		try{  
			if(out!=null){  
				out.close();  
			}  
			if(in!=null){  
				in.close();  
			}  
		}  
		catch(IOException ex){  
			ex.printStackTrace();  
		}  
	}  
	return result;  
} 

 

你可能感兴趣的:(微信/小程序-java)