UrlAccessDecisionManager.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.diagbot.config.security;
  2. import com.diagbot.facade.TokenFacade;
  3. import com.diagbot.util.HttpUtils;
  4. import com.diagbot.util.StringUtil;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.access.AccessDecisionManager;
  7. import org.springframework.security.access.AccessDeniedException;
  8. import org.springframework.security.access.ConfigAttribute;
  9. import org.springframework.security.authentication.AccountExpiredException;
  10. import org.springframework.security.authentication.InsufficientAuthenticationException;
  11. import org.springframework.security.core.Authentication;
  12. import org.springframework.security.core.GrantedAuthority;
  13. import org.springframework.security.web.FilterInvocation;
  14. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  15. import org.springframework.stereotype.Service;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.util.Collection;
  18. /**
  19. * @Description: 自定义权限拦截
  20. * @author: gaodm
  21. * @time: 2018/8/23 13:46
  22. */
  23. @Service
  24. public class UrlAccessDecisionManager implements AccessDecisionManager {
  25. @Autowired
  26. private TokenFacade tokenFacade;
  27. @Override
  28. public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
  29. HttpServletRequest request = ((FilterInvocation) object).getHttpRequest();
  30. String url, method;
  31. if (matchPermitAllUrl(request)) {
  32. return;
  33. }
  34. if ("anonymousUser".equals(authentication.getPrincipal())) {
  35. throw new AccessDeniedException("no right");
  36. } else {
  37. String tokenStr = HttpUtils.getHeaders(request).get("Authorization");
  38. if (StringUtil.isNotEmpty(tokenStr)) {
  39. tokenStr = tokenStr.replaceFirst("Bearer ", "");
  40. Boolean res = tokenFacade.verifyToken(tokenStr, 1);
  41. if (!res) {
  42. throw new AccountExpiredException("token expire");
  43. }
  44. }
  45. for (GrantedAuthority ga : authentication.getAuthorities()) {
  46. String[] authority = ga.getAuthority().split(";");
  47. url = authority[0];
  48. method = authority[1];
  49. if (matchers(url, request)) {
  50. if (method.equals(request.getMethod()) || "ALL".equals(method)) {
  51. return;
  52. }
  53. }
  54. }
  55. }
  56. throw new AccessDeniedException("no right");
  57. }
  58. @Override
  59. public boolean supports(ConfigAttribute attribute) {
  60. return true;
  61. }
  62. @Override
  63. public boolean supports(Class<?> clazz) {
  64. return true;
  65. }
  66. private Boolean matchPermitAllUrl(HttpServletRequest request) {
  67. if (matchers("/swagger/**", request)
  68. || matchers("/v2/**", request)
  69. || matchers("/swagger-ui.html/**", request)
  70. || matchers("/swagger-resources/**", request)
  71. || matchers("/webjars/**", request)
  72. || matchers("/druid/**", request)
  73. || matchers("/actuator/**", request)
  74. || matchers("/hystrix/**", request)
  75. || matchers("/sys/user/getJwt", request)
  76. || matchers("/sys/user/refreshJwt", request)
  77. || matchers("/sys/dictionaryInfo/getDictionary", request)
  78. || matchers("/sys/user/checkToken", request)
  79. || matchers("/oauth/token", request)
  80. || matchers("/oauth/check_token", request)
  81. || matchers("/qc/behospitalInfo/analyze", request)
  82. || matchers("/cache/clear", request)
  83. || matchers("/qcMode/getMenu", request)
  84. || matchers("/", request)) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. private boolean matchers(String url, HttpServletRequest request) {
  90. AntPathRequestMatcher matcher = new AntPathRequestMatcher(url);
  91. if (matcher.matches(request)) {
  92. return true;
  93. }
  94. return false;
  95. }
  96. }