diff --git a/src/main/java/me/zhyd/oauth/config/AuthConfig.java b/src/main/java/me/zhyd/oauth/config/AuthConfig.java index 6e72e826dda11874f22900392e04418615296074..f0eb3494943aa7c8dde91d619facfc8af0c5c947 100644 --- a/src/main/java/me/zhyd/oauth/config/AuthConfig.java +++ b/src/main/java/me/zhyd/oauth/config/AuthConfig.java @@ -6,7 +6,7 @@ import lombok.*; * JustAuth配置类 * * @author yadong.zhang (yadong.zhang0415(a)gmail.com) - * @version 1.0 + * @version 1.9.3 * @since 1.8 */ @Getter @@ -45,13 +45,6 @@ public class AuthConfig { */ private boolean unionId; - /** - * 一个神奇的参数,最好使用随机的不可测的内容,可以用来防止CSRF攻击 - *
- * 1.8.0版本新增参数 - */ - private String state; - /** * Stack Overflow Key *
diff --git a/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java index 07ce7da703a57a1aa77c681990c609d1c8a6dc28..f0c6f90edace21a98845b4b02f6db119efcee65d 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java @@ -86,17 +86,18 @@ public class AuthAlipayRequest extends AuthDefaultRequest { } /** - * 返回认证url,可自行跳转页面 + * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state} * + * @param state state 验证授权流程的参数,可以防止csrf * @return 返回授权地址 */ @Override - public String authorize() { + public String authorize(String state) { return UrlBuilder.fromBaseUrl(source.authorize()) .queryParam("app_id", config.getClientId()) .queryParam("scope", "auth_user") .queryParam("redirect_uri", config.getRedirectUri()) - .queryParam("state", getRealState(config.getState())) + .queryParam("state", getRealState(state)) .build(); } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java b/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java index 5ce2149a1ebdd3c4f4fa4314e8fbdf12f203f1cb..11492b555943e3b4c57f9fe2bbc97c7ed58381e7 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java @@ -79,18 +79,19 @@ public class AuthBaiduRequest extends AuthDefaultRequest { } /** - * 返回认证url,可自行跳转页面 + * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state} * + * @param state state 验证授权流程的参数,可以防止csrf * @return 返回授权地址 */ @Override - public String authorize() { + public String authorize(String state) { return UrlBuilder.fromBaseUrl(source.authorize()) .queryParam("response_type", "code") .queryParam("client_id", config.getClientId()) .queryParam("redirect_uri", config.getRedirectUri()) .queryParam("display", "popup") - .queryParam("state", getRealState(config.getState())) + .queryParam("state", getRealState(state)) .build(); } diff --git a/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java b/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java index 98e45bfdbec639cdce1631408c0b5c3cf4ff35ac..ae28769917db8cb7fdf22194b345eb1167c87a6a 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java @@ -4,11 +4,11 @@ import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONObject; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.enums.AuthUserGender; import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; -import me.zhyd.oauth.enums.AuthUserGender; import me.zhyd.oauth.utils.UrlBuilder; /** @@ -71,18 +71,19 @@ public class AuthCodingRequest extends AuthDefaultRequest { } /** - * 返回认证url,可自行跳转页面 + * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state} * + * @param state state 验证授权流程的参数,可以防止csrf * @return 返回授权地址 */ @Override - public String authorize() { + public String authorize(String state) { return UrlBuilder.fromBaseUrl(source.authorize()) .queryParam("response_type", "code") .queryParam("client_id", config.getClientId()) .queryParam("redirect_uri", config.getRedirectUri()) .queryParam("scope", "user") - .queryParam("state", getRealState(config.getState())) + .queryParam("state", getRealState(state)) .build(); } } diff --git a/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java index 1774a159d1a3b2f439878650c2386d1a0cce7ec9..7a8848ff4c2521e8f94ec69639a308264cb321ed 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java @@ -2,7 +2,6 @@ package me.zhyd.oauth.request; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; -import lombok.Data; import lombok.extern.slf4j.Slf4j; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthSource; @@ -43,7 +42,6 @@ public abstract class AuthDefaultRequest implements AuthRequest { public AuthResponse login(AuthCallback authCallback) { try { AuthChecker.checkCode(source == AuthSource.ALIPAY ? authCallback.getAuth_code() : authCallback.getCode()); - AuthChecker.checkState(authCallback.getState(), config.getState()); AuthToken authToken = this.getAccessToken(authCallback); AuthUser user = this.getUserInfo(authToken); @@ -64,16 +62,31 @@ public abstract class AuthDefaultRequest implements AuthRequest { /** * 返回认证url,可自行跳转页面 + *
+ * 不建议使用该方式获取授权地址,不带{@code state}的授权地址,容易受到csrf攻击。
+ * 建议使用{@link AuthDefaultRequest#authorize(String)}方法生成授权地址,在回调方法中对{@code state}进行校验
*
* @return 返回授权地址
*/
+ @Deprecated
@Override
public String authorize() {
+ return this.authorize(null);
+ }
+
+ /**
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
+ *
+ * @param state state 验证授权流程的参数,可以防止csrf
+ * @return 返回授权地址
+ */
+ @Override
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
+ .queryParam("state", getRealState(state))
.build();
}
@@ -130,7 +143,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
}
/**
- * 获取state,如果为空, 则默认去当前日期的时间戳
+ * 获取state,如果为空, 则默认取当前日期的时间戳
*
* @param state 原始的state
* @return 返回不为null的state
diff --git a/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java
index ce0f52fdde8ef4be657b9cd660edb7c425ea4ab8..ad29df0f8d35da8a3500fccbf7c929d430560d51 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java
@@ -58,18 +58,19 @@ public class AuthDingTalkRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("appid", config.getClientId())
.queryParam("scope", "snsapi_login")
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java
index a68f232291330759c870c35570926f7bbdb517f1..db9582c6096290e8b59f3e04ece71bfa2191758d 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java
@@ -89,18 +89,19 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_key", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
.queryParam("scope", "user_info")
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java b/src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
index f71378ced50992fc05c9ccba06b15ccaed3c5035..fb4a64a3f072180c5fdaaff85d91c931fb9e8942 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
@@ -63,12 +63,4 @@ public class AuthGithubRequest extends AuthDefaultRequest {
.build();
}
- /**
- * 检查响应内容是否正确
- *
- * @param object 请求响应内容
- */
- private void checkResponse(JSONObject object) {
-
- }
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java b/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
index 61b4f7fba2c0bf88139e33ccf5a38e86c2b9d281..a3033a160fdfbe0dbfe29bcb1967c445359afafb 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
@@ -61,19 +61,19 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
- * https://openidconnect.googleapis.com/v1/userinfo
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("scope", "openid%20email%20profile")
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java b/src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
index e22a7417c0360f7ecdae406a4ba3206b89bd4612..3deefb716eb3719af87ec4ff80e79cc405e668d4 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java
@@ -182,18 +182,19 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
.queryParam("scope", "r_liteprofile%20r_emailaddress%20w_member_social")
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java b/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
index 0ac9e00882d8a718cafc85e06e6caebf438b6b42..abdcb10b8c5d22d785f1dfd51d6daadafd313f6b 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java
@@ -109,19 +109,20 @@ public class AuthMiRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
.queryParam("scope", "user/profile%20user/openIdV2%20user/phoneAndEmail")
.queryParam("skip_confirm", "false")
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java b/src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
index addd187ede4c37328f6e7896b8b0ccb879abc795..4716c15e1f086e2aa9c6656b65111ae0772c7f0e 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java
@@ -102,19 +102,20 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
.queryParam("response_mode", "query")
.queryParam("scope", "offline_access%20user.read%20mail.read")
- .queryParam("state", getRealState(config.getState()))
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthPinterestRequest.java b/src/main/java/me/zhyd/oauth/request/AuthPinterestRequest.java
index a8f5c3835e3348196ef8ee3b8fe4d69bf2f08545..7c3f0001538c706559f2b1dcce71101a89e402b2 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthPinterestRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthPinterestRequest.java
@@ -69,14 +69,20 @@ public class AuthPinterestRequest extends AuthDefaultRequest {
return jsonObject.getJSONObject("60x60").getString("url");
}
+ /**
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
+ *
+ * @param state state 验证授权流程的参数,可以防止csrf
+ * @return 返回授权地址
+ */
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
.queryParam("scope", "read_public")
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthRequest.java b/src/main/java/me/zhyd/oauth/request/AuthRequest.java
index d06913ccbbd466d9ed8b19e73200d285bdc4602b..7b838e75515dd188491f05a96a4b7f0e10121fdc 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthRequest.java
@@ -18,10 +18,21 @@ public interface AuthRequest {
*
* @return 返回授权地址
*/
+ @Deprecated
default String authorize() {
throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
}
+ /**
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
+ *
+ * @param state state 验证授权流程的参数,可以防止csrf
+ * @return 返回授权地址
+ */
+ default String authorize(String state) {
+ throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
+ }
+
/**
* 第三方登录
*
diff --git a/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java b/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java
index c23439e75a48e6b4cbfb1bb9ba7c6b5b9121065a..cf438c272d76fd07250050ae961d2604303467ba 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java
@@ -67,14 +67,20 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
.build();
}
+ /**
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
+ *
+ * @param state state 验证授权流程的参数,可以防止csrf
+ * @return 返回授权地址
+ */
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
.queryParam("scope", "read_inbox")
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java b/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java
index 7a3b522eadbbbddabf848b03a66db274564a7a82..f350ccd693de15b1fa2b1e510ef89f5289a451ff 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java
@@ -4,11 +4,11 @@ import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
+import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
-import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.utils.GlobalAuthUtil;
import me.zhyd.oauth.utils.UrlBuilder;
@@ -55,18 +55,19 @@ public class AuthTaobaoRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
.queryParam("view", "web")
+ .queryParam("state", getRealState(state))
.build();
}
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java b/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java
index 7aaa77dc1d497812d4543508339a7abd5eac9519..5930aeb072f41c5c9c3eff74765d8449d2209401 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java
@@ -71,18 +71,19 @@ public class AuthTencentCloudRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
.queryParam("scope", "user")
- .queryParam("state", getRealState(config.getState()))
+ .queryParam("state", getRealState(state))
.build();
}
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java b/src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java
index 2a1c9790f0f85e7329bfbfc949f1449dff05a0cf..ab73d3a62f0096be0554ce381202803826cc03aa 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java
@@ -65,19 +65,20 @@ public class AuthToutiaoRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_key", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
- .queryParam("state", getRealState(config.getState()))
.queryParam("auth_only", 1)
.queryParam("display", 0)
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java b/src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
index dbc029a7f37a8a4f19adb9571077ce917a0a3d97..f22f35409bebde8d6fb19e3c0e8d66fbcbc664da 100644
--- a/src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
@@ -100,18 +100,19 @@ public class AuthWeChatRequest extends AuthDefaultRequest {
}
/**
- * 返回认证url,可自行跳转页面
+ * 返回带{@code state}参数的认证url,授权回调时会带上这个{@code state}
*
+ * @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
*/
@Override
- public String authorize() {
+ public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("appid", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
.queryParam("scope", "snsapi_login")
- .queryParam("state", getRealState(config.getState()).concat("#wechat_redirect"))
+ .queryParam("state", getRealState(state))
.build();
}
diff --git a/src/main/java/me/zhyd/oauth/utils/AuthChecker.java b/src/main/java/me/zhyd/oauth/utils/AuthChecker.java
index 33b59c3556bb3686a7644e0f24746d8462517ce6..ddb70beeafc3c49101a80b7fef13b9ef2ebd8e43 100644
--- a/src/main/java/me/zhyd/oauth/utils/AuthChecker.java
+++ b/src/main/java/me/zhyd/oauth/utils/AuthChecker.java
@@ -63,25 +63,4 @@ public class AuthChecker {
throw new AuthException(AuthResponseStatus.ILLEGAL_CODE);
}
}
-
- /**
- * 校验state的合法性防止被CSRF
- *
- * @param newState 新的state,一般为回调时传回的state(可能被篡改)
- * @param originalState 原始的state,发起授权时向第三方平台传递的state
- */
- public static void checkState(String newState, String originalState) {
- // 如果原始state为空,表示当前平台未使用state
- if (StringUtils.isEmpty(originalState)) {
- return;
- }
- // 如果授权之前使用了state,但是回调时未返回state,则表示当前请求为非法的请求,可能正在被CSRF攻击
- if (StringUtils.isEmpty(newState)) {
- throw new AuthException(AuthResponseStatus.ILLEGAL_REQUEST);
- }
- // 如果授权前后的state不一致,则表示当前请求为非法的请求,新的state可能为伪造
- if (!newState.equals(originalState)) {
- throw new AuthException(AuthResponseStatus.ILLEGAL_REQUEST);
- }
- }
}
diff --git a/src/main/java/me/zhyd/oauth/utils/AuthState.java b/src/main/java/me/zhyd/oauth/utils/AuthState.java
deleted file mode 100644
index 1ca1b70f3915ffb346159e49109065e0239cbdc4..0000000000000000000000000000000000000000
--- a/src/main/java/me/zhyd/oauth/utils/AuthState.java
+++ /dev/null
@@ -1,230 +0,0 @@
-package me.zhyd.oauth.utils;
-
-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;
-
-import java.nio.charset.Charset;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * state工具,负责创建、获取和删除state
- *
- * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
- * @version 1.0
- * @since 1.8
- */
-@Slf4j
-public class AuthState {
-
- /**
- * 空字符串
- */
- private static final String EMPTY_STR = "";
-
- /**
- * state存储器
- */
- private static ConcurrentHashMap