Quellcode durchsuchen

优化OAuth2 client api

zhouhao vor 8 Jahren
Ursprung
Commit
538db8178e
15 geänderte Dateien mit 397 neuen und 50 gelöschten Zeilen
  1. 124 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/AccessTokenInfo.java
  2. 30 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/GrantType.java
  3. 8 16
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ServerConfig.java
  4. 30 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2RequestBuilder.java
  5. 27 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2RequestBuilderFactory.java
  6. 0 1
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2RequestService.java
  7. 0 8
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2SessionBuilder.java
  8. 45 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/exception/OAuth2RequestException.java
  9. 4 6
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/listener/OAuth2CodeAuthBeforeEvent.java
  10. 5 12
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/OAuth2Request.java
  11. 10 2
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/OAuth2Session.java
  12. 29 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ReTry.java
  13. 28 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/TokenExpiredCallBack.java
  14. 29 5
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/response/OAuth2Response.java
  15. 28 0
      hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/response/ResponseConvert.java

+ 124 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/AccessTokenInfo.java

@@ -0,0 +1,124 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *  
+ *  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 org.hswebframework.web.authorization.oauth2.client;
+
+/**
+ * 默认的服务实现
+ *
+ * @author zhouhao
+ */
+public class AccessTokenInfo {
+    //授权码
+    private String  accessToken;
+    //更新码
+    private String  refreshToken;
+    //有效期
+    private Integer expiresIn;
+    //授权范围
+    private String  scope;
+
+    private Long createTime;
+
+    private Long updateTime;
+
+    private String tokenType;
+
+    public boolean isExpire() {
+        return updateTime != null && System.currentTimeMillis() - updateTime > expiresIn * 1000;
+    }
+
+    public String getTokenType() {
+        return tokenType;
+    }
+
+    public void setTokenType(String tokenType) {
+        this.tokenType = tokenType;
+    }
+
+    /**
+     * @return 授权码
+     */
+    public String getAccessToken() {
+        return this.accessToken;
+    }
+
+    /**
+     * 设置 授权码
+     */
+    public void setAccessToken(String accessToken) {
+        this.accessToken = accessToken;
+    }
+
+    /**
+     * @return 更新码
+     */
+    public String getRefreshToken() {
+        return this.refreshToken;
+    }
+
+    /**
+     * 设置 更新码
+     */
+    public void setRefreshToken(String refreshToken) {
+        this.refreshToken = refreshToken;
+    }
+
+    /**
+     * @return 有效期
+     */
+    public Integer getExpiresIn() {
+        return this.expiresIn;
+    }
+
+    /**
+     * 设置 有效期
+     */
+    public void setExpiresIn(Integer expiresIn) {
+        this.expiresIn = expiresIn;
+    }
+
+    /**
+     * @return 授权范围
+     */
+    public String getScope() {
+        return this.scope;
+    }
+
+    /**
+     * 设置 授权范围
+     */
+    public void setScope(String scope) {
+        this.scope = scope;
+    }
+
+    public Long getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Long createTime) {
+        this.createTime = createTime;
+    }
+
+    public Long getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Long updateTime) {
+        this.updateTime = updateTime;
+    }
+}

+ 30 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/GrantType.java

@@ -0,0 +1,30 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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 org.hswebframework.web.authorization.oauth2.client;
+
+/**
+ * @author zhouhao
+ */
+public interface GrantType {
+    String authorization_code = "authorization_code";
+    String implicit           = "implicit";
+    String password           = "password";
+    String client_credentials = "client_credentials";
+    String refresh_token      = "refresh_token";
+}

+ 8 - 16
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ServerConfig.java

@@ -23,20 +23,12 @@ package org.hswebframework.web.authorization.oauth2.client;
  *
  * @author zhouhao
  */
