我的 REST 应用程序使用 Shiro 基本身份验证来保护 REST 端点,并且在从浏览器进行测试时运行良好。
现在我希望能够使用 Apache HttpClient 从 java 客户端登录应用程序
有什么想法吗?
谢谢
最佳答案
您可以将 Java URL 与身份 validator 一起使用
下面是一个使用 Java URL 访问 SVN http 存储库并使用基本身份验证的示例:
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;
}
}
关于java - Apache Shiro authc 使用 Java 和 apache HttpClient 进行基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14873628/