身份验证AWS方法Rest DSL

时间:2018-03-14 17:21:13

标签: json rest oauth-2.0 apache-camel jax-rs

我对 Restful services

有疑问

我需要将CSV文件上传到AWS服务器。我在账户注册。 首先,我需要获取访问令牌并使用它来上传文件。我还没有编写任何代码,试图了解最佳方法,我希望使用Camel-Rest-DSL。需要与JSON通信。但是,身份验证部分让我陷入困境,我很确定它使用OAuth2身份验证,RestFul Web服务和JSON,这应该只是一个客户端,我正在寻找WSS4J for JAX-RS OAuth2,但我不知道。

我是用邮递员做的,这就是场景。用户名和密码是虚构的

    *Get Access Token  
                    uses POST verb
                    requires Token Request URL
    uses Basic Auth  requires Username = Client ID of tenant ( needs to be encoded base64 )
    HEADER parm Content-Type = x-www-form-urlencoded
    Content-Type': 'application/x-www-form-urlencoded',
                   'Authorization': 'Basic ' + encoded Client ID
    Access Token body - grant_type, username, password
    Body = username = 5c0642fe-a495-44db-93f7-67034556fa2c061_ingestor
                    password = 154f0252d166f27b5e21ef171b03212a79f41a0daf3
                    grant_type = password

    #returns the access_token as JSON

    POST or upload files
    uses POST verb
    requires Ingestion URL UploadURL

    UploadURL=https://apm-ts-query-svc-prd.app-api.aws-usw02-pr.something.io/v2/time_series/
    UploadFolder=upload
    headers = 
    key Authentication  "Bearer + access Token" (from request access token above)
    key Tenant = TenantUUID
    key X-GE-CsvFormat = ODB

    # Body
    form-data
    key file=file
    # POST DATA
    headers content-type application/json
                   authorization: "" + token
                   tenant: "" + tenant

我的环境

Jboss Fuse 6.3-310

Karaf版本2.4.0.redhat-630310

JVM   Java虚拟机Java HotSpot(TM)64位服务器VM版本25.162 -

B12   版本1.8.0_162   供应商Oracle Corporation

操作系统   名称Linux版本2.6.32-696.20.1.el6.x86_64

1 个答案:

答案 0 :(得分:0)

我不能使用OAuth2 / SAML断言,所以我只需要一个令牌并缓存它并稍后再使用它。这是我的测试代码

    @Override 
    public void configure() throws Exception { 

        //Configure the Rest Web Service Component transport to use a REST implementation
        restConfiguration() //Configures the REST DSL to use a specific REST implementation
            .component("jetty")         //Specifies the Camel component to use as the REST transport
            .host("localhost")          //The hostname to use for exposing the REST service
            .port("8282")           //The port number to use for exposing the REST service JMX tooling
            .scheme("https")            //The protocol to use for exposing the REST service
            .contextPath("/oauth/token")        //Sets a leading context path for the REST services
            .bindingMode(RestBindingMode.json)  //Enables binding mode for JSON 
            .jsonDataFormat("json-jackson")     //Specifies the component that Camel uses to implement the JSON data format
            .dataFormatProperty("prettyPrint", "true"); //set arbitrary properties on the underlying data format component

        //Configure the Rest Endpoint
        rest("/oauth")      //Defines a service using the REST DSL. Each of the verb clauses are terminated by a to() keyword,
                            //which forwards the incoming message to an endpoint 
            .post("/token")
            .produces("application/json")
            .consumes("application/json")
            .type(TokenEntities.class)
            .route()
            .routeId("Get Auth Token Route")
            .autoStartup(true)
            .id("Get Auth Token Service")
            .description("Get Authorization Token")
            .process(new UAARequestTokenProcessor())
            .to("https://d1e53858-2903-4c21-86c0-95edc7a5cef2.pager-uaa.run.aws-usw02-pr.ice.pager.io/oauth/token")
            .to("log:logger?showBody=true")
            .to("direct:accessToken")
            .endRest();

        //Define the Route - from() Defines a regular Camel route.
        from("direct:accessToken").to("log:logger?showBody=true"); }

public class UAARequestTokenProcessor implements Processor {

private static final Logger LOG = LoggerFactory.getLogger(UAARequestTokenProcessor.class);

private String clientId = "myClientID";
private String userName = "myUserName";
private String password = "myPassword";

@Override
public void process(Exchange exchange) throws Exception {

        LOG.info("Processing UAA token request for " + clientId + " and " + userName);

        Message msg = exchange.getOut(); //create outbound message exchange

        StringBuilder authHeader = new StringBuilder("Basic ");       
        authHeader.append(Base64.getEncoder().encodeToString((clientId + ":").getBytes("UTF_8")));

        String body = String.format("grant_type=password&username=%s&password=%s",
                  URLEncoder.encode(userName, "UTF-8"), //Translates a string into x-www-form-urlencoded format
                  URLEncoder.encode(password, "UTF-8"));

         msg.setHeader(Exchange.CONTENT_TYPE, "MediaType.APPLICATION_FORM_URLENCODE");
         msg.setHeader("Authorization", authHeader.toString());
         msg.setBody(body);
    }

}