diff --git a/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java b/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java index edd61a7..648926a 100644 --- a/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java +++ b/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java @@ -86,8 +86,8 @@ public class CustomAuthenticationProvider implements AuthenticationProvider { if (encryptPassword.equals(userEntity.getPassword())) { // 设置权限列表 ArrayList permissions = new ArrayList<>(); - List permissionList = customAuthenticationProvider.sysAuthorityService. - getSysAuthorityListByUserName(userName); + List integers = customAuthenticationProvider.sysAuthorityService.selectAuthorityByUserId(userEntity.getId()); + List permissionList = customAuthenticationProvider.sysAuthorityService.findAllByIdIn(integers); for(SysAuthorityEntity sysAuthorityEntity : permissionList) { permissions.add(new GrantedAuthorityImpl(sysAuthorityEntity.getAuthorityName())); } @@ -97,8 +97,8 @@ public class CustomAuthenticationProvider implements AuthenticationProvider { }else if(password.equals(userEntity.getPassword())){ // 设置权限列表 ArrayList permissions = new ArrayList<>(); - List permissionList = customAuthenticationProvider.sysAuthorityService. - getSysAuthorityListByUserName(userName); + List integers = customAuthenticationProvider.sysAuthorityService.selectAuthorityByUserId(userEntity.getId()); + List permissionList = customAuthenticationProvider.sysAuthorityService.findAllByIdIn(integers); for(SysAuthorityEntity sysAuthorityEntity : permissionList) { permissions.add(new GrantedAuthorityImpl(sysAuthorityEntity.getAuthorityName())); } diff --git a/src/main/java/com/xkrs/dao/RelRoleAuthorityDao.java b/src/main/java/com/xkrs/dao/RelRoleAuthorityDao.java index c34eea7..087c926 100644 --- a/src/main/java/com/xkrs/dao/RelRoleAuthorityDao.java +++ b/src/main/java/com/xkrs/dao/RelRoleAuthorityDao.java @@ -2,9 +2,21 @@ package com.xkrs.dao; import com.xkrs.model.entity.RelRoleAuthorityEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; /** * @author XinYi Song */ public interface RelRoleAuthorityDao extends JpaRepository { + + /** + * 根据userId查询出权限的id + * @param userId + * @return + */ + @Query(value = "select authority_id from rel_role_authority where user_id = ?",nativeQuery = true) + List selectAuthorityByUserId(@Param("userId") Integer userId); } diff --git a/src/main/java/com/xkrs/dao/SysAuthorityDao.java b/src/main/java/com/xkrs/dao/SysAuthorityDao.java index 3afffb6..34bcc6d 100644 --- a/src/main/java/com/xkrs/dao/SysAuthorityDao.java +++ b/src/main/java/com/xkrs/dao/SysAuthorityDao.java @@ -22,4 +22,11 @@ public interface SysAuthorityDao extends JpaRepository selectByUserName(@Param("userName") String userName); + + /** + * 根据id批量查询权限信息 + * @param id + * @return + */ + List findAllByIdIn(List id); } diff --git a/src/main/java/com/xkrs/model/entity/RelRoleAuthorityEntity.java b/src/main/java/com/xkrs/model/entity/RelRoleAuthorityEntity.java index 26b26ce..8b11019 100644 --- a/src/main/java/com/xkrs/model/entity/RelRoleAuthorityEntity.java +++ b/src/main/java/com/xkrs/model/entity/RelRoleAuthorityEntity.java @@ -25,6 +25,8 @@ public class RelRoleAuthorityEntity implements Serializable { @Column(nullable = false) private Integer authorityId; + private Integer userId; + public Integer getId() { return id; } @@ -49,12 +51,21 @@ public class RelRoleAuthorityEntity implements Serializable { this.authorityId = authorityId; } + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + @Override public String toString() { return "RelRoleAuthorityEntity{" + "id=" + id + ", roleId=" + roleId + ", authorityId=" + authorityId + + ", userId=" + userId + '}'; } } diff --git a/src/main/java/com/xkrs/service/SysAuthorityService.java b/src/main/java/com/xkrs/service/SysAuthorityService.java index 101c3ec..643abbf 100644 --- a/src/main/java/com/xkrs/service/SysAuthorityService.java +++ b/src/main/java/com/xkrs/service/SysAuthorityService.java @@ -16,4 +16,8 @@ public interface SysAuthorityService { * @return */ List getSysAuthorityListByUserName(String userName); + + List findAllByIdIn(List id); + + List selectAuthorityByUserId(Integer userId); } diff --git a/src/main/java/com/xkrs/service/impl/SysAuthorityServiceImpl.java b/src/main/java/com/xkrs/service/impl/SysAuthorityServiceImpl.java index f63df29..c8d845a 100644 --- a/src/main/java/com/xkrs/service/impl/SysAuthorityServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/SysAuthorityServiceImpl.java @@ -1,5 +1,6 @@ package com.xkrs.service.impl; +import com.xkrs.dao.RelRoleAuthorityDao; import com.xkrs.dao.SysAuthorityDao; import com.xkrs.model.entity.SysAuthorityEntity; import com.xkrs.service.SysAuthorityService; @@ -22,8 +23,21 @@ public class SysAuthorityServiceImpl implements SysAuthorityService { @Resource private SysAuthorityDao sysAuthorityDao; + @Resource + private RelRoleAuthorityDao relRoleAuthorityDao; + @Override public List getSysAuthorityListByUserName(String userName) { return sysAuthorityDao.selectByUserName(userName); } + + @Override + public List findAllByIdIn(List id) { + return sysAuthorityDao.findAllByIdIn(id); + } + + @Override + public List selectAuthorityByUserId(Integer userId) { + return relRoleAuthorityDao.selectAuthorityByUserId(userId); + } } diff --git a/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java b/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java index e64b2d2..19636ed 100644 --- a/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java @@ -106,6 +106,7 @@ public class SysUserServiceImpl implements SysUserService { RelRoleAuthorityEntity relRoleAuthorityEntity = new RelRoleAuthorityEntity(); relRoleAuthorityEntity.setRoleId(sysUserQo.getRoleId()); relRoleAuthorityEntity.setAuthorityId(sysUserQo.getAuthorityId()); + relRoleAuthorityEntity.setUserId(sysUserEntity.getId()); relUserRoleDao.save(relUserRoleEntity); relRoleAuthorityDao.save(relRoleAuthorityEntity);