将code作为参数传递过来
//如果有code,说明是微信小程序,根据code获取openId
//classify用于标识是哪个小程序
if (!CheckUtil.checkNulls( keUser.getCode(),keUser.getClassify())){
//
String openid = OpenIdUtil.oauth2GetOpenid(keUser.getCode(),keUser.getClassify())
printParamsLog(openid, logger)
keUser.setUserId(openid)
}1234567812345678
二、工具类
package com.util
import net.sf.json.JSONObject
import org.apache.http.client.HttpClient
import org.apache.http.client.ResponseHandler
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.BasicResponseHandler
import org.apache.http.impl.client.DefaultHttpClient
import java.util.HashMap
import java.util.Map
/**
* @author xsx
*/
public class OpenIdUtil {
public static String oauth2GetOpenid(String code,String classify) {
String appid=""
String appsecret=""
switch (classify){
case "1":
//自己的配置appid
appid = "**********"
//自己的配置APPSECRET
appsecret = "**********"
break
case "2":
appid = "**********"
appsecret = "************"
break
case "3":
appid = "**********"
appsecret = "************"
break
case "4":
appid = "**********"
appsecret = "************"
break
case "5":
appid = "**********"
appsecret = "************"
}
//授权(必填)
String grant_type = "authorization_code"
//URL
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"
//请求参数
String params = "appid=" + appid + "&secret=" + appsecret + "&js_code=" + code + "&grant_type=" + grant_type
//发送请求
String data = HttpUtil.get(requestUrl, params)
//解析相应内容(转换成json对象)
JSONObject json = JSONObject.fromObject(data)
//用户的唯一标识(openid)
String Openid =String.valueOf(json.get("openid"))
//System.out.println(Openid)
return Openid
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
三、发送请求的工具类
package com.util
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL
import java.net.URLConnection
import java.util.List
import java.util.Map
/**
* @author xsx
*/
public class HttpUtil {
/**
* 向指定URL发送GET方法的请求
*
* @param url
*发送请求的URL
* @param param
*请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return String 所代表远程资源的响应结果
*/
public static String get(String url,String param){
String result = ""
BufferedReader in = null
try {
String urlNameString = url + "?" + param
//System.out.println(urlNameString)
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 (compatibleMSIE 6.0Windows NT 5.1SV1)")
// 建立实际的连接
connection.connect()
// 获取所有响应头字段
Map<String, List<String>>map = connection.getHeaderFields()
// 遍历所有的响应头字段
/*for (String key : map.keySet()) {
System.out.println(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
}
}
具体是报什么错误呢?一般解密报错的情况有,输入不满足16的倍数个字节,这种情况会报,AES解密异常Given final block not properly padded,具体参考:http://www.it399.com/blog/web/201805211406 可以使用这个在线工具解密,如果报错会有相应的错误提示,希望能够帮助到你。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)