重构用户角色类

This commit is contained in:
liuchengqian 2023-02-06 09:51:50 +08:00
parent acd127babd
commit 85e6dbf06f
9 changed files with 53 additions and 61 deletions

View File

@ -88,8 +88,8 @@ public class CustomAuthenticationProvider implements AuthenticationProvider {
if (encryptPassword.equals(userEntity.getPassword())) {
// 设置权限列表
ArrayList<GrantedAuthority> permissions = new ArrayList<>();
List<Integer> integers = customAuthenticationProvider.sysAuthorityService.selectAuthorityByUserId(userEntity.getId());
List<SysAuthorityEntity> permissionList = customAuthenticationProvider.sysAuthorityService.findAllByIdIn(integers);
List<Long> longs = customAuthenticationProvider.sysAuthorityService.selectAuthorityByUserId(userEntity.getId());
List<SysAuthorityEntity> permissionList = customAuthenticationProvider.sysAuthorityService.findAllByIdIn(longs);
for (SysAuthorityEntity sysAuthorityEntity : permissionList) {
permissions.add(new GrantedAuthorityImpl(sysAuthorityEntity.getAuthorityName()));
}

View File

@ -19,7 +19,7 @@ public interface SysAuthorityService {
*/
List<SysAuthorityEntity> getSysAuthorityListByUserName(String userName);
List<SysAuthorityEntity> findAllByIdIn(List<Integer> id);
List<SysAuthorityEntity> findAllByIdIn(List<Long> id);
List<Integer> selectAuthorityByUserId(Long userId);
List<Long> selectAuthorityByUserId(Long userId);
}

View File

@ -29,12 +29,12 @@ public class SysAuthorityServiceImpl implements SysAuthorityService {
}
@Override
public List<SysAuthorityEntity> findAllByIdIn(List<Integer> id) {
public List<SysAuthorityEntity> findAllByIdIn(List<Long> id) {
return sysAuthorityDao.findAllByIdIn(id);
}
@Override
public List<Integer> selectAuthorityByUserId(Long userId) {
public List<Long> selectAuthorityByUserId(Long userId) {
return relRoleAuthorityDao.selectAuthorityByUserId(userId);
}
}

View File

@ -8,21 +8,12 @@ import org.springframework.data.repository.query.Param;
import java.util.List;
/**
* @author XinYi Song
*/
public interface RelRoleAuthorityDao extends JpaRepository<RelRoleAuthorityEntity, Long>, JpaSpecificationExecutor<RelRoleAuthorityEntity> {
/**
* 根据userId查询出权限的id
*
* @param userId
* @return
*/
@Query(value = "select authority_id from rel_role_authority where user_id = ?", nativeQuery = true)
List<Integer> selectAuthorityByUserId(@Param("userId") Long userId);
@Query(value = "SELECT * FROM rel_role_authority WHERE authority_id = ?1", nativeQuery = true)
List<RelRoleAuthorityEntity> selectByAuthorityId(Integer authorityId);
List<Long> selectAuthorityByUserId(@Param("userId") Long userId);
}

View File

@ -4,9 +4,6 @@ import com.xkrs.straw.model.entity.RelUserRoleEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author XinYi Song
*/
public interface RelUserRoleDao extends JpaRepository<RelUserRoleEntity, Long>, JpaSpecificationExecutor<RelUserRoleEntity> {
}

View File

@ -8,22 +8,16 @@ import org.springframework.data.repository.query.Param;
import java.util.List;
public interface SysAuthorityDao extends JpaRepository<SysAuthorityEntity, Integer>, JpaSpecificationExecutor<SysAuthorityEntity> {
public interface SysAuthorityDao extends JpaRepository<SysAuthorityEntity, Long>, JpaSpecificationExecutor<SysAuthorityEntity> {
/**
* 查询权限实体列表根据用户名 Object[]
*
* @param userName 用户名
* @return 用户实体
*/
@Query(value = "SELECT a.id, a.authority_name, a.authority_name_zh, a.authority_desc FROM sys_authority a,rel_role_authority ra,sys_role r WHERE r.id = ra.role_id AND ra.authority_id = a.id AND r.id IN (SELECT r.id FROM sys_user u,sys_role r,rel_user_role ur WHERE u.user_name = :userName AND u.id = ur.user_id AND ur.role_id = r.id)", nativeQuery = true)
List<SysAuthorityEntity> selectByUserName(@Param("userName") String userName);
/**
* 根据id批量查询权限信息
*
* @param id
* @return
*/
List<SysAuthorityEntity> findAllByIdIn(List<Integer> id);
List<SysAuthorityEntity> findAllByIdIn(List<Long> id);
}

View File

@ -18,40 +18,40 @@ public class RelRoleAuthorityEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "rel_role_authority_seq_gen")
@SequenceGenerator(name = "rel_role_authority_seq_gen", sequenceName = "rel_role_authority_id_seq", allocationSize = 1)
private Integer id;
private Long id;
@Column(nullable = false)
private Integer roleId;
private Long roleId;
@Column(nullable = false)
private Integer authorityId;
private Long authorityId;
private Long userId;
public RelRoleAuthorityEntity() {
}
public Integer getId() {
public Long getId() {
return id;
}
public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}
public Integer getRoleId() {
public Long getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
public void setRoleId(Long roleId) {
this.roleId = roleId;
}
public Integer getAuthorityId() {
public Long getAuthorityId() {
return authorityId;
}
public void setAuthorityId(Integer authorityId) {
public void setAuthorityId(Long authorityId) {
this.authorityId = authorityId;
}
@ -65,6 +65,11 @@ public class RelRoleAuthorityEntity implements Serializable {
@Override
public String toString() {
return "RelRoleAuthorityEntity{" + "id=" + id + ", roleId=" + roleId + ", authorityId=" + authorityId + ", userId=" + userId + '}';
return "RelRoleAuthorityEntity{" +
"id=" + id +
", roleId=" + roleId +
", authorityId=" + authorityId +
", userId=" + userId +
'}';
}
}

View File

@ -3,11 +3,6 @@ package com.xkrs.straw.model.entity;
import javax.persistence.*;
import java.io.Serializable;
/**
* RelUserRole 表实体类
*
* @author tajochen
*/
@Entity
@Table(name = "rel_user_role")
public class RelUserRoleEntity implements Serializable {
@ -18,19 +13,22 @@ public class RelUserRoleEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "rel_user_role_seq_gen")
@SequenceGenerator(name = "rel_user_role_seq_gen", sequenceName = "rel_user_role_id_seq", allocationSize = 1)
private Integer id;
private Long id;
@Column(nullable = false)
private Long userId;
@Column(nullable = false)
private Integer roleId;
private Long roleId;
public Integer getId() {
public RelUserRoleEntity() {
}
public Long getId() {
return id;
}
public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}
@ -42,16 +40,20 @@ public class RelUserRoleEntity implements Serializable {
this.userId = userId;
}
public Integer getRoleId() {
public Long getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
public void setRoleId(Long roleId) {
this.roleId = roleId;
}
@Override
public String toString() {
return "RelUserRoleEntity{" + "id=" + id + ", userId=" + userId + ", roleId=" + roleId + '}';
return "RelUserRoleEntity{" +
"id=" + id +
", userId=" + userId +
", roleId=" + roleId +
'}';
}
}

View File

@ -3,11 +3,6 @@ package com.xkrs.straw.model.entity;
import javax.persistence.*;
import java.io.Serializable;
/**
* SysAuthority 表实体类
*
* @author tajochen
*/
@Entity
@Table(name = "sys_authority")
public class SysAuthorityEntity implements Serializable {
@ -18,7 +13,7 @@ public class SysAuthorityEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sys_authority_seq_gen")
@SequenceGenerator(name = "sys_authority_seq_gen", sequenceName = "sys_authority_id_seq", allocationSize = 1)
private Integer id;
private Long id;
@Column(length = 64, nullable = false, unique = true, columnDefinition = "varchar(64)")
private String authorityName;
@ -29,11 +24,14 @@ public class SysAuthorityEntity implements Serializable {
@Column(length = 128, columnDefinition = "varchar(128)")
private String authorityDesc;
public Integer getId() {
public SysAuthorityEntity() {
}
public Long getId() {
return id;
}
public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}
@ -42,7 +40,7 @@ public class SysAuthorityEntity implements Serializable {
}
public void setAuthorityName(String authorityName) {
this.authorityName = authorityName == null ? null : authorityName.trim();
this.authorityName = authorityName;
}
public String getAuthorityNameZh() {
@ -50,7 +48,7 @@ public class SysAuthorityEntity implements Serializable {
}
public void setAuthorityNameZh(String authorityNameZh) {
this.authorityNameZh = authorityNameZh == null ? null : authorityNameZh.trim();
this.authorityNameZh = authorityNameZh;
}
public String getAuthorityDesc() {
@ -58,11 +56,16 @@ public class SysAuthorityEntity implements Serializable {
}
public void setAuthorityDesc(String authorityDesc) {
this.authorityDesc = authorityDesc == null ? null : authorityDesc.trim();
this.authorityDesc = authorityDesc;
}
@Override
public String toString() {
return "SysAuthorityEntity{" + "id=" + id + ", authorityName='" + authorityName + '\'' + ", authorityDesc='" + authorityDesc + '\'' + ", authorityNameZh='" + authorityNameZh + '\'' + '}';
return "SysAuthorityEntity{" +
"id=" + id +
", authorityName='" + authorityName + '\'' +
", authorityNameZh='" + authorityNameZh + '\'' +
", authorityDesc='" + authorityDesc + '\'' +
'}';
}
}