JAVA實現微信授權登入
阿新 • • 發佈:2019-01-03
1.先寫一個工具類 AuthUtil.java 來存放APPID等資訊
public class AuthUtil { public static final String APPID = "換成自己的APPID "; public static final String APPSECRET = "換成自己的APPSECRET "; public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException { JSONObject jsonObject = null; DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.fromObject(result); } httpGet.releaseConnection(); return jsonObject; } }
2.新建一個 LoginController.java 類來實現授權登入
import java.io.IOException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.http.client.ClientProtocolException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import net.sf.json.JSONObject; @Controller public class LoginController extends HttpServlet { /** * 微信授權登入 */ @RequestMapping("/wxLogin") protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // backUrl 需要遵循微信官方的定義,微信的介面只能用 https 來訪問 // 所以我這裡是直接把整個專案打包成 jar 包,然後扔到自己的伺服器上 String backUrl = "https://www.yyzheng.cn/o2o/callBack"; String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AuthUtil.APPID + "&redirect_uri=" + URLEncoder.encode(backUrl) + "&response_type=code" + "&scope=snsapi_userinfo" + "&state=STATE#wechat_redirect"; response.sendRedirect(url); } /** * 登入成功的回撥函式 * * @param request * @param response * @throws ClientProtocolException * @throws IOException * @throws ServletException */ @RequestMapping("/callBack") protected void deGet(HttpServletRequest request, HttpServletResponse response) throws ClientProtocolException, IOException, ServletException { String code = request.getParameter("code"); 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"); String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openid + "&lang=zh_CN"; JSONObject userInfo = AuthUtil.doGetJson(infoUrl); if( userInfo != null ){ // 這裡是把授權成功後,獲取到的東西放到 info 裡面,前端可以通過 EL 表示式直接獲取相關資訊 request.setAttribute("info", userInfo); // 這裡是授權成功返回的頁面 request.getRequestDispatcher("/success.jsp").forward(request, response); }else{ request.getRequestDispatcher("/fail.jsp").forward(request, response); } } }
3.寫一個頁面來訪問授權功能
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1"> <title>Insert title here</title> </head> <body> <a href="/o2o/wxLogin">微信公眾授權登入</a> </body> </html>
4.微信授權成功訪問的頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<title>Insert title here</title>
</head>
<body>
授權成功!!!
${info }
</body>
</html>
5.微信授權失敗訪問的頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<title>Insert title here</title>
</head>
<body>
授權失敗!!!
</body>
</html>