springboot 项目整合拦截器
springboot 项目整合拦截器
创建拦截器 PermissionIntercepter
package com.dongao.project.aspectj.interceptor; import com.dongao.project.api.domain.Constants; import com.dongao.project.aspectj.json.Json; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.framework.web.domain.AjaxResult; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author: dongao * @create: 2019/10/31 */ @Component public abstract class PermissionIntercepter extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (this.isBlack(request)) { AjaxResult ajaxResult = new AjaxResult(); ajaxResult.put("code", Constants.CODE.USER_IN_BLACKLIST.getValue()); ajaxResult.put("msg", Constants.CODE.USER_IN_BLACKLIST.getDescription()); ajaxResult.put("obj", ""); ServletUtils.renderString(response, Json.marshal(ajaxResult)); return false; } return true; } /** * 判断是否是黑名单用户 * @param request * @return * @throws Exception */ public abstract boolean isBlack(HttpServletRequest request) throws Exception; }
创建实现类,用于实现具体业务 UserBlackIntercepter
package com.dongao.project.aspectj.interceptor.impl; import com.dongao.project.api.domain.Constants; import com.dongao.project.aspectj.interceptor.PermissionIntercepter; import com.dongao.project.utils.RedisUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; /** * 判断是否是黑名单用户 * @author: dongao * @create: 2019/10/31 */ @Component public class UserBlackIntercepter extends PermissionIntercepter { private static final Logger logger = LoggerFactory.getLogger(UserBlackIntercepter.class); @Override public boolean isBlack(HttpServletRequest request) throws Exception { String userId = request.getParameter("userId"); logger.info("开始校验当前用户userId:【{}】是否是黑名单用户======",userId); boolean result = RedisUtils.hasKey(Constants.COMMUNITY_USER_BLACK_KEY + userId); logger.info("校验当前用户userId:【{}】是否是黑名单用户结果result:【{}】======",userId,result); return result; } }
创建配置类实现WebMvcConfigurer
package com.dongao.project.config; import com.dongao.project.aspectj.interceptor.PermissionIntercepter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @ClassName PermissionConfig * @Author dongao * @Version 1.0 * @Date 2019/10/31 0028 下午 8:07 **/ @Configuration public class PermissionConfig implements WebMvcConfigurer { @Autowired private PermissionIntercepter permissionIntercepter; /** * 自定义拦截规则 */ @Override public void addInterceptors(InterceptorRegistry registry) { String permissionUrl = ConfigConstant.permissionUrl; String[] split = permissionUrl.split(","); registry.addInterceptor(permissionIntercepter).addPathPatterns(split); } }
拦截路径如下
permission.urlPatterns=/s1/v1/course/addCours,\ /s1/v1/courseCom/addCourCom