Apache Shiro authcBasic authentication using Java and apache HttpClient
2 answers
This worked for me
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 9009),
new UsernamePasswordCredentials("username", "password*"));
HttpGet httpget = new HttpGet("http://localhost:9009/path/list");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
System.out.println("executing request" + httpget.getRequestLine());
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
System.out.println("Job Done!");
} catch (Exception ex) {
Logger.getLogger(Command.class.getName()).log(Level.SEVERE, null, ex);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
0
source to share
You can use Java url with Authenticator
Below is an example of using Java URL to access SVN http repository using Basic Authentication:
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;
/**
* Created with IntelliJ IDEA.
* User: Omar MEBARKI
* To change this template use File | Settings | File Templates.
*/
public class URLConfiguration {
private URL configURL;
public URLConfiguration(final String login, final String password, String httpURL) throws Exception {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(login, password.toCharArray());
}
});
this.configURL = new URL(httpURL);
}
public Properties getConfiguration() throws Exception {
Properties props = new Properties();
props.load(configURL.openStream());
return props;
}
}
0
source to share