【问题标题】:Autherization service is not responsding with access_token while using zuul gateway使用 zuul 网关时,授权服务没有使用访问令牌响应
【发布时间】:2017-11-22 13:34:57
【问题描述】:

我用 springboot 1.4 创建了多个微服务。上周我决定实施 oauth2 授权服务。我的计划是这样的,

  1. 每个请求都应该由Eureka注册的zuul网关处理。

  2. 所以eureka会调用新的Authorization服务来获取access_token。

我发现的问题是我可以直接从另一个端口(8081)中运行的授权服务器获取 JWT 访问令牌。当我尝试通过 zuul 网关获取 jwt 令牌时,不幸的是我得到了一个空字符串。

请看一下我的网关配置

application.yml (zuul-gateway)

 zuul:
   routes: 
    static:
    path: /static/** 
   uaa:
    path: /uaa/**
    sensitive-headers: 
    serviceId: microservice-security-oauth2-server
   users:
    path: /users/**
    serviceId: microservice-core-user



security:
 basic:
  enabled: false

zuul的应用类是

 @SpringBootApplication
 @EnableZuulProxy
 @EnableEurekaClient
 @ComponentScan(basePackages={"com.configuration","com.zullfilter"})
 public class ZullGatewayServerApplication {

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

}

授权服务器配置类是

   @Configuration
   @EnableAuthorizationServer
   public class OAuth2AuthorizationConfiguration extends  AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private Environment environment;

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.tokenStore(tokenStore())
            .tokenEnhancer(jwtTokenEnhancer())
            .authenticationManager(authenticationManager);
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security.tokenKeyAccess("permitAll()")
            .checkTokenAccess("isAuthenticated()");
}

@Bean
public TokenStore tokenStore() {
    return new JwtTokenStore(jwtTokenEnhancer());
}

@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
    String pwd = environment.getProperty("keystore.password");
    KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
            new ClassPathResource("jwt.jks"),
            pwd.toCharArray());
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setKeyPair(keyStoreKeyFactory.getKeyPair("jwt"));
    return converter;
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
            .withClient("service-account-1")
            .secret("service-account-1-secret")
            .authorizedGrantTypes("client_credentials")
            .scopes("resource-server-read", "resource-server-write")
            .accessTokenValiditySeconds(6000);
 }
}

我正在获取 access_token 直接向

请求
 curl service-account-1:service-account-1-secret@localhost:8081/uaa/oauth/token -d grant_type=client_credentials

但是当我尝试使用 zuul 代理时,我得到一个空字符串

 curl service-account-1:service-account-1-secret@localhost:8765/uaa/oauth/token -d grant_type=client_credentials

我正在使用 zuul 网关版本 1.3 和弹簧靴 1.4

如果有人之前遇到过这个问题,请告诉我

【问题讨论】:

  • 有没有可能已经解决了这个问题?...解决了什么问题?

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


【解决方案1】:

在我的情况下,解决方案是告诉 Zuul 不要去掉前缀。我在身份验证服务器上点击了错误的 url 并获得了 401 禁止,而 Zuul 正在吞下错误并返回 200。

zuul:
  routes:
    auth:
      path: /oauth/**
      serviceId: saapi-auth-server
      stripPrefix: false
      sensitiveHeaders: true

我在 Zuul 客户端上有一个像下面这样的 ResourceServerConfigurerAdapter。我不得不禁用 CSRF,但我们所有的调用都是系统到系统的,所以我们应该很好。

@Configuration
@EnableResourceServer
public class JwtSecurityConfig extends ResourceServerConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/oauth/**").permitAll()
            .antMatchers("/api/**").hasAuthority("ROLE_API_ACCESS")
            .and()
            .csrf().disable();
    }
}

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2021-08-26
    • 2022-10-23
    • 2018-01-15
    • 1970-01-01
    • 2021-10-09
    • 2019-11-26
    • 1970-01-01
    • 2021-07-01
    相关资源
    最近更新 更多