JAVA中HTTP基本認(rèn)證(Basic Authentication)實(shí)現(xiàn)
什么是 HTTP 基本認(rèn)證
HTTP 基本認(rèn)證是一種簡單的認(rèn)證方法??蛻舳丝梢酝ㄟ^用戶名和密碼進(jìn)行認(rèn)證。這些憑證以特定的格式在 Authorization HTTP Header 中發(fā)送。一般它以 Basic 關(guān)鍵字開始,后面是一個(gè) base64 編碼的用戶名:密碼值。冒號(hào)字符在這里很重要。頭部應(yīng)該嚴(yán)格遵循這個(gè)格式。
例如,要用 javanorth 用戶名和 http 密碼進(jìn)行認(rèn)證,我們必須發(fā)送這個(gè) Header。
Basic amF2YW5vcnRoOmh0dHA=
我們可以通過使用 base64 解碼器和檢查解碼的結(jié)果來驗(yàn)證。
服務(wù)端這么做
- 服務(wù)端告知客戶端使用 Basic Authentication 方式進(jìn)行認(rèn)證
- 服務(wù)端接收并處理客戶端按照 Basic Authentication 方式發(fā)送的數(shù)據(jù)
服務(wù)端告知客戶端使用 Basic Authentication 方式進(jìn)行認(rèn)證
- 服務(wù)端返回 401(Unauthozied)狀態(tài)碼給客戶端
- 服務(wù)端在Response 的 header “WWW-Authenticate” 中添加信息

服務(wù)端接收并處理客戶端按照 Basic Authentication 方式發(fā)送的數(shù)據(jù)
private boolean checkBasicAuthorization(HttpServletRequest request) {
String rawStringAuthorization = request.getHeader("Authorization");
Assert.isTrue(StringUtils.startsWith(rawStringAuthorization, "Basic"), "Basic 認(rèn)證失敗");
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 認(rèn)證失敗");
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 方式向服務(wù)端發(fā)送數(shù)據(jù)
如果客戶端是瀏覽器
瀏覽器支持 Basic Authentication 方式認(rèn)證。瀏覽器會(huì)自動(dòng)彈出提示窗體,并自動(dòng)向該地址發(fā)送認(rèn)證請(qǐng)求。
瀏覽器自動(dòng)彈出的對(duì)話框:

點(diǎn)擊“登錄”后,瀏覽器自動(dòng)向該地址發(fā)送請(qǐng)求:

- 輸入用戶名:
myuser,密碼:mypassword “bXl1c2VyOm15cGFzc3dvcmQ=”=base64("myuser:mypassword")
如果客戶端是 RestTemplat
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors()
.add(new BasicAuthenticationInterceptor("myuser","mypassword"));
;
return restTemplate;
}
}
如果客戶端是 HttpClient
略
其它
Basic Authentication 方式的認(rèn)證,通常不需要登錄頁面,只需要登錄Action即可。

參考
https://developer.atlassian.com/server/jira/platform/basic-authentication/
到此這篇關(guān)于JAVA中HTTP基本認(rèn)證(Basic Authentication)的文章就介紹到這了,更多相關(guān)JAVA HTTP基本認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?boot整合dubbo+zookeeper的詳細(xì)過程
本文講解Spring?Boot整合Dubbo與Zookeeper實(shí)現(xiàn)API、Provider、Consumer模式,包含依賴配置、啟動(dòng)類注解、服務(wù)注冊(cè)與調(diào)用步驟,及QoS、超時(shí)、負(fù)載均衡等參數(shù)設(shè)置,確保分布式服務(wù)通信正常運(yùn)行,感興趣的朋友一起看看吧2025-07-07
Springboot項(xiàng)目快速實(shí)現(xiàn)過濾器功能
上篇文章已經(jīng)給大家介紹了Springboot項(xiàng)目如何快速實(shí)現(xiàn)Aop功能,這篇文章給大家介紹Springboot項(xiàng)目如何快速實(shí)現(xiàn)過濾器功能,感興趣的小伙伴可以參考閱讀2023-03-03
sms4j?2.0?全新來襲功能的調(diào)整及maven變化詳解
這篇文章主要介紹了sms4j?2.0?全新來襲功能的調(diào)整及maven變化詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

