RbasService.java 1.4 KB

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