Basic Authentication with Resteasy client

后端 未结 4 1370
囚心锁ツ
囚心锁ツ 2021-02-20 05:57

I\'m trying to perform an basic auth to the login-module which runs on my jboss using REST. I already found an StackOverflow topic which explains how to authenticate with creden

4条回答
  •  星月不相逢
    2021-02-20 06:57

    You can add a raw authorization header to your REST client by invoking .header(HttpHeaders.AUTHORIZATION, authHeader) in your client configuration. The credentials must be packed in authorization header in the format of "user:pass", encoded as base64 byte array and then appended to the string "Basic " which identifies basic auth.

    This is the whole snippet (inspired by this post on baeldung)

        String auth = userName + ":" + password;
        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("ISO-8859-1")));
        String authHeader = "Basic " + new String(encodedAuth);
    
        authToken = restClient.target(restApiUrl + loginPath)
                .request()
                .accept(MediaType.TEXT_PLAIN)
                .header(HttpHeaders.AUTHORIZATION, authHeader)
                .get(String.class);
    

    This worked for me in a Resteasy client. For information, when testing this with wget I had to use the --auth-no-challenge flag.

提交回复
热议问题