From 00d626d394219e94b44744e2f0c13ed81a83a06e Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 16 Feb 2020 15:52:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../component/UserAuthoritiesGenerator.java | 19 +++++++++++++++ ...onalPermissionContainersListGenerator.java | 2 ++ ...FunctionalScopeRelationsListGenerator.java | 2 ++ ...ionContainersCollectionsListGenerator.java | 2 ++ .../ase/component/UserRolesListGenerator.java | 3 ++- .../ase/component/UserTagsListGenerator.java | 1 + .../ase/configure/ComponentsConfigure.java | 23 ------------------- .../codesdream/ase/model/permission/User.java | 16 ++++++------- .../ase/service/ASEUserDetailsService.java | 7 +++++- 9 files changed, 41 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/codesdream/ase/component/UserAuthoritiesGenerator.java diff --git a/src/main/java/com/codesdream/ase/component/UserAuthoritiesGenerator.java b/src/main/java/com/codesdream/ase/component/UserAuthoritiesGenerator.java new file mode 100644 index 0000000..52abd66 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/UserAuthoritiesGenerator.java @@ -0,0 +1,19 @@ +package com.codesdream.ase.component; + +import com.codesdream.ase.model.permission.User; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Collection; + +@Component +public class UserAuthoritiesGenerator { + @Resource + UserRolesListGenerator userRolesListGenerator; + + public Collection grantedAuthorities(User user){ + return userRolesListGenerator.generateRoles(user); + } + +} diff --git a/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java b/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java index cc6ddfd..535af65 100644 --- a/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserFunctionalPermissionContainersListGenerator.java @@ -1,6 +1,7 @@ package com.codesdream.ase.component; import com.codesdream.ase.model.permission.*; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Collection; @@ -8,6 +9,7 @@ import java.util.Collection; /** * 生成功能性权限容器列表 */ +@Component public class UserFunctionalPermissionContainersListGenerator { private UserFunctionalScopeRelationsListGenerator functionalScopeRelationsListGenerator; diff --git a/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java b/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java index 027ecd7..2b213a0 100644 --- a/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserFunctionalScopeRelationsListGenerator.java @@ -3,6 +3,7 @@ package com.codesdream.ase.component; import com.codesdream.ase.model.permission.FunctionalScopeRelation; import com.codesdream.ase.model.permission.PermissionContainersCollection; import com.codesdream.ase.model.permission.User; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Collection; @@ -10,6 +11,7 @@ import java.util.Collection; /** * 生成功能性权限容器与范围性权限容器关联对列表 */ +@Component public class UserFunctionalScopeRelationsListGenerator { private UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator; diff --git a/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java b/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java index e4a87bd..88f161e 100644 --- a/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserPermissionContainersCollectionsListGenerator.java @@ -3,6 +3,7 @@ package com.codesdream.ase.component; import com.codesdream.ase.model.permission.PermissionContainersCollection; import com.codesdream.ase.model.permission.Tag; import com.codesdream.ase.model.permission.User; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Collection; @@ -10,6 +11,7 @@ import java.util.Collection; /** * 生成权限容器集合列表 */ +@Component public class UserPermissionContainersCollectionsListGenerator { private UserTagsListGenerator userTagsListGenerator; diff --git a/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java b/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java index d05c35c..c4317c7 100644 --- a/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserRolesListGenerator.java @@ -4,6 +4,7 @@ import com.codesdream.ase.model.permission.FunctionalPermissionContainer; import com.codesdream.ase.model.permission.User; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Collection; @@ -11,7 +12,7 @@ import java.util.Collection; /** * 生成用户访问权限角色列表 */ - +@Component public class UserRolesListGenerator { private UserFunctionalPermissionContainersListGenerator functionalPermissionContainersListGenerator; diff --git a/src/main/java/com/codesdream/ase/component/UserTagsListGenerator.java b/src/main/java/com/codesdream/ase/component/UserTagsListGenerator.java index cb0f2a8..b3fca13 100644 --- a/src/main/java/com/codesdream/ase/component/UserTagsListGenerator.java +++ b/src/main/java/com/codesdream/ase/component/UserTagsListGenerator.java @@ -14,6 +14,7 @@ import java.util.Optional; /** * 生成用户标签列表 */ +@Component public class UserTagsListGenerator { @Resource UserRepository userRepository; diff --git a/src/main/java/com/codesdream/ase/configure/ComponentsConfigure.java b/src/main/java/com/codesdream/ase/configure/ComponentsConfigure.java index c0e7aaa..85708af 100644 --- a/src/main/java/com/codesdream/ase/configure/ComponentsConfigure.java +++ b/src/main/java/com/codesdream/ase/configure/ComponentsConfigure.java @@ -6,29 +6,6 @@ import org.springframework.context.annotation.Configuration; @Configuration public class ComponentsConfigure { - @Bean - UserTagsListGenerator userTagsListGenerator(){ - return new UserTagsListGenerator(); - } - @Bean - UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator(){ - return new UserPermissionContainersCollectionsListGenerator(userTagsListGenerator()); - } - - @Bean - UserFunctionalScopeRelationsListGenerator userFunctionalScopeRelationsListGenerator(){ - return new UserFunctionalScopeRelationsListGenerator(userPermissionContainersCollectionsListGenerator()); - } - - @Bean - UserFunctionalPermissionContainersListGenerator userFunctionalPermissionContainersListGenerator(){ - return new UserFunctionalPermissionContainersListGenerator(userFunctionalScopeRelationsListGenerator()); - } - - @Bean - UserRolesListGenerator userRolesListGenerator(){ - return new UserRolesListGenerator(userFunctionalPermissionContainersListGenerator()); - } } diff --git a/src/main/java/com/codesdream/ase/model/permission/User.java b/src/main/java/com/codesdream/ase/model/permission/User.java index 680b7ed..87b739c 100644 --- a/src/main/java/com/codesdream/ase/model/permission/User.java +++ b/src/main/java/com/codesdream/ase/model/permission/User.java @@ -1,8 +1,9 @@ package com.codesdream.ase.model.permission; +import com.codesdream.ase.component.UserAuthoritiesGenerator; import com.codesdream.ase.component.UserRolesListGenerator; import com.codesdream.ase.model.activity.UserActivity; -import com.codesdream.ase.service.UserService; +import com.codesdream.ase.service.IUserService; import lombok.Data; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; @@ -10,6 +11,7 @@ import org.springframework.security.core.userdetails.UserDetails; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.persistence.*; +import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -19,9 +21,6 @@ import java.util.Set; @Table(name = "user") public class User implements UserDetails { - @Resource - private transient UserService userService; - @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @@ -49,6 +48,9 @@ public class User implements UserDetails { @Column(nullable = false) private boolean deleted; + // 访问控制角色(不在数据表中) Spring Security + private transient Collection authorities; + // 用户关联标签 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Set tags; @@ -90,11 +92,7 @@ public class User implements UserDetails { this.tags = new HashSet<>(); this.userDetail = new UserDetail(); this.userAuth = new UserAuth(); - } - - @Override - public Collection getAuthorities() { - return userService.getUserAuthorities(this); + this.authorities = new ArrayList<>(); } } diff --git a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java index 8c0d9b8..aea8bc3 100644 --- a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java +++ b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java @@ -1,5 +1,6 @@ package com.codesdream.ase.service; +import com.codesdream.ase.component.UserAuthoritiesGenerator; import com.codesdream.ase.model.permission.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; @@ -14,7 +15,10 @@ import java.util.Optional; public class ASEUserDetailsService implements UserDetailsService { @Resource - UserService userService; + IUserService userService; + + @Resource + UserAuthoritiesGenerator userAuthoritiesGenerator; @Override @Transactional @@ -27,6 +31,7 @@ public class ASEUserDetailsService implements UserDetailsService { else { System.out.println("Returning user information"); System.out.println("User Password: "+user.get().getPassword()); + user.get().setAuthorities(userAuthoritiesGenerator.grantedAuthorities(user.get())); return user.get(); } }