From 53ca1699fa9a125fb4680818c55399058746ba27 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Mon, 13 Jan 2020 12:54:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BD=93=E5=89=8D=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=A7=92=E8=89=B2=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/ClientUserAuthConverter.java | 13 ++++--- .../common/component/SecurityComponent.java | 3 ++ .../com/cm/common/pojo/bos/UserInfoBO.java | 35 +++++++++++++------ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java index b7f6d03..8410d6e 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/converter/ClientUserAuthConverter.java @@ -45,12 +45,13 @@ public class ClientUserAuthConverter implements UserAuthenticationConverter { Object principal = map.get("user_name"); if (!Objects.isNull(principal)) { Collection authorities; - if("admin".equals(principal.toString())) { + List 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 userInfo = (Map) 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,13 +69,13 @@ public class ClientUserAuthConverter implements UserAuthenticationConverter { } } - private Collection getAuthorities(Map map) { + private Collection getAuthorities(List roles, Map map) { Collection authorities = (Collection) map.get("authorities"); - if(authorities.isEmpty()) { + if (authorities.isEmpty()) { authorities = new LinkedHashSet(); authorities.add(new RoleGrantedAuthority("ROLE_ALL")); authorities.add(new RoleGrantedAuthority("ROLE_GROUP_ALL")); - return authorities; + return authorities; } Map params = new HashMap<>(1); params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken()); @@ -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; } diff --git a/cloud-common/src/main/java/com/cm/common/component/SecurityComponent.java b/cloud-common/src/main/java/com/cm/common/component/SecurityComponent.java index bc213da..da7729d 100644 --- a/cloud-common/src/main/java/com/cm/common/component/SecurityComponent.java +++ b/cloud-common/src/main/java/com/cm/common/component/SecurityComponent.java @@ -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; } diff --git a/cloud-common/src/main/java/com/cm/common/pojo/bos/UserInfoBO.java b/cloud-common/src/main/java/com/cm/common/pojo/bos/UserInfoBO.java index fe427af..5b5ef22 100644 --- a/cloud-common/src/main/java/com/cm/common/pojo/bos/UserInfoBO.java +++ b/cloud-common/src/main/java/com/cm/common/pojo/bos/UserInfoBO.java @@ -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 dataAuthorityUserIds; private List baseDepartmentIds; private List departments; + private List roles; public String getUserId() { return userId == null ? "" : userId.trim(); @@ -84,25 +86,38 @@ public class UserInfoBO { this.departments = departments; } + public List getRoles() { + if (roles == null) { + return new ArrayList<>(); + } + return roles; + } + + public void setRoles(List 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(); }