简体   繁体   中英

Calling RestAPI from java : No AuthenticationProvider found UsernamePasswordAuthenticationToken

I am just trying to call a rest api using the code below

SSLConnectionSocketFactory sslsf;
CredentialsProvider credentialsProvider;
HttpClient httpClient;

final static Logger LOGGER = Logger.getLogger(Constructor.class);

public Constructor() {
    try{
    sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault(), NoopHostnameVerifier.INSTANCE);

    credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
            ConfigData.getInstance().getSAEUsername(), ConfigData.getInstance().getSAEPassword()));
    httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(credentialsProvider)
            .build();
    }
    catch (Exception e) {
    }
}

public String callAPI() {
    String jsonResponse = "";
    try {
        String s = "111.11.11.11";   
        //String s1 = "username"; // this works fine
        String s1 = "domain\username"; //AuthenticationProvider found Error
        String s2 =  "password";    
        String endpoint = "https://" + s + ":3600/Xxxxxxx/Yyyyyy/Ccccccc";
        HttpGet request = new HttpGet(endpoint);
        request.addHeader("content-type", "application/json");
        request.addHeader("accept", "application/json");//

        byte[] credentials = Base64.encodeBase64(
                (s1 + ":" + s2)
                        .getBytes(StandardCharsets.UTF_8));
        String s3 = new String(credentials, StandardCharsets.UTF_8);
        request.addHeader("Authorization", "Basic " + s3);
        HttpResponse response = httpClient.execute(request);
        jsonResponse = EntityUtils.toString(response.getEntity());
        LOGGER.info("Response: " + jsonResponse);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return jsonResponse;
}

Here if the username is just "username" (Eg: admin) the i get proper response but if the username is part of a domain like "domain\\username"(Eg: DAAK\\admin) then i get below error in response:

<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 401 </title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing /Xxxxxxx/Yyyyyy/Ccccccc. Reason:
<pre>    No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
</body>
</html>

I do not know Srings framework. Is there a simple fox for this?

Basic authentification consist only of username:password, you can't use the domaine here.

Why do you need to provide the domain ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM