问题
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 ?
回答1:
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
.
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());
}
}
To OAuth request token you need to send:
- 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
- Authorization header - This is part of standard HTTP component specified by endpoint options
- Form data - These are converted from headers in
PrepareMultipartFormData
Processor- grant_type
- username
- password
- client_id
Final route can be implemented in this way:
(Replace constants with some expressions, to set it dynamically. If you need only token
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());
}
来源:https://stackoverflow.com/questions/50196703/how-to-get-access-token-from-authorisation-server-using-apache-camel-routes