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

JAVA 中 HTTP 基本认证(Basic Authentication)

目录

  • 服务端这么做
    • 服务端告知客户端使用 Basic Authentication 方式进行认证
    • 服务端接收并处理客户端按照 Basic Authentication 方式发送的数据
  • 客户端这么做
    • 如果客户端是浏览器
    • 如果客户端是 RestTemplat
    • 如果客户端是 HttpClient
  • 其它
  • 参考

服务端这么做

  1. 服务端告知客户端使用 Basic Authentication 方式进行认证
  2. 服务端接收并处理客户端按照 Basic Authentication 方式发送的数据

服务端告知客户端使用 Basic Authentication 方式进行认证

  • 服务端返回 401(Unauthozied)状态码给客户端
  • 服务端在Response 的 header “WWW-Authenticate” 中添加信息

在这里插入图片描述

服务端接收并处理客户端按照 Basic Authentication 方式发送的数据

private boolean checkBasicAuthorization(HttpServletRequest request) {String rawStringAuthorization = request.getHeader("Authorization");Assert.isTrue(StringUtils.startsWith(rawStringAuthorization, "Basic"), "Basic 认证失败");String base64StringAuthorization = StringUtils.replaceOnce(rawStringAuthorization, "Basic", "");base64StringAuthorization = StringUtils.trim(base64StringAuthorization);byte[] bytesAuthorization = Base64Utils.decodeFromString(base64StringAuthorization);String stringAuthorization = new String(bytesAuthorization);String[] arrUserAndPass = StringUtils.split(stringAuthorization, ":");Assert.isTrue(2==arrUserAndPass.length, "Basic 认证失败");String username = arrUserAndPass[0];String password = arrUserAndPass[1];if (StringUtils.equals(username, "myuser") && StringUtils.equals(password, "mypassword")) {return true;}return false;
}
  • org.apache.commons.lang3.StringUtils
  • org.springframework.util.Base64Utils

客户端这么做

客户端按照 Basic Authentication 方式向服务端发送数据

如果客户端是浏览器

浏览器支持 Basic Authentication 方式认证。浏览器会自动弹出提示窗体,并自动向该地址发送认证请求。

浏览器自动弹出的对话框:
在这里插入图片描述
点击“登录”后,浏览器自动向该地址发送请求:
在这里插入图片描述

  • 输入用户名:myuser,密码:mypassword
  • “bXl1c2VyOm15cGFzc3dvcmQ=” = base64("myuser:mypassword")

如果客户端是 RestTemplat

@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate() {RestTemplate restTemplate = new RestTemplate();restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("myuser","mypassword")); 
;return restTemplate;}
}

如果客户端是 HttpClient

其它

Basic Authentication 方式的认证,通常不需要登录页面,只需要登录Action即可。
在这里插入图片描述

参考

https://developer.atlassian.com/server/jira/platform/basic-authentication/

http://www.lryc.cn/news/350872.html

相关文章:

  • 【Flutter】 webview请求权限问题
  • rocketmq 学习二 基本概念
  • C++初阶学习第十弹——探索STL奥秘(五)——深入讲解vector的迭代器失效问题
  • C#自动实现缺陷数据增强
  • JPHS-JMIR Public Health and Surveillance
  • Flutter 中的 AnimatedThere 小部件:全面指南
  • 2024南京智博会:展示国内外前沿科技成果,推动智能产业快速发展
  • 基于springboot实现的校园博客系统
  • 人从胚胎开始就要交税,直到死亡,是这样吗?
  • c语言指针入门(二)
  • 一篇讲透排序算法之插入排序and选择排序
  • CompletableFuture的主要用途是什么?
  • QtCreator,动态曲线实例
  • Model-Based Pose Estimation for Rigid Objects(基于SIFT)
  • STM32自己从零开始实操02:输入部分原理图
  • JavaScript异步编程——03-Ajax传输json和XML的技术文档
  • 移动端常用meta
  • C++_C++11的学习
  • RAC11G参数修改错误导致启库失败处理
  • UE4打包Win64项目命令行
  • c语言bug汇总中篇5
  • 【linux】进程(一)
  • 手把手教你用Python轻松玩转SQL注入
  • redis的几种部署模式及注意事项
  • 使用Python生成一束玫瑰花
  • 紫光同创PGL22G开发板|盘古22K开发板,国产FPGA开发板,接口丰富
  • 大模型的实践应用24-LLaMA-Factory微调通义千问qwen1.5-1.8B模型的实例
  • 力扣爆刷第142天之二叉树五连刷(构造树、搜索树)
  • 0407放大电路的频率响应
  • 数据分析必备:一步步教你如何用Pandas做数据分析(6)