springboot 项目整合拦截器

本文详细介绍如何在SpringBoot项目中实现权限拦截器,包括创建抽象拦截器PermissionIntercepter,具体实现类UserBlackIntercepter,以及配置类PermissionConfig。通过Redis检查用户是否在黑名单中,对特定路径进行拦截。

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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn565973850

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值