如何使用 Apache Camel 路由从授权服务器获取访问令牌? [英] How to get access token from Authorisation Server using Apache Camel routes?
问题描述
我有一个授权服务器 [用 @SpringBootApplication
注释的简单类,@RestController
,@Configuration
,@EnableAuthorizationServer
&oauth2 安全性] 运行在端口 8081
上,工作正常 &使用 POST
方法从 POSTMAN 请求时提供访问令牌以及键值对形式的必要参数,http://localhost:8080/oauth/token
,但是我应该如何在java中实现camel路由通过在body中传递参数来获取访问令牌?
I have an authorization server [Simple Class annotated with @SpringBootApplication
,
@RestController
,@Configuration
,@EnableAuthorizationServer
& oauth2 security] running on port 8081
which works fine & provides the access token when requested from POSTMAN using POST
method along with needful parameters in the form of key value pair,
http://localhost:8080/oauth/token
, but how should i implement the camel route in java to get the access token by passing parameters in body ?
推荐答案
这个问题更多是关于使用 Apache Camel 发送 multipart/form-data
.前段时间我在玩它,并使用自定义处理器解决了它,将标题转换为 multipart/form-data
格式,并使用 Content-Disposition: form-data
.
This question is more about sending multipart/form-data
with Apache Camel. I was playing with it some time ago and solved it with custom Processor, converting headers to multipart/form-data
format with Content-Disposition: form-data
.
这是我的处理器将标头转换为 multipart/form-data
格式:
This is my Processor converting headers to multipart/form-data
format:
public class PrepareMultipartFormData implements Processor {
private String[] multipartHeaders;
public PrepareMultipartFormData(String... multipartHeaders) {
this.multipartHeaders = multipartHeaders;
}
@Override
public void process(Exchange exchange) throws Exception {
addMultipart(exchange.getIn(), multipartHeaders);
}
private static void addMultipart(Message message, String... multipartKeys){
final String boundary = "---------------------------"+RandomStringUtils.randomAlphanumeric(9);
message.setHeader(Exchange.CONTENT_TYPE, "multipart/form-data;boundary="+boundary);
StringBuilder sb = new StringBuilder("--").append(boundary);
for (String key: multipartKeys) {
sb.append("\r\n")
.append("Content-Disposition: form-data; name=\"").append(key).append("\"")
.append("\r\n\r\n")
.append(message.getHeader(key, String.class))
.append("\r\n")
.append("--").append(boundary);
}
message.setBody(sb.toString());
}
}
您需要向 OAuth 请求令牌发送:
- HTTP 标头
- 授权标头 - 这是由端点选项
指定的标准HTTP组件的一部分authUsername
和authPassword
- Content-Type - 这是添加到我的
PrepareMultipartFormData
处理器
- HTTP headers
- Authorization header - This is part of standard HTTP component specified by endpoint options
authUsername
andauthPassword
- Content-Type - This is added in my
PrepareMultipartFormData
Processor
- grant_type
- 用户名
- 密码
- client_id
最终路线可以这样实现:
(用一些表达式替换常量,以动态设置它.如果您只需要token
响应,请添加一些解组,因为此路由返回 JSON)Final route can be implemented in this way:
(Replace constants with some expressions, to set it dynamically. If you need onlytoken
in response, add some unmarshalling, since this route returns JSON)from("direct:getTokenResponse") .setHeader(Exchange.HTTP_METHOD, constant("POST")) .setHeader(Exchange.HTTP_PATH, constant("oauth/token")) .setHeader("grant_type", constant("password")) .setHeader("username", constant("admin")) .setHeader("password", constant("admin1234")) .setHeader("client_id", constant("spring-security-oauth2-read-write-client")) .process(new PrepareMultipartFormData("grant_type", "username", "password", "client_id")) .to("http://localhost:8080?authMethod=Basic&authUsername=oauth-endpoint-username&authPassword=oauth-endpoint-password") .convertBodyTo(String.class) .to("log:response");
<小时>
Updating answer to provide a bit shorter implementation of
PrepareMultipartFormData#addMultipart
using MultipartEntityBuilder.private static void addMultipart(Message message, String... multipartKeys) throws Exception{ MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (String key: multipartKeys) { builder.addTextBody(key, message.getHeader(key, String.class)); } HttpEntity resultEntity = builder.build(); message.setHeader(Exchange.CONTENT_TYPE, resultEntity.getContentType().getValue()); message.setBody(resultEntity.getContent()); }
这篇关于如何使用 Apache Camel 路由从授权服务器获取访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Authorization header - This is part of standard HTTP component specified by endpoint options
- 授权标头 - 这是由端点选项