PermissionCheckService.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.keao.edu.user.config;
  2. import com.keao.edu.auth.api.client.SysUserFeignService;
  3. import com.keao.edu.auth.api.entity.SysUser;
  4. import com.keao.edu.auth.api.util.SecurityUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.GrantedAuthority;
  9. import org.springframework.stereotype.Component;
  10. import java.util.Collection;
  11. @Component("pcs")
  12. public class PermissionCheckService {
  13. @Autowired
  14. private SysUserFeignService sysUserFeignService;
  15. public boolean hasPermissions(String... permissions) {
  16. Authentication authentication = SecurityUtils.getAuthentication();
  17. if (authentication == null) {
  18. return false;
  19. }
  20. SysUser user = sysUserFeignService.queryUserInfo();
  21. if (user.getIsSuperAdmin()) {
  22. return true;
  23. }
  24. Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
  25. for (String perm : permissions) {
  26. for (GrantedAuthority authority : authorities) {
  27. if (StringUtils.equalsIgnoreCase(perm, authority.getAuthority())) {
  28. return true;
  29. }
  30. }
  31. }
  32. return false;
  33. }
  34. public boolean hasRoles(String... roles) {
  35. return hasPermissions(roles);
  36. }
  37. }