RbasService.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.yihu.jw.config.security;
  2. import com.yihu.base.security.rbas.RbasServiceProvider;
  3. import org.springframework.security.core.Authentication;
  4. import org.springframework.security.core.userdetails.UserDetails;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.util.AntPathMatcher;
  7. import javax.servlet.http.HttpServletRequest;
  8. import java.util.HashSet;
  9. import java.util.Set;
  10. /**
  11. * Created by chenweida on 2017/12/5.
  12. * 判断用户是否有权限访问该请求路径
  13. */
  14. @Service("rbasServiceProvider")
  15. public class RbasService implements RbasServiceProvider {
  16. private AntPathMatcher antPathMatcher = new AntPathMatcher();
  17. @Override
  18. public Boolean hasPerssion(HttpServletRequest request, Authentication authentication) {
  19. Object principal = authentication.getPrincipal();
  20. boolean hasPerssion = false;
  21. if (principal instanceof UserDetails) {
  22. //获取用户名字
  23. String username = ((UserDetails) principal).getUsername();
  24. String[] saasPhone = username.split(",");
  25. String saas = saasPhone[0];
  26. String phone = saasPhone[1];
  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. return hasPerssion;
  36. }else{
  37. return true;
  38. }
  39. }
  40. }