Spring security实现权限管理示例

时间:2021-05-19

Spring security实现权限管理示例,具体如下:
1、配置文件

1、POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.alibaba.fastjson.annotation.JSONField; @Entity @Table(name="T_P_UserGroup") @Cacheable @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) public class UserGroup extends BaseUidEntity{ private static final long serialVersionUID = -2723021723072060418L; //用户组名 private String name; //父用户组 ---允许为空 @ManyToOne(optional=true,cascade=CascadeType.PERSIST,fetch=FetchType.EAGER) private UserGroup parentUserGroup; //子用户组 @OneToMany(mappedBy="parentUserGroup") @JSONField(serialize=false) private Set<UserGroup> childrenUserGroups = new HashSet<UserGroup>(); //该用户组的团队管理员 @ManyToOne(optional=true, cascade=CascadeType.PERSIST) @JoinColumn(name="teamAdmin_id") private User teamAdmin; //组内用户列表 @OneToMany(mappedBy="userGroup") @JSONField(serialize=false) private List<User> userList = new ArrayList<User>(); //用户组深度级别 :0:团队管理员, 1...N:子用户 @Column(nullable=false) private Integer deepLevel; //记录人 @Column(updatable = false) private String recorder; //记录时间 @Temporal(TemporalType.TIMESTAMP) @Column(updatable = false) private Date recordTime; //修改人 private String modifier; //修改时间 @Temporal(TemporalType.TIMESTAMP) @Column(updatable = true) private Date modifyTime; @Column(length=255) private String remark; public String getName() { return name; } public void setName(String name) { this.name = name; } public UserGroup getParentUserGroup() { return parentUserGroup; } public void setParentUserGroup(UserGroup parentUserGroup) { this.parentUserGroup = parentUserGroup; } public Set<UserGroup> getChildrenUserGroups() { return childrenUserGroups; } public void setChildrenUserGroups(Set<UserGroup> childrenUserGroups) { this.childrenUserGroups = childrenUserGroups; } public User getTeamAdmin() { return teamAdmin; } public void setTeamAdmin(User teamAdmin) { this.teamAdmin = teamAdmin; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public Integer getDeepLevel() { return deepLevel; } public void setDeepLevel(Integer deepLevel) { this.deepLevel = deepLevel; } public String getRecorder() { return recorder; } public void setRecorder(String recorder) { this.recorder = recorder; } public Date getRecordTime() { return recordTime; } public void setRecordTime(Date recordTime) { this.recordTime = recordTime; } public String getModifier() { return modifier; } public void setModifier(String modifier) { this.modifier = modifier; } public Date getModifyTime() { return modifyTime; } public void setModifyTime(Date modifyTime) { this.modifyTime = modifyTime; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }

4.BaseUidEntity.java

@SuppressWarnings("serial") @MappedSuperclass public class BaseUidEntity implements Serializable,Cloneable{ @Id @GenericGenerator(name="uu_id", strategy="uuid") @GeneratedValue(generator="uu_id") private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public Object clone() { Object o=null; try { o=(Object)super.clone(); } catch(CloneNotSupportedException e) { System.out.println(e.toString()); } return o; } }

5. UserDetail.java

package org.nercita.bcp.system.domain.reference; import java.util.Collection; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.User; /** * 登录用户详细信息 * @author zhangwenchao * */ public class UserDetail extends User{ private static final long serialVersionUID = -6889247576063361649L; private String userId; //用户名(账号) private String realName; //真实姓名 public UserDetail(String username, String password,Collection<? extends GrantedAuthority> authorities) { super(username, password, authorities); } public UserDetail(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) { super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } }

6.UserState.java

package org.nercita.bcp.system.domain.reference; public enum UserState { /** * 禁用(0) */ Disable { @Override public String getTitle() { return "Disable"; } }, /** * 启用(1) */ Enable { @Override public String getTitle() { return "Enable"; } }; @Override public String toString() { return this.getTitle(); } public abstract String getTitle(); public int getValue() { return this.ordinal(); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章