如何在Spring Cloud Gateway中配置路由以使用带有authorization-grant-type: password
的OAuth2客户端?换句话说,如何在API的请求中添加带有令牌的Authorization头?因为我正在与遗留应用程序集成,所以我必须使用授权类型的密码。
我有这个应用程序:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("route_path", r -> r.path("/**")
.filters(f -> f.addRequestHeader("Authorization", "bearer <token>"))
.uri("http://localhost:8092/messages"))
.build();
}
}
将<token>
替换为实际令牌,一切都正常。
我发现这个项目做类似的事情:https://github.com/jgrandja/spring-security-oauth-5-2-migrate。它具有客户端(messaging-client-password
),该客户端用于配置WebClient
以添加OAuth2支持来进行请求(即,通过添加授权报头)。
我们不能马上使用这个示例项目,因为Spring Cloud Gateway是被动的,而且我们的配置方式发生了很大的变化。
更新
我有点让它工作,但它是在非常糟糕的状态。
首先,我发现了如何将WebClientConfig
转换为React式:
@Configuration
public class WebClientConfig {
@Bean
WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
oauth.setDefaultOAuth2AuthorizedClient(true);
oauth.setDefaultClientRegistrationId("messaging-client-password");
return WebClient.builder()
.filter(oauth)
.build();
}
@Bean
ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
ReactiveClientRegistrationRepository clientRegistrationRepository,
ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
.refreshToken()
.password()
.build();
DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
new DefaultReactiveOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientRepository);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
// For the `password` grant, the `username` and `password` are supplied via request parameters,
// so map it to `OAuth2AuthorizationContext.getAttributes()`.
authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());
return authorizedClientManager;
}
private Function<OAuth2AuthorizeRequest, Mono<Map<String, Object>>> contextAttributesMapper() {
return authorizeRequest -> {
Map<String, Object> contextAttributes = Collections.emptyMap();
ServerWebExchange serverWebExchange = authorizeRequest.getAttribute(ServerWebExchange.class.getName());
String username = serverWebExchange.getRequest().getQueryParams().getFirst(OAuth2ParameterNames.USERNAME);
String password = serverWebExchange.getRequest().getQueryParams().getFirst(OAuth2ParameterNames.PASSWORD);
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
contextAttributes = new HashMap<>();
// `PasswordOAuth2AuthorizedClientProvider` requires both attributes
contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
}
return Mono.just(contextAttributes);
};
}
}
有了这个配置,我们可以使用WebClient
来发出一个请求。
@GetMapping("/explicit")
public Mono<String[]> explicit() {
return this.webClient
.get()
.uri("http://localhost:8092/messages")
.attributes(clientRegistrationId("messaging-client-password"))
.retrieve()
.bodyToMono(String[].class);
}
然后,通过调用这个函数,我们可以获得对授权客户端的引用:
private OAuth2AuthorizedClient authorizedClient;
@GetMapping("/token")
public String token(@RegisteredOAuth2AuthorizedClient("messaging-client-password") OAuth2AuthorizedClient authorizedClient) {
this.authorizedClient = authorizedClient;
return authorizedClient.getAccessToken().getTokenValue();
}
最后,通过配置全局过滤器,我们可以修改请求以包含Authorization头:
@Bean
public GlobalFilter customGlobalFilter() {
return (exchange, chain) -> {
//adds header to proxied request
exchange.getRequest().mutate().header("Authorization", authorizedClient.getAccessToken().getTokenType().getValue() + " " + authorizedClient.getAccessToken().getTokenValue()).build();
return chain.filter(exchange);
};
}
在按顺序运行这三个请求之后,我们可以使用Spring Cloud Gateway的密码授权。
当然,这个过程非常凌乱,还需要做的是:
1.获取筛选器内授权客户端的引用
1.使用contextAttributesMapper
使用凭据初始化授权客户端
1.将所有这些内容写入一个过滤器中,而不是全局过滤器中。TokenRelayGatewayFilterFactory实现可以很好地帮助您完成此操作。
1条答案
按热度按时间nimxete21#
我实现了授权-授予-类型:使用
WebClientHttpRoutingFilter
的密码。默认情况下,Spring云网关使用Netty路由过滤器,但有一种不需要Netty的替代方法(https://cloud.spring.io/spring-cloud-gateway/reference/html/#the-netty-routing-filter)
WebClientHttpRoutingFilter
使用WebClient
来路由请求。WebClient
可以通过ExchangeFilterFunction
(https://docs.spring.io/spring-security/site/docs/current/reference/html5/#webclient)配置为ReactiveOAuth2AuthorizedClientManager
。ReactiveOAuth2AuthorizedClientManager
将负责管理访问/刷新令牌,并为您完成所有繁重的工作Here您可以查看此实现。