use of com.github.liuweijw.system.api.model.AuthPermission in project fw-cloud-framework by liuweijw.
the class PermissionFeignApiClient method findMenuByRole.
@Override
@ApiOperation(httpMethod = GET, value = "通过角色获取菜单权限")
@ApiImplicitParam(name = "roleCode", value = "用户roleCode", required = true, dataType = "string", paramType = "path")
public Set<AuthPermission> findMenuByRole(@PathVariable("roleCode") String roleCode) {
Set<AuthPermission> permissions = new HashSet<AuthPermission>();
if (StringHelper.isBlank(roleCode))
return permissions;
Set<AuthMenu> menus = menuService.findMenuByRole(roleCode);
if (null == menus || menus.size() == 0)
return permissions;
menus.stream().forEach(r -> {
permissions.add(new AuthPermission(r.getUrl()));
});
return permissions;
}
use of com.github.liuweijw.system.api.model.AuthPermission in project fw-cloud-framework by liuweijw.
the class PermissionServiceImpl method hasPermission.
@SuppressWarnings("unchecked")
@Override
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
// options 跨域配置,现在处理是通过前端配置代理,不使用这种方式,存在风险
/*
* if (HttpMethod.OPTIONS.name().equalsIgnoreCase(request.getMethod())) { return true; }
*/
Object principal = authentication.getPrincipal();
List<SimpleGrantedAuthority> grantedAuthorityList = (List<SimpleGrantedAuthority>) authentication.getAuthorities();
boolean hasPermission = false;
if (null == principal)
return hasPermission;
if (CollectionUtils.isEmpty(grantedAuthorityList))
return hasPermission;
String token = JwtUtil.getToken(request);
if (null == token) {
log.warn("==> gateway|permissionService 未获取到Header Authorization");
return hasPermission;
}
if (!"anonymousUser".equals(principal.toString())) {
RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
tokenStore.setPrefix(SecurityConstant.PREFIX);
OAuth2AccessToken accessToken = tokenStore.readAccessToken(token);
if (null == accessToken || accessToken.isExpired()) {
log.warn("==> gateway|permissionService token 过期或者不存在");
return hasPermission;
}
}
// 接口层面做了缓存处理,后续可以继续优化
Set<AuthPermission> permissions = new HashSet<AuthPermission>();
for (SimpleGrantedAuthority authority : grantedAuthorityList) {
permissions.addAll(permissionFeignApi.findMenuByRole(authority.getAuthority()));
}
// 网关处理是否拥有菜单权限,菜单下的功能权限校验由调用子模块负责
String requestURI = request.getRequestURI();
for (AuthPermission menu : permissions) {
if (StringHelper.isNotEmpty(menu.getUrl()) && antPathMatcher.match(menu.getUrl(), requestURI)) {
hasPermission = true;
break;
}
}
return hasPermission;
}
Aggregations