RbasService.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.yihu.jw.service;
  2. import com.yihu.base.security.rbas.IRbasService;
  3. import org.springframework.context.annotation.Primary;
  4. import org.springframework.security.core.Authentication;
  5. import org.springframework.security.core.userdetails.UserDetails;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.util.AntPathMatcher;
  9. import javax.servlet.http.HttpServletRequest;
  10. import java.util.HashSet;
  11. import java.util.Set;
  12. /**
  13. * Created by chenweida on 2017/12/5.
  14. * 判断用户是否有权限访问该路径
  15. */
  16. @Primary
  17. @Service("rbasService")
  18. public class RbasService implements IRbasService {
  19. private AntPathMatcher antPathMatcher = new AntPathMatcher();
  20. @Override
  21. public Boolean hasPerssion(HttpServletRequest request, Authentication authentication) {
  22. Object principal = authentication.getPrincipal();
  23. boolean hasPerssion = false;
  24. if (principal instanceof UserDetails) {
  25. //获取用户名字
  26. String username = ((UserDetails) principal).getUsername();
  27. //获取用户全部权限
  28. Set<String> uris = new HashSet<>();
  29. for (String uri : uris) {
  30. if (antPathMatcher.match(uri, request.getRequestURI())) {
  31. hasPerssion = true;
  32. break;
  33. }
  34. }
  35. }
  36. return hasPerssion;
  37. }
  38. }