import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class AuthUtil {
public static final String APPID = "wx*******b1";//自己的微信APPID
public static final String APPSECRET = "dbf**********b90e2e8";//自己的微信APPSECRET
public static JSONObject doGetJson(String URL) throws IOException {
JSONObject jsonObject = null;
HttpURLConnection conn = null;
InputStream is = null;
BufferedReader br = null;
StringBuilder result = new StringBuilder();
try {
//创建远程url连接对象
URL url = new URL(URL);
//通过远程url连接对象打开一个连接,强转成HTTPURLConnection类
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
//设置连接超时时间和读取超时时间
conn.setConnectTimeout(15000);
conn.setReadTimeout(60000);
conn.setRequestProperty("Accept", "application/json");
//发送请求
conn.connect();
//通过conn取得输入流,并使用Reader读取
if (200 == conn.getResponseCode()) {
is = conn.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
result.append(line);
System.out.println(line);
}
} else {
System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (is != null) {
is.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
conn.disconnect();
}
jsonObject = JSONObject.parseObject(result.toString());
return jsonObject;
}
}
工具类
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.monitor.util.AuthUtil;
@RestController
public class controlle {
@RequestMapping("loginPage")
public String index(HttpServletResponse response) throws IOException {
//请求获取code的回调地址
//用线上环境的域名或者用内网穿透,不能用ip
String callBack = "http://a********y.net/wxAuth/callBack";
//请求地址
String url = "https://open.weixin.qq.com/connect/oauth2/authorize" +
"?appid=" + AuthUtil.APPID +
"&redirect_uri=" + URLEncoder.encode(callBack) +
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE#wechat_redirect";
//重定向
//response.sendRedirect(url);
return url;
}
@RequestMapping("/wxAuth/callBack")
public void wxCallBack(HttpServletRequest request,HttpServletResponse response) throws IOException {
String code = request.getParameter("code");
//获取access_token
String url = "https://api.weixin.qq.com/sns/oauth2/access_token" +
"?appid=" + AuthUtil.APPID +
"&secret=" + AuthUtil.APPSECRET +
"&code=" + code +
"&grant_type=authorization_code";
JSONObject jsonObject = AuthUtil.doGetJson(url);
String openid = jsonObject.getString("openid");
String token = jsonObject.getString("access_token");
System.out.println("请求获取access_token:" + token);
//4. 获取用户信息
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?"
+ "access_token=" + token
+ "&openid=" + openid
+ "&lang=zh_CN";
JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
System.out.println(userInfo);
request.setAttribute("userInfo", userInfo);
//request.getRequestDispatcher("success.jsp").forward(request, response);
//此时已获取到userInfo,再根据业务进行处理
System.out.println("请求获取userInfo:" + userInfo);
}
}