微信授权
回调域名需要和公众号后台设置一致,否则微信回调请求不到
@RequestMapping("/wxLogin")
public String wxLogin() throws IOException {
//域名
String sym = "http://www.baidu.com";
//这里是回调的url
String redirectUri = URLEncoder.encode(sys + "/api/callBack", "UTF-8");
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=" +
微信商户的appid +
"&redirect_uri=" + redirectUri +
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE" +
"#wechat_redirect";
//可以替换STATE携带需要的参数到微信回调的接口里
//JSONObject jsonObject = new JSONObject();
//jsonObject.put("A",需要携带的参数);
//String s = jsonObject.toString();
//url = url.replace("STATE", URLEncoder.encode(s, "UTF-8"));
return url;
}
微信授权回调
@RequestMapping("callBack")
public String callBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
//获取回调地址中的code
String code = request.getParameter("code");
String state = request.getParameter("state");
//从state里拿到携带来的参数
//JSONObject object1 = JSONObject.parseObject(state);
//String A = object1.getString("A");
String appId = "xxxxxxxxxxx";
//开发者密码
String secret = "xxxxxxxxxxxxxxxxxxxxxxxx";
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=" + appId +
"&secret=" + secret +
"&code=" + code +
"&grant_type=authorization_code";
//获取openId
String data = HttpUtils.doGet(url);
AccessToken accessToken = JSONObject.parseObject(data, AccessToken.class);
return accessToken.getOpenid();
}
工具类
public class HttpUtils {
public static String doGet(String httpurl) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(httpurl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.connect();
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return result;
}
public static String doPost(String httpUrl, String param) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json");
os = connection.getOutputStream();
os.write(param.getBytes());
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return result;
}
}
public class AccessToken {
private String accessToken;
private String expiresIn;
private String refreshToken;
private String openid;
private String scope;
}