zoukankan      html  css  js  c++  java
  • 第三方授权方式1

    实现微信授权登录

    首先按照官方文档的步骤

    1 第一步:用户同意授权,获取code

    2 第二步:通过code换取网页授权access_token

    3 第三步:刷新access_token(如果需要)

    4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

    @WebServlet("/wxLogin")
    public class LoginServlet extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String backUrl = "http://url/WeixinTest/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";
            System.out.println("================");
            response.sendRedirect(url);
        }
    }
    @WebServlet("/callBack")
    public class CallBackServlet extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String code = request.getParameter("code");
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
                    + "appid="+AuthUtil.APPID
                    + "&secret="+AuthUtil.APPSECET
                    + "&code="+code
                    + "&grant_type=authorization_code";
            JSONObject jsonObject = AuthUtil.doGetJson(url);
            String openid = jsonObject.getString("openid");
            String token = jsonObject.getString("access_token");
            String tourl = "https://api.weixin.qq.com/sns/userinfo?"
                    + "access_token="+token
                    + "&openid="+openid
                    + "&lang=zh_CN";
            JSONObject userInfo = AuthUtil.doGetJson(tourl);
            System.out.println(userInfo);
        }
    }

    包装的util

    public class AuthUtil {
        public static final String  APPID = "id";
        public static final String APPSECET = "secet";
        
        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;
        }
    }

     大家觉得不错的话可以支持一下

  • 相关阅读:
    易语言破解与安装
    用 AS3.0 的 fscommand 命令调用 .exe 文件。
    swf批量导出
    pureMVC java版搭建流程
    PureMVC 框架总结收录
    一些算法
    练习3.34
    关于数组的注意事项
    练习3.30、3.33
    练习3.27、3.28、3.29
  • 原文地址:https://www.cnblogs.com/xiangxiang521/p/9392105.html
Copyright © 2011-2022 走看看