修复登录问题

This commit is contained in:
Saturneric 2020-02-16 15:52:22 +08:00
parent ee9d71413a
commit 00d626d394
9 changed files with 41 additions and 34 deletions

View File

@ -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<? extends GrantedAuthority> grantedAuthorities(User user){
return userRolesListGenerator.generateRoles(user);
}
}

View File

@ -1,6 +1,7 @@
package com.codesdream.ase.component; package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.*; import com.codesdream.ase.model.permission.*;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -8,6 +9,7 @@ import java.util.Collection;
/** /**
* 生成功能性权限容器列表 * 生成功能性权限容器列表
*/ */
@Component
public class UserFunctionalPermissionContainersListGenerator { public class UserFunctionalPermissionContainersListGenerator {
private UserFunctionalScopeRelationsListGenerator functionalScopeRelationsListGenerator; private UserFunctionalScopeRelationsListGenerator functionalScopeRelationsListGenerator;

View File

@ -3,6 +3,7 @@ package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.FunctionalScopeRelation; import com.codesdream.ase.model.permission.FunctionalScopeRelation;
import com.codesdream.ase.model.permission.PermissionContainersCollection; import com.codesdream.ase.model.permission.PermissionContainersCollection;
import com.codesdream.ase.model.permission.User; import com.codesdream.ase.model.permission.User;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -10,6 +11,7 @@ import java.util.Collection;
/** /**
* 生成功能性权限容器与范围性权限容器关联对列表 * 生成功能性权限容器与范围性权限容器关联对列表
*/ */
@Component
public class UserFunctionalScopeRelationsListGenerator { public class UserFunctionalScopeRelationsListGenerator {
private UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator; private UserPermissionContainersCollectionsListGenerator userPermissionContainersCollectionsListGenerator;

View File

@ -3,6 +3,7 @@ package com.codesdream.ase.component;
import com.codesdream.ase.model.permission.PermissionContainersCollection; import com.codesdream.ase.model.permission.PermissionContainersCollection;
import com.codesdream.ase.model.permission.Tag; import com.codesdream.ase.model.permission.Tag;
import com.codesdream.ase.model.permission.User; import com.codesdream.ase.model.permission.User;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -10,6 +11,7 @@ import java.util.Collection;
/** /**
* 生成权限容器集合列表 * 生成权限容器集合列表
*/ */
@Component
public class UserPermissionContainersCollectionsListGenerator { public class UserPermissionContainersCollectionsListGenerator {
private UserTagsListGenerator userTagsListGenerator; private UserTagsListGenerator userTagsListGenerator;

View File

@ -4,6 +4,7 @@ import com.codesdream.ase.model.permission.FunctionalPermissionContainer;
import com.codesdream.ase.model.permission.User; import com.codesdream.ase.model.permission.User;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -11,7 +12,7 @@ import java.util.Collection;
/** /**
* 生成用户访问权限角色列表 * 生成用户访问权限角色列表
*/ */
@Component
public class UserRolesListGenerator { public class UserRolesListGenerator {
private UserFunctionalPermissionContainersListGenerator functionalPermissionContainersListGenerator; private UserFunctionalPermissionContainersListGenerator functionalPermissionContainersListGenerator;

View File

@ -14,6 +14,7 @@ import java.util.Optional;
/** /**
* 生成用户标签列表 * 生成用户标签列表
*/ */
@Component
public class UserTagsListGenerator { public class UserTagsListGenerator {
@Resource @Resource
UserRepository userRepository; UserRepository userRepository;

View File

@ -6,29 +6,6 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class ComponentsConfigure { 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());
}
} }

View File

@ -1,8 +1,9 @@
package com.codesdream.ase.model.permission; package com.codesdream.ase.model.permission;
import com.codesdream.ase.component.UserAuthoritiesGenerator;
import com.codesdream.ase.component.UserRolesListGenerator; import com.codesdream.ase.component.UserRolesListGenerator;
import com.codesdream.ase.model.activity.UserActivity; import com.codesdream.ase.model.activity.UserActivity;
import com.codesdream.ase.service.UserService; import com.codesdream.ase.service.IUserService;
import lombok.Data; import lombok.Data;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; 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.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -19,9 +21,6 @@ import java.util.Set;
@Table(name = "user") @Table(name = "user")
public class User implements UserDetails { public class User implements UserDetails {
@Resource
private transient UserService userService;
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private int id; private int id;
@ -49,6 +48,9 @@ public class User implements UserDetails {
@Column(nullable = false) @Column(nullable = false)
private boolean deleted; private boolean deleted;
// 访问控制角色(不在数据表中) Spring Security
private transient Collection<?extends GrantedAuthority> authorities;
// 用户关联标签 // 用户关联标签
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Tag> tags; private Set<Tag> tags;
@ -90,11 +92,7 @@ public class User implements UserDetails {
this.tags = new HashSet<>(); this.tags = new HashSet<>();
this.userDetail = new UserDetail(); this.userDetail = new UserDetail();
this.userAuth = new UserAuth(); this.userAuth = new UserAuth();
} this.authorities = new ArrayList<>();
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return userService.getUserAuthorities(this);
} }
} }

View File

@ -1,5 +1,6 @@
package com.codesdream.ase.service; package com.codesdream.ase.service;
import com.codesdream.ase.component.UserAuthoritiesGenerator;
import com.codesdream.ase.model.permission.User; import com.codesdream.ase.model.permission.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
@ -14,7 +15,10 @@ import java.util.Optional;
public class ASEUserDetailsService implements UserDetailsService { public class ASEUserDetailsService implements UserDetailsService {
@Resource @Resource
UserService userService; IUserService userService;
@Resource
UserAuthoritiesGenerator userAuthoritiesGenerator;
@Override @Override
@Transactional @Transactional
@ -27,6 +31,7 @@ public class ASEUserDetailsService implements UserDetailsService {
else { else {
System.out.println("Returning user information"); System.out.println("Returning user information");
System.out.println("User Password: "+user.get().getPassword()); System.out.println("User Password: "+user.get().getPassword());
user.get().setAuthorities(userAuthoritiesGenerator.grantedAuthorities(user.get()));
return user.get(); return user.get();
} }
} }