mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-27 02:59:02 +08:00
feat: sa-token-quick-login 插件支持 Http Basic 方式通过认证
This commit is contained in:
@@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 校验(使用全局配置的账号密码),校验不通过则抛出异常
|
||||
*/
|
||||
|
||||
@@ -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 校验(使用全局配置的账号密码),校验不通过则抛出异常
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user