googleoauth2.0-redirect-uri不匹配,尽管与google云平台中的授权重定向uri相同

d7v8vwbk  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(250)

我在很多其他的帖子中看到过这个问题,但是没有一个能解决我的问题。我有一个前端vue.js应用程序和一个spring boot java应用程序。
我正在使用vue google oauth从我的前端应用程序提示google登录以获取身份验证代码,然后我想使用我的后端服务器获取用户详细信息并在那里处理逻辑。
在google云平台上,我定义了一个授权的重定向uri:

当我在前端发送我的身份验证代码时,我使用的是同一个uri

import api from "@/assets/js/api";
import AdminNavigation from "./AdminNavigation";
import { mapGetters } from "vuex";
import Axios from "axios";

export default {
  name: "Dashboard",
  computed: {
    ...mapGetters(["IsSignedIn"]),
  },
  data() {
    return {
      title: "Christopher s' portfolio admin",
      appDescription:
        "Here you can add contents for the front end portfolio website.",
      isInit: false,
    };
  },
  components: {
    AdminNavigation,
  },
  methods: {
    signIn: async function () {
      try {
        const authCode = await this.$gAuth.getAuthCode();
        Axios.post("http://localhost:8080/authenticate", {
          code: authCode,
          redirect_uri: "http://localhost:3000/admin/dashboard",
        });
      } catch (err) {
        console.log(err);
      }
    },
  },
  mounted() {
    let that = this;
    let checkGauthLoad = setInterval(function () {
      that.isInit = that.$gAuth.isInit;
      if (!this.IsSignedIn) {
        that.signIn();
      }

      if (that.isInit) clearInterval(checkGauthLoad);
    }, 1000);
  },
};

我的后端服务器接收身份验证代码和重定向uri,这与在google云平台上定义的内容相同。

package com.salay.christophersalayportfolio.controllers;

import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.salay.christophersalayportfolio.general.ConstantVariables;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;

@Controller
public class AdminController {
    @CrossOrigin(origins = "http://localhost:3000")
    @RequestMapping(value = "/authenticate", method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    @ResponseBody
    public String authentication(HttpEntity<String> data) throws IOException, ParseException {

        JSONParser parser = new JSONParser();
        JSONObject json = (JSONObject) parser.parse(data.getBody());

        String authCode = json.get("code").toString();
        String redirect_uri = json.get("redirect_uri").toString();

        try {
            GoogleTokenResponse response =
                new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
                    ConstantVariables.GOOGLE_CLIENT_ID,
                    ConstantVariables.GOOGLE_CLIENT_SECRET,
                    authCode, redirect_uri).execute();

            System.out.println("Access token: " + response.getAccessToken());

        } catch (TokenResponseException e) {
            if (e.getDetails() != null) {
                System.err.println("Error: " + e.getDetails().getError());
                if (e.getDetails().getErrorDescription() != null) {
                    System.err.println(e.getDetails().getErrorDescription());
                }
                if (e.getDetails().getErrorUri() != null) {
                    System.err.println(e.getDetails().getErrorUri());
                }
            } else {
                System.err.println(e.getMessage());
            }
        }
        return "";
    }

}

但我得到以下错误:

400 Bad Request redirect_uri_mismatch

我一直在看大量的堆栈溢出问题,到目前为止没有一个解决方案对我有效。。。有什么想法吗?

lo8azlld

lo8azlld1#

听起来你没有发送你认为你是oauth的细节。您是否已从spring boot后端捕获到授权服务器的https消息?是否可以在此处发布详细信息?
如果有帮助的话,我的这篇博客文章包含了一些关于在java中配置http代理的注解。

相关问题