获取微信用户OpenID

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zengsange/article/details/75305642
功能需求:获取用户openid ,实现微信公众号自动登录.
实现思路:用户访问公众号链接,获取到code,用code换取用户openid,将openid保存到session,每次登陆判断openid是否绑定用户,如果已经绑定了用户则直接登陆,如果openid没有绑定用户则进行登陆,用户登陆系统后 将用户ID和openid绑定到一起,
一 获取用户openid
1.调用微信授权接口获取code
weixin_code.url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx039fa5e93cf51b8e&redirect_uri=http%3a%2f%2ffinance.ngrok.cc%2fFussenFinanceLoanWeb%2fmicroLetter%2floginPhone&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
appid:公众号基本配置里 开发者ID(AppID),
redirect_uri:项目接收code  的方法,
代码如下:
 @RequestMapping(value="/loginPhone" )
    public String loginPhone(HttpServletRequest request,HttpServletResponse response,
     String code,HttpSession httpSession,Model model){
 Map<String,Object> open = new HashMap<String,Object>();
try {
String openID= null; //取微信openID
if(request.getSession().getAttribute("openID")== null){
 openID = MicroLetterController.queryOpenID(code);
}else{
openID = (String) request.getSession().getAttribute("openID");
}
open.put("openID", openID);
 UserInfoVO user = null;
 user= userInfoService.queryOpenID(open); //通过微信ID查询是否已经绑定
 if(user!=null){
//保存用户信息
httpSession.setAttribute(SessionConstants.LOGIN_SESSION_ID, user);
//重定向到查询菜单权限
String WEBPATH = "http://" + request.getHeader("Host") + "" + "/FussenFinanceLoanWeb";
return "redirect:"+WEBPATH+"/menuRole/queryMenuRole";
 }
 model.addAttribute("openID", openID);
 httpSession.setAttribute("openID", openID); //微信ID
} catch (Exception e) {
e.printStackTrace();
}
 return "phone/optloan/login/login_phone"; //跳转到登录页面
    }


2.用code换取openid
weixin_openid.url=https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx039fa5e93cf51b8e&secret=e2fa4c2a05ce99575e6c139a5c37de1c&grant_type=authorization_code&code=code
appid:公众号基本配置里 开发者ID (AppID),
secret公众号基本配置里 开发者密码 (AppSecret),
code:调第一个接口获取,
代码如下:

        /**
         * 调用微信接口取openID
         * @param token
         * @param code
         * @return
         * @throws ParseException
         * @throws IOException
        * @throws InterruptedException 
         */
public static String queryOpenID(String code) throws ParseException, IOException, InterruptedException{
String openiD = null;
String url ;
     url = PropertiesHandler.getConfigValue("weixin_openid.url").toString();
     url =url+code;
JSONObject jsonObject = doGetStr(url);
if(jsonObject != null){
if(null!=jsonObject.getString("openid")){
openiD = jsonObject.getString("openid");
}
}
return openiD;
}
get和post方法
/**
 * get请求
 * @param url        
 * @return
 * @throws ParseException
 * @throws IOException
 */
public static JSONObject doGetStr(String url) throws ParseException, IOException{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
HttpResponse httpResponse = client.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
if(entity != null){
String result = EntityUtils.toString(entity,"UTF-8");
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}
/**
 * POST请求
 * @param url
 * @param outStr
 * @return
 * @throws ParseException
 * @throws IOException
 */
public static JSONObject doPostStr(String url,String outStr) throws ParseException, IOException{
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
JSONObject jsonObject = null;
httpost.setEntity(new StringEntity(outStr,"UTF-8"));
HttpResponse response = client.execute(httpost);
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
jsonObject = JSONObject.fromObject(result);
return jsonObject;
}
到此获取openid结束!!!!!!
流程图

展开阅读全文

没有更多推荐了,返回首页