com.wangshanhai.power.config.ShanhaiPowerAnnotationPermissionsInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shanhai-power-spring-boot-starter Show documentation
Show all versions of shanhai-power-spring-boot-starter Show documentation
山海Power - 基于SpringBoot的权限组件,极致精简,只为满足简单需要。
The newest version!
package com.wangshanhai.power.config;
import com.wangshanhai.power.annotation.RequiresPermissions;
import com.wangshanhai.power.exceptions.ShanHaiNotPermissionException;
import com.wangshanhai.power.open.ShanhaiPower;
import com.wangshanhai.power.service.PermissionService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* 权限拦截器
* @author Shmily
*/
public class ShanhaiPowerAnnotationPermissionsInterceptor extends HandlerInterceptorAdapter {
/**
* 用户权限有效性校验
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
PermissionService permissionService= ShanhaiPower.loadPermissionService();
if(!(handler instanceof HandlerMethod)){
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
RequiresPermissions requiresPermissions=handlerMethod.getMethodAnnotation(RequiresPermissions.class);
if(requiresPermissions!=null){
List allPermission= permissionService.queryAllPermission(request);
if(!CollectionUtils.isEmpty(allPermission)){
for(String permissions:requiresPermissions.value()){
if(!allPermission.contains(permissions)){
throw new ShanHaiNotPermissionException("您没有操作该资源的权限");
}
}
}else{
throw new ShanHaiNotPermissionException("20002","您没有操作该资源的权限");
}
}
return true;
}
}