提交 5694d482 编写于 作者: 不合群的混子's avatar 不合群的混子

🎨 增加部分工具类及方法、重载AuthState部分方法

上级 350f9d70
...@@ -4,6 +4,7 @@ import cn.hutool.core.codec.Base64; ...@@ -4,6 +4,7 @@ import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponseStatus; import me.zhyd.oauth.model.AuthResponseStatus;
...@@ -30,6 +31,16 @@ public class AuthState { ...@@ -30,6 +31,16 @@ public class AuthState {
*/ */
private static ConcurrentHashMap<String, String> stateBucket = new ConcurrentHashMap<>(); 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 * 生成随机的state
* *
...@@ -158,6 +169,15 @@ public class AuthState { ...@@ -158,6 +169,15 @@ public class AuthState {
stateBucket.remove(key); stateBucket.remove(key);
} }
/**
* 登录成功后,清除state
*
* @param source oauth平台
*/
public static void delete(AuthSource source) {
delete(source.name());
}
private static String getCurrentIp() { private static String getCurrentIp() {
String currentIp = IpUtils.getIp(); String currentIp = IpUtils.getIp();
return StringUtils.isEmpty(currentIp) ? EMPTY_STR : currentIp; return StringUtils.isEmpty(currentIp) ? EMPTY_STR : currentIp;
......
package me.zhyd.oauth.utils; package me.zhyd.oauth.utils;
import cn.hutool.core.codec.Base64; 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 me.zhyd.oauth.exception.AuthException;
import javax.crypto.Mac; import javax.crypto.Mac;
...@@ -12,9 +14,7 @@ import java.nio.charset.Charset; ...@@ -12,9 +14,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.*;
import java.util.HashMap;
import java.util.Map;
/** /**
* 全局的工具类 * 全局的工具类
...@@ -82,6 +82,19 @@ public class GlobalAuthUtil { ...@@ -82,6 +82,19 @@ public class GlobalAuthUtil {
return res; 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) { public static boolean isHttpProtocol(String url) {
if (StringUtils.isEmpty(url)) { if (StringUtils.isEmpty(url)) {
return false; return false;
......
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;
}
}
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册