2019-07-27 23:03:27 +08:00
|
|
|
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());
|
2019-08-11 20:11:51 +08:00
|
|
|
userInfoBO.setUserPhone(userBO.getUserPhone());
|
2019-12-15 00:40:43 +08:00
|
|
|
userInfoBO.setDataAuthority(userBO.getDataAuthority());
|
|
|
|
userInfoBO.setDataAuthorityUserIds(userBO.getDataAuthorityUserIds());
|
|
|
|
userInfoBO.setBaseDepartmentIds(userBO.getBaseDepartmentIds());
|
2019-07-27 23:03:27 +08:00
|
|
|
}
|
|
|
|
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) {
|
2019-12-15 00:40:43 +08:00
|
|
|
RoleGrantedAuthority roleGrantedAuthority = (RoleGrantedAuthority) grantedAuthority;
|
2019-07-27 23:03:27 +08:00
|
|
|
roleIds.add(roleGrantedAuthority.getRoleId());
|
|
|
|
}
|
|
|
|
return roleIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取当前用户名
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getCurrentUsername() {
|
|
|
|
UserInfoBO userInfoBO = getCurrentUser();
|
|
|
|
return userInfoBO.getUserUsername();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|