Przeglądaj źródła

优化权限控制

zhouhao 8 lat temu
rodzic
commit
b8f5d35977
9 zmienionych plików z 162 dodań i 641 usunięć
  1. 2 2
      hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DataAccessAnnotationMethodInterceptor.java
  2. 29 0
      hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/DefaultDataAccessFactory.java
  3. 0 308
      hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthentication.java
  4. 70 0
      hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthenticationBuilder.java
  5. 3 3
      hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java
  6. 0 38
      hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleDataAccessFactory.java
  7. 16 2
      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
  8. 0 288
      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/RemoteAuthentication.java
  9. 42 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/RemoteAuthenticationBuilder.java

+ 2 - 2
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DataAccessAnnotationMethodInterceptor.java

@@ -44,7 +44,7 @@ import java.util.stream.Collectors;
 /**
  * 数据级权限控制实现 <br>
  * 通过在方法上注解{@link RequiresDataAccess},标识需要进行数据级权限控制<br>
- * 控制的方式和规则由 {@link Permission#getDataAccessConfigs()}实现<br>
+ * 控制的方式和规则由 {@link Permission#getDataAccesses()}实现<br>
  *
  * @author zhouhao
  * @see DefaultDataAccessController
@@ -106,7 +106,7 @@ public class DataAccessAnnotationMethodInterceptor extends AuthorizingAnnotation
             List<String> actionList = Arrays.asList(accessAnn.action());
             //取得当前登录用户持有的控制规则
             Set<DataAccessConfig> accesses = permissionInfo
-                    .getDataAccessConfigs()
+                    .getDataAccesses()
                     .stream()
                     .filter(access -> actionList.contains(access.getAction()))
                     .collect(Collectors.toSet());

+ 29 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/DefaultDataAccessFactory.java

@@ -0,0 +1,29 @@
+package org.hswebframework.web.service.authorization.simple;
+
+import com.alibaba.fastjson.JSON;
+import org.hswebframework.web.authorization.access.DataAccessConfig;
+import org.hswebframework.web.authorization.builder.DataAccessConfigBuilderFactory;
+import org.hswebframework.web.entity.authorization.DataAccessEntity;
+import org.hswebframework.web.service.authorization.DataAccessFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+@Component
+public class DefaultDataAccessFactory implements DataAccessFactory {
+    private DataAccessConfigBuilderFactory dataAccessConfigBuilderFactory;
+
+    @Autowired
+    public void setDataAccessConfigBuilderFactory(DataAccessConfigBuilderFactory dataAccessConfigBuilderFactory) {
+        this.dataAccessConfigBuilderFactory = dataAccessConfigBuilderFactory;
+    }
+
+    @Override
+    public DataAccessConfig create(DataAccessEntity entity) {
+        return dataAccessConfigBuilderFactory.create().fromJson(JSON.toJSONString(entity)).build();
+    }
+}

+ 0 - 308
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthentication.java

@@ -1,308 +0,0 @@
-/*
- * 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.service.authorization.simple;
-
-import org.hswebframework.web.authorization.*;
-import org.hswebframework.web.authorization.access.DataAccessConfig;
-import org.hswebframework.web.authorization.access.FieldAccessConfig;
-import org.hswebframework.web.entity.authorization.*;
-import org.hswebframework.web.service.authorization.DataAccessFactory;
-
-import java.io.Serializable;
-import java.util.*;
-import java.util.stream.Collectors;
-
-/**
- * TODO 完成注释
- *
- * @author zhouhao
- */
-public class SimpleAuthentication implements Authentication {
-    private ReadOnlyUser user;
-
-    private List<Role> roles;
-
-    private List<Permission> permissions;
-
-    private Map<String, Serializable> attributes = new HashMap<>();
-
-    public SimpleAuthentication() {
-    }
-
-    public SimpleAuthentication(UserEntity user,
-                                List<RoleEntity> roleEntities,
-                                List<PermissionRoleEntity> permissionRoleEntities,
-                                DataAccessFactory dataAccessFactory) {
-        this.user = new ReadOnlyUser(user.getId(), user.getUsername(), user.getName());
-        this.roles = roleEntities.stream()
-                .map(roleEntity -> new ReadOnlyRole(roleEntity.getId(), roleEntity.getDescribe()))
-                .collect(Collectors.toList());
-        this.permissions = permissionRoleEntities.stream()
-                .map(permissionRoleEntity -> {
-                    ReadOnlyPermission permission = new ReadOnlyPermission(permissionRoleEntity.getPermissionId(), permissionRoleEntity.getActions());
-                    if (null != dataAccessFactory && null != permissionRoleEntity.getDataAccesses()) {
-                        permission.setDataAccessConfigs(permissionRoleEntity
-                                .getDataAccesses()
-                                .stream()
-                                .map(dataAccessFactory::create)
-                                .collect(Collectors.toSet()));
-                    }
-                    if (null != permissionRoleEntity.getFieldAccesses()) {
-                        permission.setFieldAccesses(permissionRoleEntity
-                                .getFieldAccesses()
-                                .stream()
-                                .map(SimpleFieldAccess::of)
-                                .collect(Collectors.toSet()));
-                    }
-                    return permission;
-                })
-                .collect(Collectors.toList());
-    }
-
-
-    @Override
-    public User getUser() {
-        return user;
-    }
-
-    @Override
-    public List<Role> getRoles() {
-        return new ArrayList<>(roles);
-    }
-
-    @Override
-    public List<Permission> getPermissions() {
-        return new ArrayList<>(permissions);
-    }
-
-    @Override
-    @SuppressWarnings("unchecked")
-    public <T extends Serializable> Optional<T> getAttribute(String name) {
-        return Optional.ofNullable((T) attributes.get(name));
-    }
-
-    @Override
-    public void setAttribute(String name, Serializable value) {
-        attributes.put(name, value);
-    }
-
-    public void setUser(ReadOnlyUser user) {
-        checkWritable(this.user);
-        this.user = user;
-    }
-
-    public void setRoles(List<Role> roles) {
-        checkWritable(this.roles);
-        this.roles = roles;
-    }
-
-    public void setPermissions(List<Permission> permissions) {
-        checkWritable(this.permissions);
-        this.permissions = permissions;
-    }
-
-    public void setAttributes(Map<String, Serializable> attributes) {
-        this.attributes = attributes;
-    }
-
-    @Override
-    @SuppressWarnings("unchecked")
-    public <T extends Serializable> T removeAttributes(String name) {
-        return (T) attributes.remove(name);
-    }
-
-    public Map<String, Serializable> getAttributes() {
-        return attributes;
-    }
-
-    public static class ReadOnlyPermission implements Permission {
-        private String                 id;
-        private Set<String>            actions;
-        private Set<SimpleFieldAccess> fieldAccesses;
-        private Set<DataAccessConfig>  dataAccessConfigs;
-
-        public ReadOnlyPermission() {
-        }
-
-        public ReadOnlyPermission(String id, Collection<String> actions) {
-            this.id = id;
-            this.actions = new HashSet<>(actions);
-        }
-
-        @Override
-        public String getId() {
-            return id;
-        }
-
-        public void setId(String id) {
-            checkWritable(this.id);
-            this.id = id;
-        }
-
-        @Override
-        public Set<String> getActions() {
-            if (actions == null) actions = Collections.emptySet();
-            return new HashSet<>(actions);
-        }
-
-        @Override
-        public Set<FieldAccessConfig> getFieldAccesses() {
-            if (fieldAccesses == null) fieldAccesses = Collections.emptySet();
-            return new HashSet<>(fieldAccesses);
-        }
-
-        public Set<DataAccessConfig> getDataAccessConfigs() {
-            if (dataAccessConfigs == null) dataAccessConfigs = Collections.emptySet();
-            return new HashSet<>(dataAccessConfigs);
-        }
-
-        public void setFieldAccesses(Set<SimpleFieldAccess> fieldAccesses) {
-            checkWritable(this.fieldAccesses);
-            this.fieldAccesses = fieldAccesses;
-        }
-
-        public void setDataAccessConfigs(Set<DataAccessConfig> dataAccessConfigs) {
-            checkWritable(this.dataAccessConfigs);
-            this.dataAccessConfigs = dataAccessConfigs;
-        }
-
-        public void setActions(Set<String> actions) {
-            checkWritable(this.actions);
-            this.actions = new HashSet<>(actions);
-        }
-    }
-
-    public static class SimpleFieldAccess implements FieldAccessConfig {
-        private String      field;
-        private Set<String> actions;
-
-        public static SimpleFieldAccess of(FieldAccessEntity entity) {
-            SimpleFieldAccess access = new SimpleFieldAccess();
-            access.setField(entity.getField());
-            access.setActions(new HashSet<>(entity.getActions()));
-            return access;
-        }
-
-        @Override
-        public String getField() {
-            return field;
-        }
-
-        @Override
-        public Set<String> getActions() {
-            return new HashSet<>(actions);
-        }
-
-        public void setField(String field) {
-            checkWritable(this.field);
-            this.field = field;
-        }
-
-        public void setActions(Set<String> actions) {
-            checkWritable(this.actions);
-            this.actions = actions;
-        }
-    }
-
-    public static class ReadOnlyRole implements Role {
-        private String id;
-
-        private String name;
-
-        public ReadOnlyRole() {
-        }
-
-        public ReadOnlyRole(String id, String name) {
-            this.id = id;
-            this.name = name;
-        }
-
-        @Override
-        public String getId() {
-            return id;
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        public void setId(String id) {
-            checkWritable(this.id);
-            this.id = id;
-        }
-
-        public void setName(String name) {
-            checkWritable(this.name);
-            this.name = name;
-        }
-    }
-
-    public static class ReadOnlyUser implements User {
-        private String id;
-
-        private String username;
-
-        private String name;
-
-        public ReadOnlyUser() {
-        }
-
-        public ReadOnlyUser(String id, String username, String name) {
-            this.id = id;
-            this.username = username;
-            this.name = name;
-        }
-
-        @Override
-        public String getId() {
-            return id;
-        }
-
-        @Override
-        public String getUsername() {
-            return username;
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        public void setId(String id) {
-            checkWritable(this.id);
-            this.id = id;
-        }
-
-        public void setUsername(String username) {
-            checkWritable(this.username);
-            this.username = username;
-        }
-
-        public void setName(String name) {
-            checkWritable(this.name);
-            this.name = name;
-        }
-    }
-
-    static final void checkWritable(Object obj) {
-        if (obj != null) {
-            throw new UnsupportedOperationException();
-        }
-    }
-}

+ 70 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthenticationBuilder.java

@@ -0,0 +1,70 @@
+/*
+ * 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.service.authorization.simple;
+
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.simple.*;
+import org.hswebframework.web.entity.authorization.PermissionRoleEntity;
+import org.hswebframework.web.entity.authorization.RoleEntity;
+import org.hswebframework.web.entity.authorization.UserEntity;
+import org.hswebframework.web.service.authorization.DataAccessFactory;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public class SimpleAuthenticationBuilder {
+
+    public static Authentication build(UserEntity user,
+                                       List<RoleEntity> roleEntities,
+                                       List<PermissionRoleEntity> permissionRoleEntities,
+                                       DataAccessFactory dataAccessFactory) {
+        SimpleAuthentication authentication = new SimpleAuthentication();
+        authentication.setUser(new SimpleUser(user.getId(), user.getUsername(), user.getName()));
+        authentication.setRoles(roleEntities.stream()
+                .map(roleEntity -> new SimpleRole(roleEntity.getId(), roleEntity.getName()))
+                .collect(Collectors.toList()));
+        authentication.setPermissions(permissionRoleEntities.stream()
+                .map(permissionRoleEntity -> {
+                    SimplePermission permission = new SimplePermission(permissionRoleEntity.getPermissionId(), new HashSet<>(permissionRoleEntity.getActions()));
+                    if (null != dataAccessFactory && null != permissionRoleEntity.getDataAccesses()) {
+                        permission.setDataAccesses(permissionRoleEntity
+                                .getDataAccesses()
+                                .stream()
+                                .map(dataAccessFactory::create)
+                                .collect(Collectors.toSet()));
+                    }
+                    if (null != permissionRoleEntity.getFieldAccesses()) {
+                        permission.setFieldAccesses(permissionRoleEntity
+                                .getFieldAccesses()
+                                .stream()
+                                .map(entity -> new SimpleFieldAccess(entity.getField(), new HashSet<>(entity.getActions())))
+                                .collect(Collectors.toSet()));
+                    }
+                    return permission;
+                })
+                .collect(Collectors.toList()));
+        return authentication;
+    }
+
+}

+ 3 - 3
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java

@@ -222,14 +222,14 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
         //用户持有的角色
         List<UserRoleEntity> roleEntities = userRoleDao.selectByUserId(userId);
         if (ListUtils.isNullOrEmpty(roleEntities)) {
-            return new SimpleAuthentication(userEntity, new ArrayList<>(), new ArrayList<>(), dataAccessFactory);
+            return SimpleAuthenticationBuilder.build(userEntity, new ArrayList<>(), new ArrayList<>(), dataAccessFactory);
         }
         List<String> roleIdList = roleEntities.stream().map(UserRoleEntity::getRoleId).collect(Collectors.toList());
 
         List<RoleEntity> roleEntityList = DefaultDSLQueryService.createQuery(roleDao).where().in(GenericEntity.id, roleIdList).noPaging().list();
         //权限角色关联信息
         List<PermissionRoleEntity> permissionRoleEntities = permissionRoleDao.selectByRoleIdList(roleIdList);
-        return new SimpleAuthentication(userEntity, roleEntityList, permissionRoleEntities, dataAccessFactory);
+        return SimpleAuthenticationBuilder.build(userEntity, roleEntityList, permissionRoleEntities, dataAccessFactory);
     }
 
     @Override
@@ -259,7 +259,7 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
             admin.setName("admin");
             roleEntityList.add(admin);
         }
-        return new SimpleAuthentication(userEntity, roleEntityList, permissionRoleEntities, dataAccessFactory);
+        return SimpleAuthenticationBuilder.build(userEntity, roleEntityList, permissionRoleEntities, dataAccessFactory);
     }
 
 

+ 0 - 38
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleDataAccessFactory.java

@@ -1,38 +0,0 @@
-package org.hswebframework.web.service.authorization.simple.access;
-
-import com.alibaba.fastjson.JSON;
-import org.hswebframework.web.authorization.access.DataAccessConfig;
-import org.hswebframework.web.entity.authorization.DataAccessEntity;
-import org.hswebframework.web.service.authorization.DataAccessFactory;
-import org.springframework.stereotype.Component;
-
-/**
- * TODO 完成注释
- *
- * @author zhouhao
- */
-@Component("simpleDataAccessFactory")
-public class SimpleDataAccessFactory implements DataAccessFactory {
-
-    @Override
-    public DataAccessConfig create(DataAccessEntity entity) {
-        AbstractDataAccess dataAccess = null;
-        try {
-            switch (entity.getType().toUpperCase()) {
-                case "CUSTOM":
-                    return dataAccess = new SimpleCustomDataAccess(entity.getConfig());
-                case "SCRIPT":
-                    return dataAccess = JSON.parseObject(entity.getConfig(), SimpleScriptDataAccess.class);
-                case "OWN_CREATED":
-                    return dataAccess = new SimpleOwnCreatedDataAccess();
-            }
-        } finally {
-            if (null != dataAccess) dataAccess.setAction(entity.getAction());
-        }
-        return createOtherType(entity);
-    }
-
-    protected DataAccessConfig createOtherType(DataAccessEntity entity) {
-        return null;
-    }
-}

+ 16 - 2
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

@@ -20,9 +20,11 @@ package org.hswebframework.web.service.oauth2.client.simple.provider;
 
 import com.alibaba.fastjson.JSON;
 import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.builder.AuthenticationBuilderFactory;
 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;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
@@ -34,12 +36,24 @@ import java.util.List;
  */
 @Component
 public class HswebResponseConvertSupport implements ResponseConvertForProviderDefinition {
+
+    private AuthenticationBuilderFactory authenticationBuilderFactory;
+
+    @Autowired(required = false)
+    public void setAuthenticationBuilderFactory(AuthenticationBuilderFactory authenticationBuilderFactory) {
+        this.authenticationBuilderFactory = authenticationBuilderFactory;
+    }
+
     @Override
     public <T> T convert(OAuth2Response response, Class<T> type) {
-        String json = response.asString();
 
+        String json = response.asString();
         if (type == Authentication.class) {
-            return (T) RemoteAuthentication.fromJson(json);
+            if (authenticationBuilderFactory != null) {
+                return (T) authenticationBuilderFactory.create().json(json).build();
+            } else {
+                throw new UnsupportedOperationException("authenticationBuilderFactory not ready");
+            }
         }
         return JSON.parseObject(json, type);
     }

+ 0 - 288
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/RemoteAuthentication.java

@@ -1,288 +0,0 @@
-/*
- *  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.service.oauth2.client.simple.provider;
-
-import com.alibaba.fastjson.JSONObject;
-import org.hswebframework.web.authorization.Authentication;
-import org.hswebframework.web.authorization.Permission;
-import org.hswebframework.web.authorization.Role;
-import org.hswebframework.web.authorization.User;
-import org.hswebframework.web.authorization.access.DataAccessConfig;
-import org.hswebframework.web.authorization.access.FieldAccessConfig;
-
-import java.io.Serializable;
-import java.util.*;
-import java.util.stream.Collectors;
-
-/**
- * @author zhouhao
- */
-public class RemoteAuthentication implements Authentication {
-    private ReadOnlyUser user;
-
-    private List<ReadOnlyRole> roles;
-
-    private List<ReadOnlyPermission> permissions;
-
-    private Map<String, Serializable> attributes = new HashMap<>();
-
-    public static RemoteAuthentication fromJson(String json) {
-        RemoteAuthentication authentication = new RemoteAuthentication();
-
-        JSONObject jsonObject = JSONObject.parseObject(json);
-
-        authentication.setUser(jsonObject.getObject("user", ReadOnlyUser.class));
-        authentication.setRoles(jsonObject.getJSONArray("roles").stream().map(role ->
-                ((JSONObject) role).toJavaObject(ReadOnlyRole.class)
-        ).collect(Collectors.toList()));
-        authentication.setPermissions(jsonObject.getJSONArray("permissions").parallelStream().map(permission ->
-                {
-                    JSONObject permissionObj= ((JSONObject) permission);
-
-                    return permissionObj.toJavaObject(ReadOnlyPermission.class);
-                }
-        ).collect(Collectors.toList()));
-        authentication.setAttributes((Map) jsonObject.getJSONObject("attributes"));
-        return authentication;
-    }
-
-    @Override
-    public ReadOnlyUser getUser() {
-        return user;
-    }
-
-    @Override
-    public List<Role> getRoles() {
-        return new ArrayList<>(roles);
-    }
-
-    @Override
-    public List<Permission> getPermissions() {
-        return new ArrayList<>(permissions);
-    }
-
-    @Override
-    @SuppressWarnings("unchecked")
-    public <T extends Serializable> Optional<T> getAttribute(String name) {
-        return Optional.of((T) attributes.get(name));
-    }
-
-    @Override
-    public void setAttribute(String name, Serializable value) {
-        attributes.put(name, value);
-    }
-
-    public void setUser(ReadOnlyUser user) {
-        checkWritable(this.user);
-        this.user = user;
-    }
-
-    public void setRoles(List<ReadOnlyRole> roles) {
-        checkWritable(this.roles);
-        this.roles = roles;
-    }
-
-    public void setPermissions(List<ReadOnlyPermission> permissions) {
-        checkWritable(this.permissions);
-        this.permissions = permissions;
-    }
-
-    public void setAttributes(Map<String, Serializable> attributes) {
-        this.attributes = attributes;
-    }
-
-    @Override
-    @SuppressWarnings("unchecked")
-    public <T extends Serializable> T removeAttributes(String name) {
-        return (T) attributes.remove(name);
-    }
-
-    public Map<String, Serializable> getAttributes() {
-        return attributes;
-    }
-
-    public static class ReadOnlyPermission implements Permission {
-        private String                 id;
-        private Set<String>            actions;
-        private Set<SimpleFieldAccess> fieldAccesses;
-        private Set<DataAccessConfig>  dataAccessConfigs;
-
-        public ReadOnlyPermission() {
-        }
-
-        public ReadOnlyPermission(String id, Collection<String> actions) {
-            this.id = id;
-            this.actions = new HashSet<>(actions);
-        }
-
-        @Override
-        public String getId() {
-            return id;
-        }
-
-        public void setId(String id) {
-            checkWritable(this.id);
-            this.id = id;
-        }
-
-        @Override
-        public Set<String> getActions() {
-            if (actions == null) actions = Collections.emptySet();
-            return new HashSet<>(actions);
-        }
-
-        @Override
-        public Set<FieldAccessConfig> getFieldAccesses() {
-            if (fieldAccesses == null) fieldAccesses = Collections.emptySet();
-            return new HashSet<>(fieldAccesses);
-        }
-
-        public Set<DataAccessConfig> getDataAccessConfigs() {
-            if (dataAccessConfigs == null) dataAccessConfigs = Collections.emptySet();
-            return new HashSet<>(dataAccessConfigs);
-        }
-
-        public void setFieldAccesses(Set<SimpleFieldAccess> fieldAccesses) {
-            checkWritable(this.fieldAccesses);
-            this.fieldAccesses = fieldAccesses;
-        }
-
-        public void setDataAccessConfigs(Set<DataAccessConfig> dataAccessConfigs) {
-            checkWritable(this.dataAccessConfigs);
-            this.dataAccessConfigs = dataAccessConfigs;
-        }
-
-        public void setActions(Set<String> actions) {
-            checkWritable(this.actions);
-            this.actions = new HashSet<>(actions);
-        }
-    }
-
-    public static class SimpleFieldAccess implements FieldAccessConfig {
-        private String      field;
-        private Set<String> actions;
-
-        @Override
-        public String getField() {
-            return field;
-        }
-
-        @Override
-        public Set<String> getActions() {
-            return new HashSet<>(actions);
-        }
-
-        public void setField(String field) {
-            checkWritable(this.field);
-            this.field = field;
-        }
-
-        public void setActions(Set<String> actions) {
-            checkWritable(this.actions);
-            this.actions = actions;
-        }
-    }
-
-    public static class ReadOnlyRole implements Role {
-        private String id;
-
-        private String name;
-
-        public ReadOnlyRole() {
-        }
-
-        public ReadOnlyRole(String id, String name) {
-            this.id = id;
-            this.name = name;
-        }
-
-        @Override
-        public String getId() {
-            return id;
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        public void setId(String id) {
-            checkWritable(this.id);
-            this.id = id;
-        }
-
-        public void setName(String name) {
-            checkWritable(this.name);
-            this.name = name;
-        }
-    }
-
-    public static class ReadOnlyUser implements User {
-        private String id;
-
-        private String username;
-
-        private String name;
-
-        public ReadOnlyUser() {
-        }
-
-        public ReadOnlyUser(String id, String username, String name) {
-            this.id = id;
-            this.username = username;
-            this.name = name;
-        }
-
-        @Override
-        public String getId() {
-            return id;
-        }
-
-        @Override
-        public String getUsername() {
-            return username;
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        public void setId(String id) {
-            checkWritable(this.id);
-            this.id = id;
-        }
-
-        public void setUsername(String username) {
-            checkWritable(this.username);
-            this.username = username;
-        }
-
-        public void setName(String name) {
-            checkWritable(this.name);
-            this.name = name;
-        }
-    }
-
-    static final void checkWritable(Object obj) {
-        if (obj != null) {
-            throw new UnsupportedOperationException();
-        }
-    }
-}

+ 42 - 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/RemoteAuthenticationBuilder.java

@@ -0,0 +1,42 @@
+/*
+ *  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.service.oauth2.client.simple.provider;
+
+import com.alibaba.fastjson.JSONObject;
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.Permission;
+import org.hswebframework.web.authorization.Role;
+import org.hswebframework.web.authorization.User;
+import org.hswebframework.web.authorization.access.DataAccessConfig;
+import org.hswebframework.web.authorization.access.FieldAccessConfig;
+import org.hswebframework.web.authorization.simple.SimpleAuthentication;
+
+import java.io.Serializable;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @author zhouhao
+ */
+public class RemoteAuthenticationBuilder {
+
+    public static Authentication fromJson(String json) {
+        return JSONObject.parseObject(json, SimpleAuthentication.class);
+    }
+}