【问题标题】:Zuul proxy oAuth2 Unauthorized in Spring BootZuul代理oAuth2在Spring Boot中未经授权
【发布时间】:2016-10-28 01:37:22
【问题描述】:

我有一个使用 oAuth2 保护的微服务,称为 country-service。当我直接向该服务发送请求时,包括我的 JWT 不记名令牌,一切正常:

server:
  port: 8081

spring:
  database:
    driverClassName: org.postgresql.Driver
  datasource:
    url: jdbc:postgresql://localhost:5432/vue-boot-country
    username: postgres
    password: postgres
  jpa:
    hibernate:
      ddl-auto: validate
    database-platform: org.hibernate.dialect.PostgreSQLDialect

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

我还有一个 api-gateway(Zuul 代理):

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class VueBootApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(VueBootApiGatewayApplication.class, args);
    }
}

除了这两个之外没有其他文件

server:
  port: 8765

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

zuul:
  routes:
    vue-boot-country-service: /api/country/**
  add-proxy-headers: true

我无法向代理发送成功的请求,我不断收到“未经授权”错误:

注意:当我从资源服务器中删除 oAuth2 安全性时,Zuul 代理似乎可以工作。

有人知道我在这里做错了什么吗?

【问题讨论】:

    标签: spring proxy spring-boot oauth-2.0 netflix-zuul


    【解决方案1】:

    这与zuuls 所谓的“敏感”标头有关,例如“授权”。这些会针对传递给内部的所有请求进行过滤...

    我不知道,设置标头是否已经在使用此配置:

    zuul:
      ignoredHeaders:
        - authorization
    

    如果没有,你可以定义一个 Zuul 过滤器 bean 来手动管理这个:

    @Component
    public class RelayTokenFilter extends ZuulFilter{
        @Override
        public String filterType() {
            return "pre";
        }
    
        @Override
        public int filterOrder() {
            return 10000;
        }
    
        @Override
        public boolean shouldFilter() {
            return true;
        }
    
        @Override
        public Object run() {
            RequestContext context = RequestContext.getCurrentContext();
    
            @SuppressWarnings("unchecked") Set<String> ignoredHeaders = (Set<String>) context.get("ignoredHeaders");
            ignoredHeaders.remove("authorization");
    
            return null;
        }
    }
    

    【讨论】:

    • 我遇到了同样的问题,太好了!
    【解决方案2】:

    我遇到了同样的问题。其中令牌不通过 Zuul 传递给微服务。所以我加了

    zuul: sensitiveHeaders: Cookie,Set-Cookie

    希望它对某人有用

    【讨论】:

      猜你喜欢
      • 2017-10-04
      • 2023-04-02
      • 2017-07-27
      • 2018-07-04
      • 2022-08-04
      • 2021-04-07
      • 2019-11-09
      • 2019-12-26
      • 2020-11-22
      相关资源
      最近更新 更多