0

我的一个项目中使用了 Micronaut。我正在使用内置登录、注销控制器启用 micronaut 的 JWT 安全功能。但是当我到达/login终点时,它给了我以下错误:

从源“前端-enpdpoint ”访问“ micronaut-login-endpoint ”处的 XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问控制允许来源”标头存在于请求的资源上。

片段来自application.yml

micronaut:
  application:
    name: app-name
  server:
    port: *port*
    cors:
      enabled: true
      configurations:
        web:
          allowedOrigins:
            - *front-end-enpdpoint*
          allowedHeaders:
            - Content-Type
          allowedMethods:
            - POST
            - GET
            - OPTIONS
  security:
    enabled: true
    endpoints:
      login:
        enabled: true
      logout:
        enabled: true
      oauth:
        enabled: true
    token:
      enabled: true
      jwt:
        enabled: true

有人可以帮我解决这个问题吗?像@Jeff-Scott-Brown 这样来自 micronaut 核心社区的人可以帮助我吗?

4

1 回答 1

1

我最近遇到了这个问题,我能够使用这个问题线程来解决这个问题。

所以,按照这篇mdn CORS 文章。我发现有两种类型的 CORS 请求,简单的和预检的。在 auth 的情况下, Authorization 标头被修改,这需要预检请求。在预检中,向服务器发出一个 http OPTIONS(http 方法)请求以确认其身份。现在 micronaut 没有选项端点,可以通过创建 OPTIONS 控制器来解决,如 github 问题中所述。

于 2020-07-21T20:18:58.430 回答