简体   繁体   English

从 postman 向 Spring OAuth2 应用发送请求

[英]Sending request from postman to Spring OAuth2 app

I have created simplest spring oauth2 application.我创建了最简单的 spring oauth2 应用程序。 Here is all(:) my code这是所有(:)我的代码

@SpringBootApplication
public class Oauth2Application {

    public static void main(String[] args) {
        SpringApplication.run(Oauth2Application.class, args);
    }
}
@RestController
@CrossOrigin("*")
public class Controller {

    @GetMapping
    public String hello() {
        return "Hello";
    }

}
spring:
  security:
    oauth2:
      client:
        registration:
          github:
            clientId: clientId
            clientSecret: clientSecret

And I want to send a request to my hello endpoint via Postman.我想通过 Postman 向我的 hello 端点发送请求。

I have filled all required fields in the OAuth2 tab and generated my token.我已经填写了 OAuth2 选项卡中的所有必填字段并生成了我的令牌。 I can see my token in the Authorization header, but still, my request to localhost:8080 fails and returns me GitHub login page.我可以在授权 header 中看到我的令牌,但是我对 localhost:8080 的请求仍然失败并返回给我 GitHub 登录页面。 What am I doing wrong?我究竟做错了什么?

I have also generated token by myself by sending direct requests to Github auth URL and access token URL and this token didn't work for my app but worked for Github API. I have also generated token by myself by sending direct requests to Github auth URL and access token URL and this token didn't work for my app but worked for Github API.

Also interesting, is that when I am sending a request from a browser, it prompts me to the login page, I am logging in to Github and it redirects me to initial the page, I can't see any Authorization headers in all those requests.同样有趣的是,当我从浏览器发送请求时,它会提示我进入登录页面,我正在登录 Github 并将我重定向到初始页面,我在所有这些请求中都看不到任何授权标头. Only JSESSIONID cookie.只有 JSESSIONID cookie。

Please, guys, I would appreciate any ideas on how to make it work.拜托,伙计们,我将不胜感激有关如何使其工作的任何想法。 Thanks谢谢

I'll post a response as I don't have enough reputation to add a comment but it's more a hint than a response...我会发布回复,因为我没有足够的声誉来添加评论,但这更像是一个提示而不是回复......

First regarding the fact that you are not able to find the Authorization header when you look at the exchanges between the browser and your backend.首先,当您查看浏览器和后端之间的交换时,您无法找到 Authorization 标头。
I don't have it either but if you take a look at the cookies of your request you will find a KEYCLOCK_IDENTITY cookie.我也没有,但是如果您查看请求的 cookie,您会发现KEYCLOCK_IDENTITY cookie。
I use Keycloak as my identity provider but yours should look like GOOGLE_IDENTITY I guess ?我使用 Keycloak 作为我的身份提供者,但我猜你的应该看起来像GOOGLE_IDENTITY
It should hold the informations needed to ensure that you are who you are (at least it's my understanding of the presence of this cookie).它应该包含确保您是谁所需的信息(至少这是我对这个 cookie 存在的理解)。

Second thing: it looks like you want to use your SpringBoot application as a resource server, you should probably take a look at this page and use the spring-boot-starter-oauth2-resource-server dependency instead of the spring-boot-starter-oauth2-client dependency in order to make it work with an HTTP client like Postman.第二件事:看起来你想使用你的 SpringBoot 应用程序作为资源服务器,你应该看看这个页面并使用spring-boot-starter-oauth2-resource-server依赖而不是spring-boot-starter -oauth2-client依赖项,以使其与 Postman 等 HTTP 客户端一起工作。

I hope it helps.我希望它有所帮助。

@Gilles is right, you are writing a OAuth2 resource-server , not a client . @Gilles 是对的,您正在编写 OAuth2 resource-server ,而不是client

You can hardly use Github as authorization-server for your resource-server: it issues "opaque" tokens (not JWTs) which you can't use easily with spring-boot-starter-oauth2-resource-server or alike.您几乎不能将 Github 用作资源服务器的授权服务器:它发出“不透明”令牌(不是 JWT),您无法轻松使用spring-boot-starter-oauth2-resource-server或类似的。

An option for you is using an OIDC authorization-server of your own (like Keycloak) capable of "user identity federation": it will proxy Github (and any other common identity provider you like like Google, Facebook, etc.).您的一个选项是使用您自己的 OIDC 授权服务器(如 Keycloak),能够“用户身份联合”:它将代理 Github(以及您喜欢的任何其他常见身份提供者,如 Google、Facebook 等)。

Once you have an authorization-server issuing JWT access-tokens, you can have a look at those tutorials to configure your resource-server:一旦你有一个授权服务器发出 JWT 访问令牌,你可以看看这些教程来配置你的资源服务器:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM