前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot 项目整合拦截器

springboot 项目整合拦截器

作者头像
六月的雨在Tencent
发布2024-03-28 20:10:01
700
发布2024-03-28 20:10:01
举报
文章被收录于专栏:CSDNCSDN
springboot 项目整合拦截器

springboot 项目整合拦截器

创建拦截器 PermissionIntercepter

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}

拦截路径如下

代码语言:javascript
复制
permission.urlPatterns=/s1/v1/course/addCours,\
  /s1/v1/courseCom/addCourCom
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springboot 项目整合拦截器
  • springboot 项目整合拦截器
    • 创建拦截器 PermissionIntercepter
      • 创建实现类,用于实现具体业务 UserBlackIntercepter
        • 创建配置类实现WebMvcConfigurer
          • 拦截路径如下
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档