如何使用Apache Camel路由从授权服务器获取访问令牌? [英] How to get access token from Authorisation Server using Apache Camel routes?

查看:177
本文介绍了如何使用Apache Camel路由从授权服务器获取访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个授权服务器[用@SpringBootApplication注释的简单类, @RestController@Configuration@EnableAuthorizationServer&在端口8081上运行的[oauth2安全性]正常&当使用POST方法从POSTMAN请求时提供访问令牌,以及以键值对形式提供的必要参数, http://localhost:8080/oauth/token,但是我应该如何在Java中实现骆驼路由以通过在正文中传递参数来获取访问令牌?

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有关.一段时间前,我在玩它,并使用自定义处理器解决了该问题,并使用Content-Disposition: form-data将标头转换为multipart/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标头
    • 授权标头-这是由端点选项authUsername指定的标准 HTTP组件的一部分和authPassword
    • Content-Type-这已添加到我的PrepareMultipartFormData处理器
    • 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
      • 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 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");
      


      使用PrepareMultipartFormData#addMultipart较短的实现/MultipartEntityBuilder.html"rel =" nofollow noreferrer> MultipartEntityBuilder .


      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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