代码之家  ›  专栏  ›  技术社区  ›  GLMills

身份验证AWS方法Rest DSL

  •  0
  • GLMills  · 技术社区  · 7 年前

    我有个问题 Restful服务

    我需要将CSV文件上载到AWS服务器。我已注册帐户。 首先,我需要获得一个访问令牌并使用它上载文件。我还没有编写任何代码,试图理解最好的方法,所以我希望使用驼峰Rest DSL。需要与JSON通信。但是,身份验证部分让我卡住了,我很确定它使用了OAuth2身份验证、RestFul web服务和JSON,这应该只是一个客户端,我正在查看用于JAX-RS OAuth2的WSS4J,但我不知道。

    我和邮递员一起做的,就是这样。用户名和密码是虚构的

        *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保险丝6.3-310

    Karaf版本2.4.0。redhat-630310

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

    b12级 版本1.8.0\u 162 供应商Oracle公司

    操作系统 将Linux版本命名为2.6.32-696.20.1。el6.x86\u 64

    1 回复  |  直到 7 年前
        1
  •  0
  •   GLMills    7 年前

    我不能使用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);
        }
    
    }