授权登录(授权登录在用户访问时会提示用户登录才可以获取到用户openid)
1.获取Code
@RequestMapping(value = "/wxLogin")
public void wxLogin(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String backUrl = "baidu.com";
String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +APPID+
"&redirect_uri=" +"获取用户信息url"+
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE#wechat_redirect";
resp.sendRedirect("获取用户信息url");
}
2.获取用户信息
//保存用户微信登录信息
@ResponseBody
@RequestMapping(value = "/index")
public void login(HttpServletRequest req,HttpServletRequest request, HttpServletResponse resp,HttpSession session) throws JSONException, IOException{
String code=req.getParameter("code");
if(code!=null) {
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);
session.setAttribute("openid", userInfo.getString("openid"));
System.out.println("infoUrl3"+userInfo);
int subscribe = userInfo.getInt("subscribe");
if(subscribe == 1) {
//获取用户国家
String country = (String)userInfo.get("country");
//获取用户城市
String city =(String)userInfo.get("city");
//获取用户性别
int sex =(int)userInfo.get("sex");
//获取用户名字
String nickname =(String)userInfo.get("nickname");
//获取用户头像
String headimgurl = (String)userInfo.get("headimgurl");
System.out.println("用户城市:"+country);
TbPersonnel tbPersonnel = tbPersonnelService.selectByID((String)userInfo.get("openid"));
if(tbPersonnel==null) {
TbPersonnel tbp = new TbPersonnel();
tbp.setId((String)userInfo.get("openid"));
tbp.setName(nickname);
tbp.setCreateTime(new Date());
tbp.setHeadImg(headimgurl);
tbPersonnelService.insertPersonnel(tbp);
session.setAttribute("Personnel", tbp);
}else {
session.setAttribute("Personnel", tbPersonnel);
}
}
resp.sendRedirect("登录后跳转主页");
}
}
静默登录(用户不用授权登录可获取openid,公众号为测试账号会提示关注错误)
1.获取code
@RequestMapping(value = "/wxLogin")
public void wxLogin(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="你的appid"&redirect_uri="你的回调址"&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
resp.sendRedirect("你的回调地址");
}
2.获取用户登录信息(未关注公众号只会回去openid,不会获取到用户信息)
@ResponseBody
@RequestMapping(value = "/login")
public void login(HttpServletRequest req,HttpServletRequest request, HttpServletResponse resp,HttpSession session) throws JSONException, IOException{
String code=req.getParameter("code");
System.out.println("微信Code:"+code);
if(code!=null) {
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");
//获取到token地址
String infoUrl2 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+AuthUtil.APPID+"&secret="+AuthUtil.APPSECRET;
JSONObject access_token=AuthUtil.doGetJson(infoUrl2);
String accesstoken = access_token.getString("access_token");
//https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN获取用户信息
String infoUrl3 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accesstoken+"&openid="+openid+"&lang=zh_CN";
JSONObject userInfo=AuthUtil.doGetJson(infoUrl3);
System.out.println("====openid:"+userInfo.getString("openid"));
session.setAttribute("openid", userInfo.getString("openid"));
System.out.println("infoUrl3"+userInfo);
int subscribe = userInfo.getInt("subscribe");
if(subscribe == 1) {
//获取用户国家
String country = (String)userInfo.get("country");
//获取用户城市
String city =(String)userInfo.get("city");
//获取用户性别
int sex =(int)userInfo.get("sex");
//获取用户名字
String nickname =(String)userInfo.get("nickname");
//获取用户头像
String headimgurl = (String)userInfo.get("headimgurl");
System.out.println("用户城市:"+country);
TbPersonnel tbPersonnel = tbPersonnelService.selectByID((String)userInfo.get("openid"));
if(tbPersonnel==null) {
TbPersonnel tbp = new TbPersonnel();
tbp.setId((String)userInfo.get("openid"));
tbp.setName(nickname);
tbp.setCreateTime(new Date());
tbp.setHeadImg(headimgurl);
tbPersonnelService.insertPersonnel(tbp);
session.setAttribute("Personnel", tbp);
}else {
session.setAttribute("Personnel", tbPersonnel);
}
}
resp.sendRedirect("http://a6d5d226.nat.nsloop.com/");
}
3.工具类
public class AuthUtil {
public static final String APPID="你的appid";
public static final String APPSECRET="你的APPSECRET";
public static JSONObject doGetJson(String url) throws IOException {
JSONObject jsonObject=null;
DefaultHttpClient defaultHttpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
if(httpEntity!=null){
String result= EntityUtils.toString(httpEntity,"UTF-8");
jsonObject=new JSONObject(result);
System.out.println("jsonObject: "+jsonObject);
}
httpGet.releaseConnection();
return jsonObject;
}