APP微信登录---第三方登录

弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述APP微信登录---第三方登录相关的知识,希望能为你提供帮助。
(一)引入maven配置

< dependency> < groupId> com.github.liyiorg< /groupId> < artifactId> weixin-popular< /artifactId> < version> 2.8.5< /version> < /dependency>

【APP微信登录---第三方登录】(二)编写controller
  • 前端传递过来一个code字段,popular框架自主完成以下的操作
    @Controller @RequestMapping(value = "https://www.songbingjia.com/weixin") public class WeiXinLogin { @Autowired private UserService userService; //固定 private String appId="wxf51"; private String appSecret="34b625fa6afd86693325c84"; //访问此资源的url:http://localhost:8090/weixin/wxLogin.json @RequestMapping(value = "https://www.songbingjia.com/wxLogin", produces = MediaType.APPLICATION_JSON_VALUE) public void wxreg(ModelMap result,HttpServletRequest request, String code){//1,获取凭证信息 SnsToken token = SnsAPI.oauth2AccessToken(appId, appSecret, code); //依据凭证信息判断用户是否用微信注册账户? String openid = token.getOpenid(); System.out.println("+++++++++++++++++++++openId=:"+openid); User userLogin = new User(); userLogin.setOpenId(openid); userLogin = userService.getUserByExample(userLogin); if (userLogin == null){//用户没有使用微信注册过用户信息//2,服务器收到用户的登录请求,向微信开放平台发送access_token是否有效的验证请求 BaseResult baseResult = SnsAPI.auth(token.getAccess_token(), token.getOpenid()); if("0".equals(baseResult.getErrcode()) & & "ok".equals(baseResult.getErrmsg())){//3,服务端获取到新的access_token等信息,并返回给客户端,客户端成功登录或者重新获取授权。 weixin.popular.bean.user.User user = SnsAPI.userinfo(token.getAccess_token(), token.getOpenid(), "zh-CN"); System.out.println("user: " + user.toString()); result.put("code",200); result.put("msg","成功获得微信登录信息"); result.put("data",user); User userInSert = new User(); //待添加其他属性 userInSert.setOpenId(user.getOpenid()); userInSert.setNickname(user.getNickname()); userInSert.setSex(user.getSex()); userInSert.setProvince(user.getProvince()); userInSert.setCountry(user.getCountry()); userInSert.setCity(user.getCity()); userInSert.setAvatar(user.getHeadimgurl()); userInSert.setLoginName(user.getNickname()); userService.addUser(userInSert); //添加该用户到数据库 userInSert = userService.getUserByExample(userInSert); System.out.println("+++++++++++++++++++userInsert="+userInSert.toString()); request.getSession().setAttribute("users",userInSert); result.put("code",200); result.put("msg","微信绑定成功!"); result.put("data",userInSert); }else{ result.put("code",302); result.put("msg","获得微信登录用户失败"); } }else{ request.getSession().setAttribute("users",userLogin); System.out.println("登录成功!"+userLogin.toString()); result.put("code",200); result.put("msg","登录成功!"); result.put("data",userLogin); } } }

     

    推荐阅读