当前位置: 首页 > news >正文

JAVA Http Basic auth

 

一、Http  Base Auth 方式

当访问一个Http Basic Auth 网站的时候需要提供用户名,密码,否则会返回401 (without authoration)。

Http Basic Authentication认证 有2种方式:

1、请求头部Authorization 中添加 用户名/密码 的base64 编码字符串。

2、url中拼用户名和密码。

 

市面上大部分浏览器支持url拼用户名,密码的方式访问,

代码不支持这种url 拼用户名,密码。

 

二、Http Basic Auth 原理

在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份认证的方法,当一个客户端向HTTP服务 器进行数据请求时,

如果客户端未被认证,则HTTP服务器将通过基本认证过程对客户端的用户名及密码进行验证,以决定用户是否合法。

客户端在接收到HTTP服务器的身份认证要求后,会提示用户输入用户名及密码, 用户输入后,

客户端将用户名和密码中间用“:”分隔合并,并将合并后的字符串用BASE64编码,在每次请求数据 时,将密文附加于请求头(Request Header)Authorization: Basic XXXXXXX中。

HTTP服务器在每次收到请求包后,根据协议取得客户端附加的用户信息(BASE64编码的用户名和密码),解开请求包,对用户名及密码进行验证,

如果用 户名及密码正确,则根据客户端请求,返回客户端所需要的数据;否则,返回错误代码或重新要求客户端提供用户名及密码。

 

 

 

三、Basic Auth  的优缺点

优点:提供简单的用户验证功能,其认证过程简单明了,适合于对安全性要求不高的系统或设备中。

缺点:输入的用户名,密码 base64编码后会出现在Authorization里,很容易被解析出来。


/* * ==================================================================== * 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(); } } }

转载于:https://www.cnblogs.com/xiaocandou/p/7991927.html

相关文章:

  • 如何两个栈实现队列?两个队列实现栈?
  • Java之字符流操作-复制文件
  • 判断是否长按某一键
  • 【Android】封装一个简单好用的打印Log的工具类
  • centos7 防火墙 开启端口 并测试
  • 设计模式
  • IDA.快捷键_ZC收集
  • 直接从google中引入jquery.js
  • Sql注入攻击
  • linux下vsftpd客户端时间不一致问题
  • 3.2 使用STC89C52控制MC20发送短信
  • POJ 2823 Sliding Window 单调队列
  • 别人做的扫地机器人,有机会我也想搞一台!
  • backgroundworker与Thread区别
  • 数据类型--字符串
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • bearychat的java client
  • DOM的那些事
  • ESLint简单操作
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • If…else
  • Vue ES6 Jade Scss Webpack Gulp
  • 翻译--Thinking in React
  • 浮现式设计
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 深入浅出Node.js
  • 树莓派 - 使用须知
  • 主流的CSS水平和垂直居中技术大全
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • 数据可视化之下发图实践
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • $NOIp2018$劝退记
  • (1)常见O(n^2)排序算法解析
  • (30)数组元素和与数字和的绝对差
  • (Ruby)Ubuntu12.04安装Rails环境
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (强烈推荐)移动端音视频从零到上手(下)
  • (一) storm的集群安装与配置
  • .NET Core 和 .NET Framework 中的 MEF2
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .net 无限分类
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • .NET/C# 使用反射调用含 ref 或 out 参数的方法
  • [AI]ChatGPT4 与 ChatGPT3.5 区别有多大
  • [android] 切换界面的通用处理
  • [ArcPy百科]第三节: Geometry信息中的空间参考解析
  • [error] 17755#0: *58522 readv() failed (104: Connection reset by peer) while reading upstream
  • [ES-5.6.12] x-pack ssl
  • [Flutter]打包IPA
  • [IE编程] WebBrowser控件中设置页面的缩放
  • [jQuery]使用jQuery.Validate进行客户端验证(中级篇-上)——不使用微软验证控件的理由...