|
@@ -1,77 +0,0 @@
|
|
|
-package com.ym.mec.auth.core.service;
|
|
|
-
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-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 com.ym.mec.auth.api.dto.SysUserInfo;
|
|
|
-import com.ym.mec.auth.config.constant.SecurityConstants;
|
|
|
-import com.ym.mec.auth.service.SysUserService;
|
|
|
-
|
|
|
-@Component("pcs")
|
|
|
-public class PermissionCheckService {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private SysUserService sysUserService;
|
|
|
-
|
|
|
- public boolean hasPermissions(String... permissions) {
|
|
|
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
- if (authentication == null) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
|
-
|
|
|
- for (String perm : permissions) {
|
|
|
- for (GrantedAuthority authority : authorities) {
|
|
|
- if (StringUtils.equals(perm, authority.getAuthority())) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean hasRoles(String... roles) {
|
|
|
-
|
|
|
- if (hasPermissions(roles)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
- if (authentication == null) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- SysUserInfo userInfo = queryUserInfo(authentication.getPrincipal().toString());
|
|
|
-
|
|
|
- List<String> rolesList = Arrays.asList(userInfo.getRoles());
|
|
|
-
|
|
|
- for (String role : roles) {
|
|
|
- if (rolesList.contains(role)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- private SysUserInfo queryUserInfo(String username) {
|
|
|
- SysUserInfo userInfo = null;
|
|
|
-
|
|
|
- if (StringUtils.startsWith(username, SecurityConstants.PHONE_PRINCIPAL_PREFIX)) {
|
|
|
- userInfo = sysUserService.queryUserInfoByPhone(StringUtils.substringAfter(username, SecurityConstants.PHONE_PRINCIPAL_PREFIX));
|
|
|
- } else {
|
|
|
- userInfo = sysUserService.queryUserInfoByUsername(StringUtils.substringAfter(username, SecurityConstants.USERNAME_PRINCIPAL_PREFIX));
|
|
|
- }
|
|
|
- return userInfo;
|
|
|
- }
|
|
|
-}
|