feat: sa-token-quick-login 插件支持 Http Basic 方式通过认证

This commit is contained in:
click33
2025-04-02 02:54:50 +08:00
parent 18ab60d4d2
commit a7f178da53
8 changed files with 223 additions and 28 deletions

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed 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.
*/
package cn.dev33.satoken.httpauth.basic;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.util.SaFoxUtil;
/**
* Sa-Token Http Basic 账号
*
* @author click33
* @since 1.41.0
*/
public class SaHttpBasicAccount {
/**
* 账号
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 构造函数
* @param username 账号
* @param password 密码
*/
public SaHttpBasicAccount(String username, String password) {
this.username = username;
this.password = password;
}
/**
* 构造函数
* @param usernameAndPassword 账号和密码,冒号隔开
*/
public SaHttpBasicAccount(String usernameAndPassword) {
if(SaFoxUtil.isEmpty(usernameAndPassword)) {
throw new SaTokenException("UsernameAndPassword 不能为空");
}
String[] arr = usernameAndPassword.split(":");
if(arr.length != 2) {
throw new SaTokenException("UsernameAndPassword 格式错误正确格式为username:password");
}
this.username = arr[0];
this.password = arr[1];
}
/**
* 获取 账号
*
* @return username 账号
*/
public String getUsername() {
return this.username;
}
/**
* 设置 账号
*
* @param username 账号
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取 密码
*
* @return password 密码
*/
public String getPassword() {
return this.password;
}
/**
* 设置 密码
*
* @param password 密码
*/
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "SaHttpBasicAccount{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

View File

@@ -45,7 +45,7 @@ public class SaHttpBasicTemplate {
}
/**
* 获取浏览器提交的 Basic 参数 (裁剪掉前缀并解码)
* 获取浏览器提交的 Http Basic 参数 (裁剪掉前缀并解码)
* @return 值
*/
public String getAuthorizationValue() {
@@ -61,7 +61,19 @@ public class SaHttpBasicTemplate {
// 裁剪前缀并解码
return SaBase64Util.decode(authorization.substring(6));
}
/**
* 获取 Http Basic 账号密码对象
* @return /
*/
public SaHttpBasicAccount getHttpBasicAccount() {
String authorizationValue = getAuthorizationValue();
if(authorizationValue == null) {
return null;
}
return new SaHttpBasicAccount(authorizationValue);
}
/**
* 对当前会话进行 Basic 校验(使用全局配置的账号密码),校验不通过则抛出异常
*/

View File

@@ -32,13 +32,21 @@ public class SaHttpBasicUtil {
public static SaHttpBasicTemplate saHttpBasicTemplate = new SaHttpBasicTemplate();
/**
* 获取浏览器提交的 Basic 参数 (裁剪掉前缀并解码)
* 获取浏览器提交的 Http Basic 参数 (裁剪掉前缀并解码)
* @return 值
*/
public static String getAuthorizationValue() {
return saHttpBasicTemplate.getAuthorizationValue();
}
/**
* 获取 Http Basic 账号密码对象
* @return /
*/
public static SaHttpBasicAccount getHttpBasicAccount() {
return saHttpBasicTemplate.getHttpBasicAccount();
}
/**
* 对当前会话进行 Basic 校验(使用全局配置的账号密码),校验不通过则抛出异常
*/