Spring Webflux资源服务器和Oauth2

时间:2019-10-20 19:52:28

标签: java spring spring-security spring-security-oauth2 spring-webflux

我有两个单独的微服务:身份验证服务器和带资源的服务。 我需要的是从身份验证服务器获取令牌,并使用此令牌向资源服务发出请求(并对其进行验证并将其交换为用户名)。这时我得到了无法正常运行的实现。

application.yml

spring:
  security:
    oauth2:
      client:
        registration:
          custom:
            client-id: SampleClientId
            client-secret: secret
            scopes: USER
            authorization-grant-type: authorization_code
            redirect-uri-template: http://localhost:8082/ui/login
        provider:
          custom:
            authorization-uri: http://localhost:8081/auth/oauth/authorize
            token-uri: http://localhost:8081/auth/oauth/token
            user-info-uri: http://localhost:8081/auth/authenticate

http://localhost:8081/auth/authenticate返回授权服务器中的主体对象的地方。

SecurityConfig.class

@EnableWebFluxSecurity
public class SecurityConfig {

    @Bean
    public SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
        return http.authorizeExchange()
                .pathMatchers("/health").permitAll()
                .anyExchange().authenticated()
                .and().oauth2Login()
                .and().build();
    }

}

一个简单的端点来检索资源服务器中的用户名

@GetMapping("/username")
public Mono<String> getHesalth(@RegisteredOAuth2AuthorizedClient("custom") OAuth2AuthorizedClient authorizedClient){
    return Mono.just(authorizedClient.getPrincipalName());
}

当我用令牌头发出请求时:

curl -X GET \
  http://localhost:8605/username \
  -H 'Authorization: Bearer {token-from-auth-server}' \
  -H 'Content-Type: application/json' 

资源服务器不返回任何内容并记录日志:

2019-10-20 21:44:38.641 DEBUG 29768 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [edd1881d] HTTP GET "/username"
2019-10-20 21:44:38.704 DEBUG 29768 --- [or-http-epoll-3] .s.u.m.MediaTypeServerWebExchangeMatcher : httpRequestMediaTypes=[*/*]
2019-10-20 21:44:38.705 DEBUG 29768 --- [or-http-epoll-3] .s.u.m.MediaTypeServerWebExchangeMatcher : Processing */*
2019-10-20 21:44:38.705 DEBUG 29768 --- [or-http-epoll-3] .s.u.m.MediaTypeServerWebExchangeMatcher : Ignoring
2019-10-20 21:44:38.705 DEBUG 29768 --- [or-http-epoll-3] .s.u.m.MediaTypeServerWebExchangeMatcher : Did not match any media types
2019-10-20 21:44:38.705 DEBUG 29768 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [edd1881d] Completed 302 FOUND
2019-10-20 21:44:38.711 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Last HTTP response frame
2019-10-20 21:44:38.711 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] No sendHeaders() called before complete, sending zero-length header
2019-10-20 21:44:38.714 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Decreasing pending responses, now 0
2019-10-20 21:44:38.715 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Last HTTP packet was sent, terminating the channel
2019-10-20 21:44:38.715 DEBUG 29768 --- [or-http-epoll-3] r.n.channel.ChannelOperationsHandler     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] No ChannelOperation attached. Dropping: EmptyLastHttpContent
2019-10-20 21:44:38.717 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Increasing pending responses, now 1
2019-10-20 21:44:38.717 DEBUG 29768 --- [or-http-epoll-3] reactor.netty.http.server.HttpServer     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@3ec155e2
2019-10-20 21:44:38.717 DEBUG 29768 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [edd1881d] HTTP GET "/oauth2/authorization/custom"
2019-10-20 21:44:38.732 DEBUG 29768 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [edd1881d] Completed 302 FOUND
2019-10-20 21:44:38.734 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Last HTTP response frame
2019-10-20 21:44:38.735 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] No sendHeaders() called before complete, sending zero-length header
2019-10-20 21:44:38.735 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Decreasing pending responses, now 0
2019-10-20 21:44:38.735 DEBUG 29768 --- [or-http-epoll-3] r.n.http.server.HttpServerOperations     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] Last HTTP packet was sent, terminating the channel
2019-10-20 21:44:38.735 DEBUG 29768 --- [or-http-epoll-3] r.n.channel.ChannelOperationsHandler     : [id: 0xedd1881d, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33452] No ChannelOperation attached. Dropping: EmptyLastHttpContent
2019-10-20 21:44:38.738 DEBUG 29768 --- [or-http-epoll-4] r.n.http.server.HttpServerOperations     : [id: 0x93302880, L:/0:0:0:0:0:0:0:1%0:8605 - R:/0:0:0:0:0:0:0:1%0:33454] New http connection, requesting read

资源服务器看不到令牌和重定向。 有人针对这种情况配置正确吗?还是我在哪里弄错了?

0 个答案:

没有答案