您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Java中,HTTP协议支持多种认证方式,主要包括以下几种:
Authorization
HTTP Header中发送,通常以Basic
关键字开始,后面是一个base64编码的用户名:密码值。HttpClient client = HttpClient.newBuilder().authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password".toCharArray());
}
}).build();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpHost targetHost = new HttpHost("example.com", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));
AuthCache authCache = new BasicAuthCache();
DigestScheme digestScheme = new DigestScheme();
digestScheme.setRealm("Example Realm");
digestScheme.setAlgorithm(DigestScheme.ALGORITHM_SHA256);
authCache.put(targetHost, digestScheme);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpGet httpget = new HttpGet("/protected-resource");
CloseableHttpResponse response = httpClient.execute(targetHost, httpget, context);
以上就是Java HTTP协议中常见的认证方式。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。