How to get access token from Authorisation Server using Apache Camel routes?

扶醉桌前 提交于 2019-12-22 08:13:01

问题


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 and authPassword
    • Content-Type - This is added in my PrepareMultipartFormData Processor
  • 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!