新增当前用户角色列表

This commit is contained in:
wenc000 2020-01-13 12:54:51 +08:00
parent 3181b8b632
commit 53ca1699fa
3 changed files with 36 additions and 15 deletions

View File

@ -45,12 +45,13 @@ public class ClientUserAuthConverter implements UserAuthenticationConverter {
Object principal = map.get("user_name");
if (!Objects.isNull(principal)) {
Collection<GrantedAuthority> authorities;
List<RoleBO> roles = new ArrayList<>();
if ("admin".equals(principal.toString())) {
authorities = new LinkedHashSet<>();
authorities.add(new RoleGrantedAuthority("ROLE_ALL"));
authorities.add(new RoleGrantedAuthority("ROLE_GROUP_ALL"));
} else {
authorities = getAuthorities(map);
authorities = getAuthorities(roles, map);
}
// 包含用户信息则直接抽取其中的用户信息
Map<String, Object> userInfo = (Map<String, Object>) map.get("user_info");
@ -59,6 +60,7 @@ public class ClientUserAuthConverter implements UserAuthenticationConverter {
userInfoBO.setUserUsername(userInfo.get("username").toString());
userInfoBO.setUserName(userInfo.get("userName").toString());
userInfoBO.setUserPhone(userInfo.get("userPhone") == null ? "" : userInfo.get("userPhone").toString());
userInfoBO.setRoles(roles);
principal = userInfoBO;
LOG.debug("获取用户权限");
return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
@ -67,7 +69,7 @@ public class ClientUserAuthConverter implements UserAuthenticationConverter {
}
}
private Collection<GrantedAuthority> getAuthorities(Map<String, ?> map) {
private Collection<GrantedAuthority> getAuthorities(List<RoleBO> roles, Map<String, ?> map) {
Collection authorities = (Collection) map.get("authorities");
if (authorities.isEmpty()) {
authorities = new LinkedHashSet();
@ -88,6 +90,7 @@ public class ClientUserAuthConverter implements UserAuthenticationConverter {
RoleBO roleBO = resultObj.toJavaObject(RoleBO.class);
RoleGrantedAuthority roleGrantedAuthority = new RoleGrantedAuthority(roleBO.getRoleId(), roleBO);
roleGrantedAuthoritySet.add(roleGrantedAuthority);
roles.add(roleBO);
}
return roleGrantedAuthoritySet;
}

View File

@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* @ClassName: SecurityCompontent
@ -48,9 +49,11 @@ public class SecurityComponent {
userInfoBO.setDataAuthority(userBO.getDataAuthority());
userInfoBO.setDataAuthorityUserIds(userBO.getDataAuthorityUserIds());
userInfoBO.setBaseDepartmentIds(userBO.getBaseDepartmentIds());
userInfoBO.setRoles(userBO.getRoles());
}
if (user instanceof UserInfoBO) {
userInfoBO = (UserInfoBO) user;
}
return userInfoBO;
}

View File

@ -1,5 +1,6 @@
package com.cm.common.pojo.bos;
import java.util.ArrayList;
import java.util.List;
/**
@ -19,6 +20,7 @@ public class UserInfoBO {
private List<String> dataAuthorityUserIds;
private List<String> baseDepartmentIds;
private List<DepartmentBO> departments;
private List<RoleBO> roles;
public String getUserId() {
return userId == null ? "" : userId.trim();
@ -84,25 +86,38 @@ public class UserInfoBO {
this.departments = departments;
}
public List<RoleBO> getRoles() {
if (roles == null) {
return new ArrayList<>();
}
return roles;
}
public void setRoles(List<RoleBO> roles) {
this.roles = roles;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"userId\":")
.append("\"").append(userId).append("\"");
sb.append(",\"userUsername\":")
.append("\"").append(userUsername).append("\"");
sb.append(",\"userName\":")
.append("\"").append(userName).append("\"");
sb.append(",\"userPhone\":")
.append("\"").append(userPhone).append("\"");
sb.append(",\"dataAuthority\":")
.append("\"").append(dataAuthority).append("\"");
sb.append("\"userId\":\"")
.append(userId).append('\"');
sb.append(",\"userUsername\":\"")
.append(userUsername).append('\"');
sb.append(",\"userName\":\"")
.append(userName).append('\"');
sb.append(",\"userPhone\":\"")
.append(userPhone).append('\"');
sb.append(",\"dataAuthority\":\"")
.append(dataAuthority).append('\"');
sb.append(",\"dataAuthorityUserIds\":")
.append(dataAuthorityUserIds);
sb.append(",\"baseDepartmentIds\":")
.append(baseDepartmentIds);
sb.append(",\"departments\":")
.append(departments);
sb.append(",\"roles\":")
.append(roles);
sb.append('}');
return sb.toString();
}