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;
        }
    }

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

  • 相关阅读:
    python2代码改成python3踩过的坑
    Mac下为什么有的文件名后带一个* 星号?
    Mac 的 Vim 中 delete 键失效的原因和解决方案(转)
    使用pandas处理大型CSV文件(转)
    Java基础——02
    javaee相关基础
    Cookie&Session笔记
    EL&JSTL笔记------jsp
    JavaWeb基础
    Java基础——01
  • 原文地址:https://www.cnblogs.com/xiangxiang521/p/9392105.html
Copyright © 2011-2022 走看看