JAVA Http Basic auth

/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package org.apache.http.examples.client;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * An example of HttpClient can be customized to authenticate
 * preemptively using BASIC scheme.
 * <b>
 * Generally, preemptive authentication can be considered less
 * secure than a response to an authentication challenge
 * and therefore discouraged.
 */
public class ClientPreemptiveBasicAuthentication {

    public static void main(String[] args) throws Exception {
        HttpHost target = new HttpHost("httpbin.org", 80, "http");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials("user", "passwd"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider).build();
        try {

            // Create AuthCache instance
            AuthCache authCache = new BasicAuthCache();
            // Generate BASIC scheme object and add it to the local
            // auth cache
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(target, basicAuth);

            // Add AuthCache to the execution context
            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            HttpGet httpget = new HttpGet("http://httpbin.org/hidden-basic-auth/user/passwd");

            System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
            for (int i = 0; i < 3; i++) {
                CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
                try {
                    System.out.println("----------------------------------------");
                    System.out.println(response.getStatusLine());
                    System.out.println(EntityUtils.toString(response.getEntity()));
                } finally {
                    response.close();
                }
            }
        } finally {
            httpclient.close();
        }
    }

}
时间: 2024-10-16 04:26:24

JAVA Http Basic auth的相关文章

java 发送带Basic Auth认证的http post请求

构造http header private static final String URL = "url"; private static final String APP_KEY = "key"; private static final String SECRET_KEY = "secret"; /** * 构造Basic Auth认证头信息 * * @return */ private String getHeader() { String

elasticsearch&#39;s basic auth plugin &quot;Jetty&quot;

Elasticsearch本身是沒有安全性的功能, 我們得透過plugin來安裝, 這次我選擇了elasticsearch-jetty,此外也有人用nginx反向代理來進行控制. 首先我們得先注意到Elasticsearch與Jetty Plugin的版本號對應, 在這裡可以查看, 如果版本號沒對應上就無法正常工作了. 這次範例是用1.2.1版本(Elasticsearch & Jetty Plugin), 到es的bin目錄下執行 plugin -url https://oss-es-plug

Nginx下配置Http Basic Auth保护目录

nginx basic auth指令 语法:     auth_basic string | off;默认值:     auth_basic off;配置段:     http, server, location, limit_except 默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示. 语法:     auth_basic_user_file file;默认值:     -配置段:     http, server, location, limit_except 1. 下载这个

ios开发使用Basic Auth 认证方式

我们app的开发通常有2种认证方式   一种是Basic Auth,一种是OAuth:现在普遍还是使用OAuth的多,而使用Basic Auth认证的少,正好呢我今天给大家介绍的就是使用的比较少的Badic Auth认证方式,这种认证方式开发和调试简单, 没有复杂的页面跳转逻辑和交互过程,更利于发起方控制.然而缺点就是安全性更低,不过也没事,我们可以使用https安全加密协议,这样才更安全. 我使用的是AFNetworking发送的网络请求,因此我们用Basic Auth认证方式就不能再使用AF

HTTP验证大法(Basic Auth,Session, JWT, Oauth, Openid)

本文标签:   HTTP Auth OpenID HTTP验证大法 Session REST   服务器 成为一个"认证"老司机 本文翻译自 Auth-Boss . 如果有翻译的不恰当或不对的地方, 欢迎指出. 成为一个认证老司机, 了解网络上不同的身份认证方法. 本文档的目的是记录和编目Web上的身份验证方法. 认证指的是创建一个系统的过程,用户可以通过该系统"登录"在线服务,并授予对受保护资源的访问权限. 以下引用可能更好地总结我想要解释的内容: 客户端认证涉及

Basic Auth

开放平台 把网站服务封装成一系列接口供第三方开发者使用,这种行为就叫做Open API,提供开放API的平台本身就被称为开放平台.比如一些网站支持QQ登录,那QQ就相当于开放平台,QQ提供了一些OPEN API,只要你提供了正确的参数,就可以访问这些资源(比如某个用于的昵称). 供第三方使用就涉及到验证和授权的过程.通常使用Basic Auth和OAuth两种方式. Basic Auth  Basic Auth使用base64编码把username和password加密后放入请求头,不安全,但是

nginx 添加basic auth

生成密码文件 htpasswd -c -d conf/htpasswd kibanaprintf "kibana:$(openssl passwd -crypt 4Bo*3Jdhk)\n" >>htpasswd 添加配置文件 server {} 中添加,还可以在http,location等配置段中添加auth_basic "nginx basic auth";auth_basic_user_file htpasswd; nginx的启动和停止 /usr/

Jmeter实现Basic Auth方式登录

背景 在实际测试日常中,产品有两种登录方式,一种是普通登录方式,另一种则是Basic Auth方式登录.两种登录对应着产品中不同的操作,对权限进行了一个划分. Postman登录 使用Postman登录,如下方式入参,即可正常登录成功 Jmeter登录 同样的一个登录操作,换到jmeter中实现时,倒遇到了不小的麻烦,琢磨了些时间,终究是解决了,分享出来与大家共享. 1.遇到的问题 切换到jmeter进行登录时,接口返回401,并报Unauthorized.401 是一个什么概念呢:“未经授权”

Basic Auth攻击

Basic Auth攻击 1.Basic Auth认证简介 Basic身份认证,是HTTP 1.0中引入的认证方案之一.虽然方案比较古老,同时存在安全缺陷,但由于实现简单,至今仍有不少网站在使用它. 例如Apche Tomcat.路由器等. 2.认证步骤 1.如下,用户访问受限资源 /protected_docs.请求报文如下: GET /protected_docs HTTP/1.1 Host: 127.0.0.1:3000 2.服务端发现 /protected_docs 为受限资源,于是向用