-public interface OAuth2ServerConfig {
-    String name = "name";
-
-    String describe = "describe";
-
-    String apiBaseUrl = "apiBaseUrl";
-
-    String authUrl = "authUrl";
-
-    String accessTokenUrl = "accessTokenUrl";
-
-    String clientId = "clientId";
-
-    String clientSecret = "clientSecret";
-
-
+public interface OAuth2Constants {
+    String access_token  = "access_token";
+    String grant_type    = "grant_type";
+    String scope         = "scope";
+    String client_id     = "client_id";
+    String client_secret = "client_secret";
+    String authorization = "Authorization";
+    String redirect_uri  = "redirect_uri";
 }

+ 30 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2RequestBuilder.java

@@ -0,0 +1,30 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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 org.hswebframework.web.authorization.oauth2.client;
+
+import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Request;
+
+/**
+ * @author zhouhao
+ */
+public interface OAuth2RequestBuilder {
+    OAuth2RequestBuilder url(String url);
+
+    OAuth2Request build();
+}

+ 27 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2RequestBuilderFactory.java

@@ -0,0 +1,27 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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 org.hswebframework.web.authorization.oauth2.client;
+
+/**
+ *
+ * @author zhouhao
+ */
+public interface OAuth2RequestBuilderFactory {
+    OAuth2RequestBuilder create(String serverId,String provider);
+}

+ 0 - 1
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2RequestService.java

@@ -34,7 +34,6 @@ public interface OAuth2RequestService {
      *
      * @param serverId 服务ID,serverId是由接口的实现模块自行定义的
      * @return OAuth2会话创建器
-     * @see OAuth2ServerConfig
      * @see OAuth2SessionBuilder
      */
     OAuth2SessionBuilder create(String serverId);

+ 0 - 8
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2SessionBuilder.java

@@ -54,14 +54,6 @@ public interface OAuth2SessionBuilder {
      */
     OAuth2Session byPassword(String username, String password);
 
-    /**
-     * 根据简化模式创建会话
-     *
-     * @return 会话
-     * @see "grant_type=implicit"
-     */
-    OAuth2Session byImplicit();
-
     /**
      * 直接指定accessToken创建会话
      *

+ 45 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/exception/OAuth2RequestException.java

@@ -0,0 +1,45 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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 org.hswebframework.web.authorization.oauth2.client.exception;
+
+import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
+
+/**
+ *
+ * @author zhouhao
+ */
+public class OAuth2RequestException extends RuntimeException {
+    OAuth2Response.ErrorType errorType;
+
+    OAuth2Response response;
+
+    public OAuth2RequestException(OAuth2Response.ErrorType errorType, OAuth2Response response) {
+        super(errorType.name());
+        this.errorType = errorType;
+        this.response = response;
+    }
+
+    public OAuth2Response.ErrorType getErrorType() {
+        return errorType;
+    }
+
+    public OAuth2Response getResponse() {
+        return response;
+    }
+}

+ 4 - 6
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/listener/OAuth2CodeAuthBeforeEvent.java

@@ -23,15 +23,14 @@ import java.util.Optional;
 import java.util.function.Function;
 
 /**
- *
  * @author zhouhao
  */
 public class OAuth2CodeAuthBeforeEvent implements OAuth2Event {
     private String                   code;
     private String                   state;
-    private Function<String, Object> parameterGetter;
+    private Function<String, String> parameterGetter;
 
-    public OAuth2CodeAuthBeforeEvent(String code, String state, Function<String, Object> parameterGetter) {
+    public OAuth2CodeAuthBeforeEvent(String code, String state, Function<String, String> parameterGetter) {
         this.code = code;
         this.state = state;
         this.parameterGetter = parameterGetter;
@@ -45,9 +44,8 @@ public class OAuth2CodeAuthBeforeEvent implements OAuth2Event {
         return state;
     }
 
-    @SuppressWarnings("unchecked")
-    public <T> Optional<T> getParameter(String name) {
-        return Optional.ofNullable((T) parameterGetter.apply(name));
+    public Optional<String> getParameter(String name) {
+        return Optional.ofNullable(parameterGetter.apply(name));
     }
 
 }

+ 5 - 12
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/OAuth2Request.java

@@ -28,14 +28,8 @@ import java.util.function.Consumer;
  * @author zhouhao
  */
 public interface OAuth2Request {
-    /**
-     * 设置路径参数,如url为:/user/{userId} .设置pathParam("userId","admin"),将自动解析url为 /user/admin
-     *
-     * @param name  参数名称
-     * @param value 参数值
-     * @return request自身
-     */
-    OAuth2Request pathParam(String name, Object value);
+
+    OAuth2Request onTokenExpired(TokenExpiredCallBack callback);
 
     /**
      * 设置请求参数,相当于/url?name=value
@@ -52,7 +46,7 @@ public interface OAuth2Request {
      * @param value 请求内容
      * @return request自身
      */
-    OAuth2Request requestBody(Object value);
+    OAuth2Request requestBody(String value);
 
     /**
      * 设置请求头
@@ -66,11 +60,10 @@ public interface OAuth2Request {
     /**
      * 设置cookie
      *
-     * @param name  名称
-     * @param value 值
+     * @param cookie 值
      * @return request自身
      */
-    OAuth2Request cookie(String name, String value);
+    OAuth2Request cookie(String cookie);
 
     /**
      * 设置请求的contentType

+ 10 - 2
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/OAuth2Session.java

@@ -18,6 +18,9 @@
 
 package org.hswebframework.web.authorization.oauth2.client.request;
 
+import org.hswebframework.web.authorization.annotation.Authorize;
+import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
+
 import java.io.Serializable;
 
 /**
@@ -30,11 +33,11 @@ import java.io.Serializable;
  */
 public interface OAuth2Session extends Serializable {
     /**
-     * 连接会话
+     * 尝试进行认证
      *
      * @return 会话自身
      */
-    OAuth2Session connect();
+    OAuth2Session authorize();
 
     /**
      * 发起一个OAuth2请求,参数为接口地址
@@ -53,6 +56,8 @@ public interface OAuth2Session extends Serializable {
      */
     OAuth2Session param(String name, Object value);
 
+    OAuth2Session scope(String scope);
+
     /**
      * 关闭会话,将清空
      */
@@ -62,4 +67,7 @@ public interface OAuth2Session extends Serializable {
      * @return 是否已关闭
      */
     boolean isClosed();
+
+    AccessTokenInfo getAccessToken();
+
 }

+ 29 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ReTry.java

@@ -0,0 +1,29 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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 org.hswebframework.web.authorization.oauth2.client.request;
+
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public interface ReTry {
+    void doReTry();
+}

+ 28 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/TokenExpiredCallBack.java

@@ -0,0 +1,28 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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 org.hswebframework.web.authorization.oauth2.client.request;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public interface TokenExpiredCallBack {
+    void call(ReTry reTry);
+}

+ 29 - 5
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/response/OAuth2Response.java

@@ -18,8 +18,10 @@
 
 package org.hswebframework.web.authorization.oauth2.client.response;
 
+import org.hswebframework.web.authorization.oauth2.client.exception.OAuth2RequestException;
+
 import java.util.List;
-import java.util.function.Function;
+import java.util.function.BiConsumer;
 
 /**
  * OAuth2 请求结果
@@ -27,6 +29,24 @@ import java.util.function.Function;
  * @author zhouhao
  */
 public interface OAuth2Response {
+    enum ErrorType {
+        ILLEGAL_CODE, //错误的授权码
+        ILLEGAL_ACCESS_TOKEN, //错误的access_token
+        ILLEGAL_CLIENT_ID,//客户端信息错误
+        ILLEGAL_CLIENT_SECRET,//客户端信息错误
+        ILLEGAL_GRANT_TYPE, //错误的授权方式
+        ILLEGAL_RESPONSE_TYPE,//response_type 错误
+        ILLEGAL_AUTHORIZATION,//Authorization 错误
+        ILLEGAL_REFRESH_TOKEN,//refresh_token 错误
+        ILLEGAL_REDIRECT_URI, //redirect_url 错误
+        UNAUTHORIZED_CLIENT, //无权限
+        EXPIRED_TOKEN, //TOKEN过期
+        INVALID_TOKEN, //TOKEN已失效
+        UNSUPPORTED_GRANT_TYPE, //不支持的认证类型
+        UNSUPPORTED_RESPONSE_TYPE, //不支持的响应类型
+        ACCESS_DENIED, //访问被拒绝
+        OTHER //其他错误
+    }
 
     /**
      * @return 结果转为字符串
@@ -41,11 +61,11 @@ public interface OAuth2Response {
     /**
      * 自定义转换方式
      *
-     * @param exchangeFunction 转换函数
-     * @param <T>              转换结果类型
+     * @param convert 转换函数
+     * @param <T>     转换结果类型
      * @return 转换结果
      */
-    <T> T as(Function<OAuth2Response, T> exchangeFunction);
+    <T> T as(ResponseConvert<T> convert);
 
     /**
      * 转换为指定的类型
@@ -75,5 +95,9 @@ public interface OAuth2Response {
      *
      * @return 响应结果本身
      */
-    OAuth2Response ifSuccess();
+    OAuth2Response onError(BiConsumer<OAuth2Response, ErrorType> onError);
+
+    BiConsumer<OAuth2Response, ErrorType> throwOnError = (response, errorType) -> {
+        throw new OAuth2RequestException(errorType, response);
+    };
 }

+ 28 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/response/ResponseConvert.java

@@ -0,0 +1,28 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *
+ *  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 org.hswebframework.web.authorization.oauth2.client.response;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public interface ResponseConvert<T> {
+    T convert(OAuth2Response response);
+}