94 lines
3.1 KiB
Java
94 lines
3.1 KiB
Java
|
package com.cm.common.component;
|
||
|
|
||
|
import com.alibaba.fastjson.JSONObject;
|
||
|
import com.cm.common.pojo.bos.RoleBO;
|
||
|
import com.cm.common.pojo.bos.RoleGrantedAuthority;
|
||
|
import com.cm.common.pojo.bos.UserBO;
|
||
|
import com.cm.common.pojo.bos.UserInfoBO;
|
||
|
import org.apache.commons.lang3.StringUtils;
|
||
|
import org.springframework.security.core.Authentication;
|
||
|
import org.springframework.security.core.GrantedAuthority;
|
||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collection;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* @ClassName: SecurityCompontent
|
||
|
* @Description: 权限工具
|
||
|
* @Author: WangGeng
|
||
|
* @Date: 2019/2/26 3:31 PM
|
||
|
* @Version: 1.0
|
||
|
**/
|
||
|
@Component
|
||
|
public class SecurityComponent {
|
||
|
|
||
|
/**
|
||
|
* 超级管理员
|
||
|
*/
|
||
|
public static final String USERNAME_ADMIN = "admin";
|
||
|
|
||
|
/**
|
||
|
* 获取当前用户
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
public UserInfoBO getCurrentUser() {
|
||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||
|
Object user = authentication.getPrincipal();
|
||
|
UserInfoBO userInfoBO = null;
|
||
|
if (user instanceof UserBO) {
|
||
|
userInfoBO = new UserInfoBO();
|
||
|
UserBO userBO = (UserBO) user;
|
||
|
userInfoBO.setUserId(userBO.getUserId());
|
||
|
userInfoBO.setUserUsername(userBO.getUsername());
|
||
|
}
|
||
|
if (user instanceof UserInfoBO) {
|
||
|
userInfoBO = (UserInfoBO) user;
|
||
|
}
|
||
|
return userInfoBO;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 角色ID列表
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
public List<String> listRoleIds() {
|
||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||
|
Collection<? extends GrantedAuthority> grantedAuthorities = authentication.getAuthorities();
|
||
|
List<String> roleIds = new ArrayList<>();
|
||
|
for (GrantedAuthority grantedAuthority : grantedAuthorities) {
|
||
|
RoleGrantedAuthority roleGrantedAuthority;
|
||
|
if (grantedAuthority instanceof RoleGrantedAuthority) {
|
||
|
roleGrantedAuthority = (RoleGrantedAuthority) grantedAuthority;
|
||
|
} else {
|
||
|
JSONObject authorityObject = JSONObject.parseObject(grantedAuthority.toString().replace("_wg_", ","));
|
||
|
if (StringUtils.contains(authorityObject.getString("authority"), "_ALL")) {
|
||
|
roleGrantedAuthority = new RoleGrantedAuthority(authorityObject.getString("authority"));
|
||
|
} else {
|
||
|
RoleBO roleBO = new RoleBO();
|
||
|
roleBO.setRoleId(authorityObject.getString("roleId"));
|
||
|
roleBO.setRoleName(authorityObject.getString("roleName"));
|
||
|
roleGrantedAuthority = new RoleGrantedAuthority(authorityObject.getString("authority"), roleBO);
|
||
|
}
|
||
|
}
|
||
|
roleIds.add(roleGrantedAuthority.getRoleId());
|
||
|
}
|
||
|
return roleIds;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取当前用户名
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
public String getCurrentUsername() {
|
||
|
UserInfoBO userInfoBO = getCurrentUser();
|
||
|
return userInfoBO.getUserUsername();
|
||
|
}
|
||
|
|
||
|
}
|