获取微信用户OpenID

功能需求:获取用户openid ,实现微信公众号自动登录.
实现思路:用户访问公众号链接,获取到co de,用co de换取用户openid,将openid保存到session,每次登陆判断openid是否绑定用户,如果已经绑定了用户则直接登陆,如果openid没有绑定用户则进行登陆,用户登陆系统后 将用户ID和openid绑定到一起,
一 获取用户openid
1.调用微信授权接口获取co de
weixin_co de.url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx039fa5e93cf51b8e&redirect_uri=http%3a%2f%2ffinance.ngrok.cc%2fFussenFinanceLoanWeb%2fmicroLetter%2floginPhone&response_type=co de&scope=snsapi_base&state=STATE#wechat_redirect
appid:公众号基本配置里  开发者ID (AppID),
redirect_uri:项目接收co de  的方法,
代码如下:
 @RequestMapping(value="/loginPhone" )
    public String loginPhone(HttpServletRequest request,HttpServletResponse response,
     String co de,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(co de);
}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.用co de换取openid
weixin_openid.url=https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx039fa5e93cf51b8e&secret=e2fa4c2a05ce99575e6c139a5c37de1c&grant_type=authorization_co de&co de=co de
appid:公众号基本配置里  开发者ID  (AppID),
secret公众号基本配置里  开发者密码  (AppSecret),
code:调第一个接口获取,
代码如下:

        /**
         * 调用微信接口取openID
         * @param token
         * @param co de
         * @return
         * @throws ParseException
         * @throws IOException
        * @throws InterruptedException 
         */
public static String queryOpenID(String co de) throws ParseException, IOException, InterruptedException{
String openiD = null;
String url ;
     url = PropertiesHandler.getConfigValue("weixin_openid.url").toString();
     url =url+co de;
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结束!!!!!!
流程图

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zengsange

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值