MyAccessDecisionManager.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.xintong.visualinspection.securityTools;
  2. import org.springframework.security.access.AccessDecisionManager;
  3. import org.springframework.security.access.AccessDeniedException;
  4. import org.springframework.security.access.ConfigAttribute;
  5. import org.springframework.security.authentication.InsufficientAuthenticationException;
  6. import org.springframework.security.core.Authentication;
  7. import org.springframework.security.core.GrantedAuthority;
  8. import org.springframework.stereotype.Service;
  9. import java.util.Collection;
  10. import java.util.Iterator;
  11. /**
  12. * 文件名:MyAccessDecisionManager
  13. * 版本信息:日期:2017/3/31 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
  14. */
  15. @Service
  16. public class MyAccessDecisionManager implements AccessDecisionManager {
  17. @Override
  18. public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
  19. if(null== configAttributes || configAttributes.size() <=0) {
  20. return;
  21. }
  22. ConfigAttribute c;
  23. String needRole;
  24. for(Iterator<ConfigAttribute> iter = configAttributes.iterator(); iter.hasNext(); ) {
  25. c = iter.next();
  26. needRole = c.getAttribute();
  27. for(GrantedAuthority ga : authentication.getAuthorities()) {
  28. if(needRole.trim().equals(ga.getAuthority())) {
  29. return;
  30. }
  31. }
  32. }
  33. throw new AccessDeniedException("no right");
  34. }
  35. @Override
  36. public boolean supports(ConfigAttribute attribute) {
  37. return true;
  38. }
  39. @Override
  40. public boolean supports(Class<?> clazz) {
  41. return true;
  42. }
  43. }