Maison  >  Article  >  Java  >  Comment Springboot implémente la connexion tierce au site Web

Comment Springboot implémente la connexion tierce au site Web

王林
王林avant
2023-05-16 18:35:07760parcourir

Étape 1 : Créez une interface qui hérite d'AuthService, WeChatAuthService, comme suit

public interface WeChatAuthService extends AuthService {
  public JSONObject getUserInfo(String accessToken, String openId);
}

Étape 2 : L'implémentation spécifique de WeChatService est la suivante

@Service
public class WeChatAuthServiceImpl extends DefaultAuthServiceImpl implements WeChatAuthService {

  private Logger logger = LoggerFactory.getLogger(WeChatAuthServiceImpl.class);

//请求此地址即跳转到二维码登录界面
  private static final String AUTHORIZATION_URL =
      "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";

  // 获取用户 openid 和access——toke 的 URL
  private static final String ACCESSTOKE_OPENID_URL =
      "https://api.weixin.qq.com/sns/oauth3/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

  private static final String REFRESH_TOKEN_URL =
      "https://api.weixin.qq.com/sns/oauth3/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s";

  private static final String USER_INFO_URL =
      "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";

  private static final String APP_ID="xxxxxx";
  private static final String APP_SECRET="xxxxxx";
  private static final String SCOPE = "snsapi_login";

  private String callbackUrl = "https://www.xxx.cn/auth/wechat"; //回调域名

  @Override
  public String getAuthorizationUrl() throws UnsupportedEncodingException {
    callbackUrl = URLEncoder.encode(callbackUrl,"utf-8");
    String url = String.format(AUTHORIZATION_URL,APP_ID,callbackUrl,SCOPE,System.currentTimeMillis());
    return url;
  }


  @Override
  public String getAccessToken(String code) {
    String url = String.format(ACCESSTOKE_OPENID_URL,APP_ID,APP_SECRET,code);

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
    URI uri = builder.build().encode().toUri();

    String resp = getRestTemplate().getForObject(uri, String.class);
    logger.error("getAccessToken resp = "+resp);
    if(resp.contains("openid")){
      JSONObject jsonObject = JSONObject.parseObject(resp);
      String access_token = jsonObject.getString("access_token");
      String openId = jsonObject.getString("openid");;

      JSONObject res = new JSONObject();
      res.put("access_token",access_token);
      res.put("openId",openId);
      res.put("refresh_token",jsonObject.getString("refresh_token"));

      return res.toJSONString();
    }else{
      throw new ServiceException("获取token失败,msg = "+resp);
    }
  }

  //微信接口中,token和openId是一起返回,故此方法不需实现
  @Override
  public String getOpenId(String accessToken) {
    return null;
  }

  @Override
  public JSONObject getUserInfo(String accessToken, String openId){
    String url = String.format(USER_INFO_URL, accessToken, openId);
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
    URI uri = builder.build().encode().toUri();

    String resp = getRestTemplate().getForObject(uri, String.class);
    logger.error("getUserInfo resp = "+resp);
    if(resp.contains("errcode")){
      throw new ServiceException("获取用户信息错误,msg = "+resp);
    }else{
      JSONObject data =JSONObject.parseObject(resp);

      JSONObject result = new JSONObject();
      result.put("id",data.getString("unionid"));
      result.put("nickName",data.getString("nickname"));
      result.put("avatar",data.getString("headimgurl"));

      return result;
    }
  }

  //微信的token只有2小时的有效期,过时需要重新获取,所以官方提供了
  //根据refresh_token 刷新获取token的方法,本项目仅仅是获取用户
  //信息,并将信息存入库,所以两个小时也已经足够了
  @Override
  public String refreshToken(String refresh_token) {

    String url = String.format(REFRESH_TOKEN_URL,APP_ID,refresh_token);

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
    URI uri = builder.build().encode().toUri();

    ResponseEntity<JSONObject> resp = getRestTemplate().getForEntity(uri,JSONObject.class);
    JSONObject jsonObject = resp.getBody();

    String access_token = jsonObject.getString("access_token");
    return access_token;
  }
}

Étape 3 :

Appelez dans le Controller, le code est la suivante :

@RequestMapping(value = "/wxLoginPage",method = RequestMethod.GET)
  public JSONObject wxLoginPage() throws Exception {
    String uri = weChatAuthService.getAuthorizationUrl();
    return loginPage(uri);
  }

  @RequestMapping(value = "/wechat")
  public void callback(String code,HttpServletRequest request,HttpServletResponse response) throws Exception {
    String result = weChatAuthService.getAccessToken(code);
    JSONObject jsonObject = JSONObject.parseObject(result);

    String access_token = jsonObject.getString("access_token");
    String openId = jsonObject.getString("openId");
//    String refresh_token = jsonObject.getString("refresh_token");

    // 保存 access_token 到 cookie,两小时过期
    Cookie accessTokencookie = new Cookie("accessToken", access_token);
    accessTokencookie.setMaxAge(60 *2);
    response.addCookie(accessTokencookie);

    Cookie openIdCookie = new Cookie("openId", openId);
    openIdCookie.setMaxAge(60 *2);
    response.addCookie(openIdCookie);

    //根据openId判断用户是否已经登陆过
    KmsUser user = userService.getUserByCondition(openId);

    if (user == null) {
      response.sendRedirect(request.getContextPath() + "/student/html/index.min.html#/bind?type="+Constants.LOGIN_TYPE_WECHAT);
    } else {
      //如果用户已存在,则直接登录
      response.sendRedirect(request.getContextPath() + "/student/html/index.min.html#/app/home?open_id=" + openId);
    }
  }

Étape 4 :

Dans le js front-end, demandez d'abord auth/wxLoginPage pour obtenir l'adresse d'autorisation. Une fois que l'utilisateur l'a autorisé, /auth/wechat sera rappelé. traitement logique dans cette méthode.

Pièges rencontrés :

1. Lors de la configuration du nom de domaine de rappel sur le site officiel de WeChat, aucun protocole http ou https n'est requis. Il vous suffit d'écrire le domaine, tel que http://baidu.com. dans baidu.com Si vous souhaitez accéder à une méthode d'un contrôleur sous le projet, telle que baidu.com/auth/wechat, il vous suffit de configurer baidu.com lors de la configuration, et il n'est pas nécessaire de spécifier ce qui suit. .auth/wechat, l'adresse suivante peut être écrite lors de la configuration de l'adresse de rappel dans le code. Le code doit être configuré comme https://baidu.com/auth/wechat
2. Accédez à l'interface du code QR d'autorisation. vous rencontrerez une situation dans laquelle le code QR ne peut pas être généré. Cela est dû à l'adresse de rappel dans le code. Il ne devrait y avoir aucun problème si vous le configurez de la manière indiquée dans le code ci-dessus.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer