项目学习:仿b站的视频网站项目06 -视频分类01

概括

本篇主要是介绍项目的结构更改以及拦截器配置和视频分类数据库文件。

项目结构更改

1、将全局异常抽出到common目录下来进行统一管理。

2、在admin目录项新建interceptor目录来定义拦截器,当有请求访问后端地址时进行Token校验。

拦截器定义

创建一个类来实现HandlerInterceptor这个接口。通过实现该接口提供的方法来对请求前、请求中、请求后来进行处理,有点aop切面的味道。

然后再创建一个类来实现WebMvcConfigurer这个接口。通过重写addInterceptors方法来将上面定义的接口注册进去管理。

参考代码如下所示:
AppInterceptor

java 复制代码
package com.easylive.admin.interceptor;

import com.easylive.component.RedisComponent;
import com.easylive.entity.constants.Constants;
import com.easylive.enums.ResponseCodeEnum;
import com.easylive.exception.BusinessException;
import com.easylive.utils.StringTools;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AppInterceptor implements HandlerInterceptor {


    @Resource
    private RedisComponent redisComponent;
    private  final static String URL_ACCOUNT = "/account";
    private  final static String URL_FILE = "/file";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (null == handler){
            return false;
        }

        if (!(handler instanceof HandlerMethod)){
            return false;
        }

        if (request.getRequestURI().contains(URL_ACCOUNT)){
            return true;
        }

        String token = request.getHeader(Constants.TOKEN_ADMIN);

//        图片
        if (request.getRequestURI().contains(URL_FILE)){
            token = getToken4Cookie(request);
        }

        if (StringTools.isEmpty(token)){
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }

        Object sessionObj = redisComponent.getTokenInfo4Admin(token);
        if (null == sessionObj){
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }

        return true;
    }

    private String getToken4Cookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            String token = null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(Constants.TOKEN_ADMIN)) {
                    token = cookie.getValue();
                    break;
                }
            }
            return token;
        }
        return null;

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

WebAppConfigure

java 复制代码
package com.easylive.admin.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebAppConfigure implements WebMvcConfigurer {

    @Resource
    private AppInterceptor appInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(appInterceptor).addPathPatterns("/**");

    }
}

数据库文件

下面是视频分类的数据库文件,提供,仅供参考。

sql 复制代码
CREATE TABLE `category_info` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增分类ID',
  `category_code` varchar(30) NOT NULL COMMENT '分类编码',
  `category_name` varchar(30) NOT NULL COMMENT '分类名称',
  `p_category_id` int(11) NOT NULL COMMENT '父类分类id',
  `icon` varchar(50) DEFAULT NULL COMMENT '图标',
  `background` varchar(50) DEFAULT NULL COMMENT '背景图',
  `sort` tinyint(4) NOT NULL COMMENT '排序号',
  PRIMARY KEY (`category_id`) USING BTREE,
  UNIQUE KEY `idx_key_category_code` (`category_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='分类表';
相关推荐
好望角雾眠5 小时前
第一阶段C#基础-10:集合(Arraylist,list,Dictionary等)
笔记·学习·c#
艾伦~耶格尔5 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
星仔编程5 小时前
python学习DAY46打卡
学习
大霞上仙5 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel
yatingliu20197 小时前
HiveQL | 个人学习笔记
hive·笔记·sql·学习
武当豆豆7 小时前
C++编程学习(第25天)
开发语言·c++·学习
风和日丽 随波逐流7 小时前
java17学习笔记-Deprecate the Applet API for Removal
笔记·学习
淮北也生橘127 小时前
Linux的ALSA音频框架学习笔记
linux·笔记·学习
diablobaal8 小时前
云计算学习100天-第17天
学习
果粒橙_LGC9 小时前
论文阅读系列(一)Qwen-Image Technical Report
论文阅读·人工智能·学习