java - Spring Cloud Gateway在调用spring oauth2授权服务器时卡住

标签 java spring spring-boot spring-cloud spring-security-oauth2

我正在尝试在 Spring Cloud 网关(spring-cloud-gateway 2.0.0.M5)后面运行 oauth2 授权服务器(spring-security-oauth2 2.2.1.RELEASE),两者都带有 spring boot 2.0.0.RC1。 如果我直接调用授权服务器,它会正常工作,但是如果我通过网关(使用curl或使用浏览器)调用它,则调用将保持卡住而没有答案。我在 Windows 10 和 Mac 上检查了这一点,这两个应用程序都在 Java 9 上运行。

如果我使用调试日志记录级别运行应用程序,则在运行 curl client:secret@localhost:8080 -d grant_type=password -d password=password -d username=username -v 时得到此输出:

在授权服务器上我只得到这个日志:

2018-02-08 17:17:41.816 DEBUG 132 --- [8081-Acceptor-0] o.apache.tomcat.util.threads.LimitLatch  : Counting up[http-nio-8081-Acceptor-0] latch=1

在我得到的网关上:

2018-02-08 17:19:06.170 DEBUG 18856 --- [ctor-http-nio-1] r.ipc.netty.http.server.HttpServer       : [id: 0x1ad52031, L:/0:0:0:0:0:0:0:0:8080] READ: [id: 0x6d119e6b, L:/127.0.0.1:8080 - R:/127.0.0.1:52564]
2018-02-08 17:19:06.171 DEBUG 18856 --- [ctor-http-nio-1] r.ipc.netty.http.server.HttpServer       : [id: 0x1ad52031, L:/0:0:0:0:0:0:0:0:8080] READ COMPLETE
2018-02-08 17:19:06.171 DEBUG 18856 --- [ctor-http-nio-4] r.i.n.http.server.HttpServerOperations   : New http connection, requesting read
2018-02-08 17:19:06.171 DEBUG 18856 --- [ctor-http-nio-4] r.ipc.netty.channel.ContextHandler       : After pipeline DefaultChannelPipeline{(reactor.left.loggingHandler = io.netty.handler.logging.LoggingHandler), (ServerContextHandler#0 = reactor.ipc.netty.channel.ServerContextHandler), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpServerHandler = reactor.ipc.netty.http.server.HttpServerHandler), (reactor.right.reactiveBridge = reactor.ipc.netty.channel.ChannelOperationsHandler)}
2018-02-08 17:19:06.171 DEBUG 18856 --- [ctor-http-nio-4] r.ipc.netty.http.server.HttpServer       : [id: 0x6d119e6b, L:/127.0.0.1:8080 - R:/127.0.0.1:52564] REGISTERED
2018-02-08 17:19:06.171 DEBUG 18856 --- [ctor-http-nio-4] r.ipc.netty.http.server.HttpServer       : [id: 0x6d119e6b, L:/127.0.0.1:8080 - R:/127.0.0.1:52564] ACTIVE
2018-02-08 17:19:06.172 DEBUG 18856 --- [ctor-http-nio-4] r.ipc.netty.http.server.HttpServer       : [id: 0x6d119e6b, L:/127.0.0.1:8080 - R:/127.0.0.1:52564] READ: 246B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 50 4f 53 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d |POST / HTTP/1.1.|
|00000010| 0a 48 6f 73 74 3a 20 6c 6f 63 61 6c 68 6f 73 74 |.Host: localhost|
|00000020| 3a 38 30 38 30 0d 0a 41 75 74 68 6f 72 69 7a 61 |:8080..Authoriza|
|00000030| 74 69 6f 6e 3a 20 42 61 73 69 63 20 59 32 78 70 |tion: Basic Y2xp|
|00000040| 5a 57 35 30 4f 6e 4e 6c 59 33 4a 6c 64 41 3d 3d |ZW50OnNlY3JldA==|
|00000050| 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 63 75 |..User-Agent: cu|
|00000060| 72 6c 2f 37 2e 34 37 2e 30 0d 0a 41 63 63 65 70 |rl/7.47.0..Accep|
|00000070| 74 3a 20 2a 2f 2a 0d 0a 43 6f 6e 74 65 6e 74 2d |t: */*..Content-|
|00000080| 4c 65 6e 67 74 68 3a 20 35 35 0d 0a 43 6f 6e 74 |Length: 55..Cont|
|00000090| 65 6e 74 2d 54 79 70 65 3a 20 61 70 70 6c 69 63 |ent-Type: applic|
|000000a0| 61 74 69 6f 6e 2f 78 2d 77 77 77 2d 66 6f 72 6d |ation/x-www-form|
|000000b0| 2d 75 72 6c 65 6e 63 6f 64 65 64 0d 0a 0d 0a 67 |-urlencoded....g|
|000000c0| 72 61 6e 74 5f 74 79 70 65 3d 70 61 73 73 77 6f |rant_type=passwo|
|000000d0| 72 64 26 70 61 73 73 77 6f 72 64 3d 70 61 73 73 |rd&password=pass|
|000000e0| 77 6f 72 64 26 75 73 65 72 6e 61 6d 65 3d 75 73 |word&username=us|
|000000f0| 65 72 6e 61 6d 65                               |ername          |
+--------+-------------------------------------------------+----------------+
2018-02-08 17:19:06.172 DEBUG 18856 --- [ctor-http-nio-4] r.i.n.http.server.HttpServerOperations   : Increasing pending responses, now 1
2018-02-08 17:19:06.172 DEBUG 18856 --- [ctor-http-nio-4] r.ipc.netty.http.server.HttpServer       : [id: 0x6d119e6b, L:/127.0.0.1:8080 - R:/127.0.0.1:52564] READ COMPLETE
2018-02-08 17:19:06.172 DEBUG 18856 --- [ctor-http-nio-4] r.ipc.netty.channel.ChannelOperations    : [HttpServer] [id: 0x6d119e6b, L:/127.0.0.1:8080 - R:/127.0.0.1:52564] handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@2adaf15
2018-02-08 17:19:06.173 DEBUG 18856 --- [ctor-http-nio-4] o.s.web.reactive.DispatcherHandler       : Processing POST request for [http://localhost:8080/]
2018-02-08 17:19:06.173 DEBUG 18856 --- [ctor-http-nio-4] s.w.r.r.m.a.RequestMappingHandlerMapping : Looking up handler method for path /
2018-02-08 17:19:06.173 DEBUG 18856 --- [ctor-http-nio-4] s.w.r.r.m.a.RequestMappingHandlerMapping : Did not find handler method for [/]
2018-02-08 17:19:06.173 DEBUG 18856 --- [ctor-http-nio-4] o.s.c.g.h.RoutePredicateHandlerMapping   : RouteDefinition matched: 306be04f-f762-488a-b24d-47be9748ad34
2018-02-08 17:19:06.173 DEBUG 18856 --- [ctor-http-nio-4] o.s.c.g.h.RoutePredicateHandlerMapping   : Mapping [Exchange: POST http://localhost:8080/] to Route{id='306be04f-f762-488a-b24d-47be9748ad34', uri=http://localhost:8081/oauth/token, order=0, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$295/218688965@63309ea4, gatewayFilters=[]}
2018-02-08 17:19:06.173 DEBUG 18856 --- [ctor-http-nio-4] o.s.c.g.handler.FilteringWebHandler      : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@b0a1231}, order=-1}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@4833eff3}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@2676dc05}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@4694f434}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@56928e17}, order=2147483647}]
2018-02-08 17:19:06.174 DEBUG 18856 --- [ctor-http-nio-4] r.i.n.c.PooledClientContextHandler       : Acquiring existing channel from pool: DefaultPromise@395fcfef(incomplete) SimpleChannelPool{activeConnections=0}
2018-02-08 17:19:06.174 DEBUG 18856 --- [ctor-http-nio-6] r.i.n.resources.DefaultPoolResources     : Created [id: 0xb483ea9b], now 1 active connections
2018-02-08 17:19:06.174 DEBUG 18856 --- [ctor-http-nio-6] r.ipc.netty.channel.ContextHandler       : After pipeline DefaultChannelPipeline{(reactor.left.loggingHandler = io.netty.handler.logging.LoggingHandler), (SimpleChannelPool$1#0 = io.netty.channel.pool.SimpleChannelPool$1), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpClientCodec), (reactor.right.reactiveBridge = reactor.ipc.netty.channel.ChannelOperationsHandler)}
2018-02-08 17:19:06.174 DEBUG 18856 --- [ctor-http-nio-6] r.ipc.netty.http.client.HttpClient       : [id: 0xb483ea9b] REGISTERED
2018-02-08 17:19:06.174 DEBUG 18856 --- [ctor-http-nio-6] r.ipc.netty.http.client.HttpClient       : [id: 0xb483ea9b] CONNECT: localhost/127.0.0.1:8081
2018-02-08 17:19:06.175 DEBUG 18856 --- [ctor-http-nio-6] r.ipc.netty.http.client.HttpClient       : [id: 0xb483ea9b, L:/127.0.0.1:52565 - R:localhost/127.0.0.1:8081] ACTIVE
2018-02-08 17:19:06.175 DEBUG 18856 --- [ctor-http-nio-6] r.i.n.c.PooledClientContextHandler       : Acquired active channel: [id: 0xb483ea9b, L:/127.0.0.1:52565 - R:localhost/127.0.0.1:8081]
2018-02-08 17:19:06.175 DEBUG 18856 --- [ctor-http-nio-6] r.ipc.netty.channel.ChannelOperations    : [HttpClient] [id: 0xb483ea9b, L:/127.0.0.1:52565 - R:localhost/127.0.0.1:8081] handler is being applied: HttpClientHandler{startURI=http://localhost:8081/oauth/token, method=POST, handler=org.springframework.cloud.gateway.filter.NettyRoutingFilter$$Lambda$443/633701416@335243d7}
2018-02-08 17:19:06.176 DEBUG 18856 --- [ctor-http-nio-6] r.ipc.netty.http.client.HttpClient       : [id: 0xb483ea9b, L:/127.0.0.1:52565 - R:localhost/127.0.0.1:8081] USER_EVENT: reactor.ipc.netty.NettyPipeline$SendOptionsChangeEvent@31db44d0
2018-02-08 17:19:06.176 DEBUG 18856 --- [ctor-http-nio-6] r.i.n.channel.ChannelOperationsHandler   : [id: 0xb483ea9b, L:/127.0.0.1:52565 - R:localhost/127.0.0.1:8081] New sending options
2018-02-08 17:19:06.176 DEBUG 18856 --- [ctor-http-nio-4] reactor.ipc.netty.channel.FluxReceive    : [id: 0x6d119e6b, L:/127.0.0.1:8080 - R:/127.0.0.1:52564] Subscribing inbound receiver [pending: 1, cancelled:false, inboundDone: true]
2018-02-08 17:19:06.177 DEBUG 18856 --- [ctor-http-nio-6] reactor.ipc.netty.ReactorNetty           : Added encoder [reactor.left.chunkedWriter] at the beginning of the user pipeline, full pipeline: [reactor.left.loggingHandler, reactor.left.httpCodec, reactor.left.chunkedWriter, reactor.right.reactiveBridge, DefaultChannelPipeline$TailContext#0]
2018-02-08 17:19:06.177 DEBUG 18856 --- [ctor-http-nio-6] r.i.n.channel.ChannelOperationsHandler   : [id: 0xb483ea9b, L:/127.0.0.1:52565 - R:localhost/127.0.0.1:8081] Writing object io.netty.handler.codec.http.multipart.HttpPostRequestEncoder$WrappedFullHttpRequest@6235dbbf

curl 输出这个并卡住:

* Rebuilt URL to: client:secret@localhost:8080/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
* Server auth using Basic with user 'client'
> POST / HTTP/1.1
> Host: localhost:8080
> Authorization: Basic Y2xpZW50OnNlY3JldA==
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 55
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 55 out of 55 bytes

如果我运行curl client:secret@localhost:8081/oauth/token -d grant_type=password -d password=password -d username=username(直接调用身份验证服务器)我成功获得了 token 。

我的 Spring Cloud Gateway 是一个简单的 Spring Boot 应用程序,此配置在端口 8080 上运行:

@Configuration
public class GatewayConfiguration {

    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes().route(spec -> spec.path("/").uri("http://localhost:8081/oauth/token")).build();
    }

}

运行在8081端口上的oauth授权服务器的配置是这样的:

@Configuration
@EnableAuthorizationServer
public class AuthorizationConfiguration extends AuthorizationServerConfigurerAdapter {

    private final AuthorizationServerTokenServices tokenServices;
    private final AuthenticationManager authenticationManager;

    @Autowired
    public AuthorizationConfiguration(AuthorizationServerTokenServices tokenServices,
            AuthenticationManager authenticationManager) {
        super();
        this.tokenServices = Objects.requireNonNull(tokenServices);
        this.authenticationManager = Objects.requireNonNull(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
        configurer.inMemory().withClient("client").secret("secret").authorizedGrantTypes("password", "refresh_token")
                .scopes("read");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenServices(this.tokenServices).authenticationManager(this.authenticationManager);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.passwordEncoder(NoOpPasswordEncoder.getInstance());
    }
}

还有这个:

@Configuration
@EnableWebSecurity
@EnableGlobalAuthentication
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().httpBasic().and().csrf()
                .disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("username").password("password").authorities("ROLE_USER").and()
                .passwordEncoder(NoOpPasswordEncoder.getInstance());
    }

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

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        defaultTokenServices.setAccessTokenValiditySeconds(5000);
        defaultTokenServices.setRefreshTokenValiditySeconds(5000);
        return defaultTokenServices;
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

}

具有 gradle 依赖项的完整代码位于此存储库中 ( https://gitlab.com/Kenzow/spring-gateway-test )。我不知道我是否应该在 spring 存储库中打开一个问题,或者只是我的配置问题。 我有其他微服务(以及授权服务器上的其他工作端点)在同一网关后面运行,并且其他微服务没有任何问题。

最佳答案

今天我将 spring-cloud-gateway 更新到了新的 2.0.0.M6,现在一切正常。

关于java - Spring Cloud Gateway在调用spring oauth2授权服务器时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48690495/

相关文章:

spring-boot-starter-jta-atomikos 和 spring-boot-starter-batch

java - keyHolder.getKey() 返回 null

java - 使用 jersey 2.0 时出现 NoSuchMethodError

java - 在 java 中使用方法避免 != null

java - 无法从 STRING 单元格获取 NUMERIC 值

java - 是否有任何基于 Spring-Security 的 Java 生产就绪安全包?

spring - 在 Spring 应用程序中处理异常的位置

java - 自定义 AnnotationFormatterFactory 不起作用

spring-boot - Spring Boot-我可以将Jetty与SL4J一起使用

java - 在 Java 中实现双向链表中的 Iterable