Procházet zdrojové kódy

优化redirectUri校验

zhouhao před 4 roky
rodič
revize
9b7cabb5b5

+ 7 - 2
hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/OAuth2Client.java

@@ -2,6 +2,9 @@ package org.hswebframework.web.oauth2.server;
 
 import lombok.Getter;
 import lombok.Setter;
+import org.hswebframework.web.oauth2.ErrorType;
+import org.hswebframework.web.oauth2.OAuth2Exception;
+import org.springframework.util.StringUtils;
 
 import javax.validation.constraints.NotBlank;
 
@@ -26,8 +29,10 @@ public class OAuth2Client {
     //client 所属用户
     private String userId;
 
-    public void validateRedirectUri(String redirectUri){
-
+    public void validateRedirectUri(String redirectUri) {
+        if (StringUtils.isEmpty(redirectUri) || (!redirectUri.startsWith(this.redirectUrl))) {
+            throw new OAuth2Exception(ErrorType.ILLEGAL_REDIRECT_URI);
+        }
     }
 
 }

+ 20 - 0
hsweb-authorization/hsweb-authorization-oauth2/src/test/java/org/hswebframework/web/oauth2/server/OAuth2ClientTest.java

@@ -0,0 +1,20 @@
+package org.hswebframework.web.oauth2.server;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class OAuth2ClientTest {
+
+    @Test
+    public void test(){
+        OAuth2Client client=new OAuth2Client();
+
+        client.setRedirectUrl("http://hsweb.me/callback");
+
+        client.validateRedirectUri("http://hsweb.me/callback");
+
+        client.validateRedirectUri("http://hsweb.me/callback?a=1&n=1");
+
+    }
+}