Browse Source

修复OAuth2 无法获取token

zhouhao 8 years ago
parent
commit
c35dc35dd6

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

@@ -17,6 +17,8 @@
  */
 package org.hswebframework.web.authorization.oauth2.client;
 
+import com.alibaba.fastjson.annotation.JSONField;
+
 /**
  * 默认的服务实现
  *
@@ -24,10 +26,13 @@ package org.hswebframework.web.authorization.oauth2.client;
  */
 public class AccessTokenInfo {
     //授权码
+    @JSONField(name = "access_token")
     private String  accessToken;
     //更新码
+    @JSONField(name = "refresh_token")
     private String  refreshToken;
     //有效期
+    @JSONField(name = "expires_in")
     private Integer expiresIn;
     //授权范围
     private String  scope;
@@ -36,6 +41,7 @@ public class AccessTokenInfo {
 
     private Long updateTime;
 
+    @JSONField(name = "token_type")
     private String tokenType;
 
     public boolean isExpire() {

+ 7 - 2
hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-client/src/main/java/org/hswebframework/web/example/oauth2/OAuth2ClientApplication.java

@@ -23,6 +23,7 @@ import org.hsweb.ezorm.rdb.executor.SqlExecutor;
 import org.hswebframework.web.authorization.Authentication;
 import org.hswebframework.web.authorization.AuthenticationManager;
 import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestService;
+import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session;
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
 import org.hswebframework.web.authorization.shiro.oauth2sso.OAuth2SSOAuthorizingListener;
 import org.hswebframework.web.commons.entity.DataStatus;
@@ -45,6 +46,8 @@ import org.springframework.jdbc.datasource.DataSourceUtils;
 import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * TODO 完成注释
@@ -67,12 +70,14 @@ public class OAuth2ClientApplication implements CommandLineRunner {
         // 而且暂时没有实现默认的OAuth2相关的权限获取策略,
         // 所以这里使用通过OAuth2进行获取
         // 实现类似sso的功能,这里实际上应该将权限信息存储起来
+        Map<String, OAuth2Session> sessionMap = new HashMap<>();
+
         return new AuthenticationManager() {
             @Override
             public Authentication getByUserId(String userId) {
                 //获取远程的用户权限信息
-                return oAuth2RequestService.create("hsweb-oauth-server")
-                        .byClientCredentials()
+                return sessionMap.computeIfAbsent("auth", key -> oAuth2RequestService.create("hsweb-oauth-server")
+                        .byClientCredentials())
                         .request("oauth2/user-auth-info/" + userId)
                         .get().onError(OAuth2Response.throwOnError)
                         .as(Authentication.class);

+ 22 - 0
hsweb-examples/hsweb-examples-oauth2/hsweb-examples-oauth2-server/src/main/java/org/hswebframework/web/example/oauth2/OAuth2ServerApplication.java

@@ -33,6 +33,7 @@ import org.hswebframework.web.dao.oauth2.OAuth2ClientDao;
 import org.hswebframework.web.entity.authorization.*;
 import org.hswebframework.web.entity.authorization.bind.BindPermissionRoleEntity;
 import org.hswebframework.web.entity.authorization.bind.BindRoleUserEntity;
+import org.hswebframework.web.service.authorization.AuthorizationSettingService;
 import org.hswebframework.web.service.authorization.PermissionService;
 import org.hswebframework.web.service.authorization.RoleService;
 import org.hswebframework.web.service.authorization.UserService;
@@ -79,6 +80,8 @@ public class OAuth2ServerApplication implements CommandLineRunner {
     @Autowired
     OAuth2ClientDao   oAuth2ClientDao;
 
+    @Autowired
+    AuthorizationSettingService authorizationSettingService;
 
     @Override
     public void run(String... strings) throws Exception {
@@ -133,6 +136,25 @@ public class OAuth2ServerApplication implements CommandLineRunner {
         roleEntity.setPermissions(Arrays.asList(permissionRoleEntity));
         roleService.insert(roleEntity);
 
+          /*            权限设置        */
+        AuthorizationSettingEntity settingEntity = entityFactory.newInstance(AuthorizationSettingEntity.class);
+
+        settingEntity.setType("role"); //绑定到角色
+        settingEntity.setSettingFor(roleEntity.getId());
+
+        settingEntity.setDescribe("测试");
+        //权限配置详情
+        AuthorizationSettingDetailEntity detailEntity = entityFactory.newInstance(AuthorizationSettingDetailEntity.class);
+        detailEntity.setPermissionId(permission.getId());
+        detailEntity.setMerge(true);
+        detailEntity.setPriority(1L);
+        detailEntity.setActions(new HashSet<>(Arrays.asList(Permission.ACTION_QUERY, Permission.ACTION_UPDATE)));
+        detailEntity.setDataAccesses(Arrays.asList(accessEntity, updateAccessEntity));
+
+        settingEntity.setDetails(Arrays.asList(detailEntity));
+
+        authorizationSettingService.insert(settingEntity);
+
         BindRoleUserEntity userEntity = entityFactory.newInstance(BindRoleUserEntity.class);
         userEntity.setId("admin");
         userEntity.setName("admin");

+ 7 - 0
hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-simple/src/main/java/org/hswebframework/web/service/oauth2/client/simple/provider/HswebResponseConvertSupport.java

@@ -19,8 +19,11 @@
 package org.hswebframework.web.service.oauth2.client.simple.provider;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.parser.Feature;
+import com.alibaba.fastjson.parser.ParserConfig;
 import org.hswebframework.web.authorization.Authentication;
 import org.hswebframework.web.authorization.builder.AuthenticationBuilderFactory;
+import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
 import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
 import org.hswebframework.web.service.oauth2.client.request.ProviderSupport;
 import org.hswebframework.web.service.oauth2.client.request.definition.ResponseConvertForProviderDefinition;
@@ -28,6 +31,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 
 /**
  * TODO 完成注释
@@ -54,6 +59,8 @@ public class HswebResponseConvertSupport implements ResponseConvertForProviderDe
             } else {
                 throw new UnsupportedOperationException("authenticationBuilderFactory not ready");
             }
+        }else if(type == AccessTokenInfo.class){
+            Map<String,Object> jsonMap = JSON.parseObject(json);
         }
         return JSON.parseObject(json, type);
     }