- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 Spring Cloud Gateway 中配置路由以使用带有 authorization-grant-type: password
的 OAuth2 客户端?换句话说,如何在 API 请求中添加带有 token 的 Authorization header ?因为我要与遗留应用程序集成,所以我必须使用授权类型密码。
我有这个应用程序:
@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>
使用实际 token ,一切正常。
我发现这个项目做类似的事情:https://github.com/jgrandja/spring-security-oauth-5-2-migrate .它有一个客户端(messaging-client-password
)用于配置WebClient
添加 OAuth2 支持以发出请求(即通过添加授权 header )。
我们不能立即使用这个示例项目,因为 Spring Cloud Gateway 是响应式(Reactive)的,我们配置事物的方式发生了显着变化。我认为解决这个问题主要是关于转换 WebClientConfig类。
更新
我有点让它工作,但它的状态非常糟糕。
首先,我找到了如何转换 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
提出请求。这会在调用端点后以某种方式初始化 OAuth2 客户端:
@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();
}
最后,通过配置全局过滤器,我们可以修改请求以包含授权 header :
@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 的密码授予。
当然,这个过程很乱。还需要做的事情:
contextAttributesMapper
使用凭据初始化授权客户端最佳答案
我使用 WebClientHttpRoutingFilter
实现了 authorization-grant-type: password。
默认情况下,spring cloud gateway 使用 Netty Routing Filter 但有一个替代方案不需要 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
将负责管理访问/刷新 token 并将为您完成所有繁重的工作
Here您可以查看此实现。此外,我用这种方法实现了客户端凭证授予
关于spring - 使用 OAuth2 资源所有者密码授权类型在 Spring Cloud Gateway 中创建路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59643201/
假设我有一个简单的模型: class Contact(models.Model): owner = models.ForeignKey(User, editable=False) fi
我在docker容器中的轨道上运行ruby。如果生成类似docker-compose run rails rails g controller posts index show的文件,则文件所有者为r
这个问题在这里已经有了答案: Get Component's Parent Form (10 个答案) 关闭 3 年前。 如何从嵌入到表单中的自定义 Winforms 组件获取父/所有者控件? 我有
我这里有密码 @interface FFRightSidebarController () @property (nonatomic, strong) FFActivitiesController *
假设您有这些表:RestaurantChains、Restaurants、MenuItems - 它们之间存在明显的关系。现在,您有表 Comments 和 Ratings,它们存储客户对链式店、餐馆
我有以下 View 层次结构, ... ... .... 单个订单的更新通过推送器进行。 我是 React 新手,想知道以下哪一个是更
所以我有一个位于另一个用户控件内部的用户控件。我们称它们为 ParentUC 和 ChildUC。我需要从 ChildUC 获取 ParentUC。 我知道获取窗口所有者的方法是Window.GetW
我们创建了 Telegram Bot ,它有许多困难的操作。机器人是由一位开发人员使用他的电话号码创建的。是否可以更改机器人的所有者或添加其他用户作为机器人的管理员? 最佳答案 最近出现了将机器人转给
默认情况下,对象(表、存储过程等)是使用 dbo 所有者/架构设置的(我认为 ms sql 2000 称其为所有者,而 ms sql 2005 称其为架构) 所有者/架构实际上是数据库中的角色或用户。
在 Mortar 中,如果 ActionBar 根据显示的屏幕发生变化,我很好奇人们如何处理它。例如,假设您想要在显示特定屏幕时更改 ActionBar 标题或操作。 注入(inject) Activ
我正在使用 Stripe.js 和 Stripe Elements 开发购物车结帐页面,而不是使用 Stripe 的结帐小部件。 在结帐页面上,使用了所有 4 个可用元素(卡片、邮政编码、expire
我尝试将本地仓库推送到 github 并收到这样的消息: The remote end hung up unexpectedly. ERROR: Permission to [repo_name] d
我是 WHM、cPanel 和 CentOS 的新手。 我安装 WHM 然后为域创建一个帐户 app.example.com和用户 peter我将域名指向正确的 IP 地址,但是当我运行我的网站 ap
我已经在 GitHub 上提交了一个项目的问题,该项目不是我的,我也不是贡献者,但我找不到标记我的问题的方法。有没有办法让我给它贴上标签,或者这只适用于贡献者? 最佳答案 它仅适用于贡献者。 这样,您
我到处都找过了,但一直找不到我要找的东西。我知道我需要什么,但无法将 2 和 2 放在一起。 我需要允许用户创建群组并允许其他用户加入群组。 任何用户都可以创建群组。 任何用户都可以发送加入另一个群组
我到处都找过了,但一直找不到我要找的东西。我知道我需要什么,但无法将 2 和 2 放在一起。 我需要允许用户创建群组并允许其他用户加入群组。 任何用户都可以创建群组。 任何用户都可以发送加入另一个群组
这是我的代码: class SpecialMeanings{ String prop1 = "prop1" def closure = { String prop1 = "inner_
我一直在使用 java OWNER 进行基于属性的配置。 我创建了一个静态方法 public static final ApplicationConfiguration config = Config
我正在运行 OpenSSH sftp-server(Linux、Raspbian),FileZilla 用作客户端。我遇到的问题是用户可以删除服务器上的任何文件,而不管文件掩码或所有者/组: 登录的用
这是一个简单的问题,我无法通过谷歌搜索和查看 github documents 找到答案。 . 如果有人对 github 中已关闭的问题发表新评论,是否会通知 Repo 所有者? 最佳答案 如果向已关
我是一名优秀的程序员,十分优秀!