1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package com.yihu.jw.service;
- import com.yihu.base.security.rbas.IRbasService;
- import org.springframework.context.annotation.Primary;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Service;
- import org.springframework.util.AntPathMatcher;
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Created by chenweida on 2017/12/5.
- * 判断用户是否有权限访问该路径
- */
- @Primary
- @Service("rbasService")
- public class RbasService implements IRbasService {
- private AntPathMatcher antPathMatcher = new AntPathMatcher();
- @Override
- public Boolean hasPerssion(HttpServletRequest request, Authentication authentication) {
- Object principal = authentication.getPrincipal();
- boolean hasPerssion = false;
- if (principal instanceof UserDetails) {
- //获取用户名字
- String username = ((UserDetails) principal).getUsername();
- //获取用户全部权限
- Set<String> uris = new HashSet<>();
- for (String uri : uris) {
- if (antPathMatcher.match(uri, request.getRequestURI())) {
- hasPerssion = true;
- break;
- }
- }
- }
- return hasPerssion;
- }
- }
|