简体   繁体   English

Spring Security Oauth2 Swagger

[英]Spring Security Oauth2 Swagger

I am attempting to configure Swagger Authentication on my resource server, so that I can authenticate against my authorization server.我正在尝试在我的资源服务器上配置 Swagger 身份验证,以便我可以对我的授权服务器进行身份验证。

I have the resource and authorization servers separated.我将资源和授权服务器分开。 They are both starting up on my localhost with different ports.他们都使用不同的端口在我的本地主机上启动。

  • Resource Server on port 8083端口 8083 上的资源服务器
  • Authorization Server on port 8081端口 8081 上的授权服务器

Whenever I attempt to "Authorize", there is a CORS issues.每当我尝试“授权”时,都会出现 CORS 问题。

I forked a project from another website to use as my testing grounds.我从另一个网站分叉了一个项目作为我的测试场。 Below is the forked project.下面是分叉的项目。

https://github.com/cbriarnold/oauth2-spring-boot-2.0.2 https://github.com/cbriarnold/oauth2-spring-boot-2.0.2

To test, once both the authorization server and resource server are started, do the following:要测试,一旦授权服务器和资源服务器都启动,请执行以下操作:

  1. Go to http://localhost:8083/swagger-ui.html转到http://localhost:8083/swagger-ui.html
  2. Click on "Authorize" button点击“授权”按钮
  3. Click on "Authorize" button in dialog单击对话框中的“授权”按钮

If you have the developer tools open, you will see that there is the CORS error如果你打开了开发者工具,你会看到有 CORS 错误

Access to fetch at ' http://localhost:8081/oauth/token/ ' from origin ' http://localhost:8083 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.从源 ' http://localhost:8083 ' 获取在 ' http://localhost:8081/oauth/token/ ' 的访问已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:重定向是不允许预检请求。

The http status code associated with the above CORS error was 302.与上述 CORS 错误相关的 http 状态代码是 302。

So, I modify the source to try to permitAll (I know this is undesired from a security perspective, but I am just trying to figure out what is going on).因此,我修改源以尝试允许所有(我知道从安全角度来看这是不受欢迎的,但我只是想弄清楚发生了什么)。 I will receive the same CORS error, but with the https status code of 403. Code is found on the following branch.我将收到相同的 CORS 错误,但 https 状态代码为 403。代码位于以下分支。

https://github.com/cbriarnold/oauth2-spring-boot-2.0.2/tree/permitAll https://github.com/cbriarnold/oauth2-spring-boot-2.0.2/tree/permitAll

Any suggestions?有什么建议?

I think you need to add @CrossOrigin(/*your hitting server*/) on your controllers我认为您需要在控制器上添加@CrossOrigin(/*your hitting server*/)

it's a chrome security check to see if your are permitted to talk with that server or not it sends an option request at first and if you're permitted it send your original request which is accessing swagger这是一项 chrome 安全检查,以查看您是否被允许与该服务器通信,它首先发送一个选项请求,如果您被允许,它会发送您访问 swagger 的原始请求

if you want to make sure if that is a cross origin problem or not try to send that request via postman because postman send your request direct without option request at first如果您想确定这是否是跨源问题,或者不尝试通过邮递员发送该请求,因为邮递员首先直接发送您的请求而没有选项请求

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

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