小程序后台获取openId解密的java代码怎么写

小程序后台获取openId解密的java代码怎么写,第1张

一、获取code

将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

}

}

public class Test0 {

public static void main(String[] args) {

Employee ZhangSan = new Employee("001", "张三", '男', "销售部", 6000, 1000, "普通员工斗念稿")

Employee LiSi = new Employee("002", "李四", '女', "人事部", 7000, 2000, "超级员工")

System.out.println(ZhangSan.toString())//打印张三信息

System.out.println(LiSi.toString())//打印李四信息

}

}

class Employee{

private String id//员工ID

private String name//姓名

private char sex//高歼性别

private String department //部门

private int basic_salary //基本工资

private int extra_salary //薪金

private String classify //类别

/**

* 构造方法

*/

public Employee(String id, String name, char sex, String department,

int basic_salary, int extra_salary, String classify) {

this.id = id

this.name = name

this.sex = sex

this.department = department

this.basic_salary = basic_salary

this.extra_salary = extra_salary

this.classify = classify

}

public Employee(){

}

public String getId() {

return id

}

public void setId(String id) {

this.id = id

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public char getSex() {

return sex

}

public void setSex(char sex) {

this.sex = sex

}

public String getDepartment() {

return department

}

public void setDepartment(String department) {

this.department = department

}

public int getBasic_salary() {

return basic_salary

}

public void setBasic_salary(int basic_salary) {

this.basic_salary = basic_salary

}

public int getExtra_salary() {

return extra_salary

}

public void setExtra_salary(int extra_salary) {

this.extra_salary = extra_salary

}

public String getClassify() {

return classify

}

public void setClassify(String classify) {

this.classify = classify

}

public String toString(){

String str ="[员工ID:"+id+"|姓名:"+name+"|性别:"+sex+

"|部门:"+department+"|基本工资空孝:+"+basic_salary+

"|薪金:"+extra_salary+"|类别:"+classify+"]"

return str

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12300191.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存