mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2025-12-06 08:48:27 +08:00
🎨 增加部分工具类及方法、重载AuthState部分方法
This commit is contained in:
parent
350f9d70ae
commit
5694d48288
@ -4,6 +4,7 @@ import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.config.AuthSource;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthResponseStatus;
|
||||
|
||||
@ -30,6 +31,16 @@ public class AuthState {
|
||||
*/
|
||||
private static ConcurrentHashMap<String, String> stateBucket = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 生成随机的state
|
||||
*
|
||||
* @param source oauth平台
|
||||
* @return state
|
||||
*/
|
||||
public static String create(AuthSource source) {
|
||||
return create(source.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机的state
|
||||
*
|
||||
@ -158,6 +169,15 @@ public class AuthState {
|
||||
stateBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录成功后,清除state
|
||||
*
|
||||
* @param source oauth平台
|
||||
*/
|
||||
public static void delete(AuthSource source) {
|
||||
delete(source.name());
|
||||
}
|
||||
|
||||
private static String getCurrentIp() {
|
||||
String currentIp = IpUtils.getIp();
|
||||
return StringUtils.isEmpty(currentIp) ? EMPTY_STR : currentIp;
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
package me.zhyd.oauth.utils;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
@ -12,9 +14,7 @@ import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 全局的工具类
|
||||
@ -82,6 +82,19 @@ public class GlobalAuthUtil {
|
||||
return res;
|
||||
}
|
||||
|
||||
public static String parseMapToString(Map<String, Object> params, boolean encode) {
|
||||
List<String> paramList = new ArrayList<>();
|
||||
params.forEach((k, v) -> {
|
||||
if (ObjectUtil.isNull(v)) {
|
||||
paramList.add(k + "=");
|
||||
} else {
|
||||
String valueString = v.toString();
|
||||
paramList.add(k + "=" + (encode ? urlEncode(valueString) : valueString));
|
||||
}
|
||||
});
|
||||
return CollUtil.join(paramList, "&");
|
||||
}
|
||||
|
||||
public static boolean isHttpProtocol(String url) {
|
||||
if (StringUtils.isEmpty(url)) {
|
||||
return false;
|
||||
|
||||
70
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
Normal file
70
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
Normal file
@ -0,0 +1,70 @@
|
||||
package me.zhyd.oauth.utils;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 构造URL
|
||||
* </p>
|
||||
*
|
||||
* @author yangkai.shen
|
||||
* @date Created in 2019-07-18 15:47
|
||||
*/
|
||||
@Data
|
||||
public class UrlBuilder {
|
||||
private String baseUrl;
|
||||
|
||||
private final Map<String, Object> params = new LinkedHashMap<>(4);
|
||||
|
||||
/**
|
||||
* @param baseUrl 基础路径
|
||||
* @return the new {@code UrlBuilder}
|
||||
*/
|
||||
public static UrlBuilder fromBaseUrl(String baseUrl) {
|
||||
UrlBuilder builder = new UrlBuilder();
|
||||
builder.setBaseUrl(baseUrl);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加参数
|
||||
*
|
||||
* @param key 参数名称
|
||||
* @param value 参数值
|
||||
* @return this UrlBuilder
|
||||
*/
|
||||
public UrlBuilder queryParam(String key, Object value) {
|
||||
Assert.notBlank(key, "参数名不能为空");
|
||||
|
||||
String valueAsString = (value != null ? value.toString() : null);
|
||||
this.params.put(key, valueAsString);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造url
|
||||
*
|
||||
* @return url
|
||||
*/
|
||||
public String build() {
|
||||
return this.build(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造url
|
||||
*
|
||||
* @param encode 转码
|
||||
* @return url
|
||||
*/
|
||||
public String build(boolean encode) {
|
||||
String baseUrl = StrUtil.addSuffixIfNot(this.baseUrl, "?");
|
||||
String paramString = GlobalAuthUtil.parseMapToString(this.params, encode);
|
||||
return baseUrl + paramString;
|
||||
}
|
||||
}
|
||||
39
src/test/java/me/zhyd/oauth/utils/UrlBuilderTest.java
Normal file
39
src/test/java/me/zhyd/oauth/utils/UrlBuilderTest.java
Normal file
@ -0,0 +1,39 @@
|
||||
package me.zhyd.oauth.utils;
|
||||
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.config.AuthSource;
|
||||
import me.zhyd.oauth.request.AuthWeChatRequest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* UrlBuilder测试类
|
||||
* </p>
|
||||
*
|
||||
* @author yangkai.shen
|
||||
* @date Created in 2019-07-18 16:36
|
||||
*/
|
||||
public class UrlBuilderTest {
|
||||
@Test
|
||||
public void testUrlBuilder() {
|
||||
AuthConfig config = new AuthConfig();
|
||||
config.setClientId("appid-110110110");
|
||||
config.setClientSecret("secret-110110110");
|
||||
config.setRedirectUri("https://xkcoding.com");
|
||||
config.setState(AuthState.create(AuthSource.WECHAT));
|
||||
// @formatter:off
|
||||
String build = UrlBuilder.fromBaseUrl(AuthSource.WECHAT.authorize())
|
||||
.queryParam("appid", config.getClientId())
|
||||
.queryParam("redirect_uri", config.getRedirectUri())
|
||||
.queryParam("response_type", "code")
|
||||
.queryParam("scope", "snsapi_login")
|
||||
.queryParam("state", config.getState().concat("#wechat_redirect"))
|
||||
.build(false);
|
||||
// @formatter:on
|
||||
AuthWeChatRequest request = new AuthWeChatRequest(config);
|
||||
String authorize = request.authorize();
|
||||
Assert.assertEquals(build, authorize);
|
||||
AuthState.delete(AuthSource.WECHAT);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user