项目学习:仿b站的视频网站项目06 -视频分类01
概括
本篇主要是介绍项目的结构更改以及拦截器配置和视频分类数据库文件。
项目结构更改
1、将全局异常抽出到common目录下来进行统一管理。
2、在admin目录项新建interceptor目录来定义拦截器,当有请求访问后端地址时进行Token校验。
拦截器定义
创建一个类来实现HandlerInterceptor
这个接口。通过实现该接口提供的方法来对请求前、请求中、请求后来进行处理,有点aop切面的味道。
然后再创建一个类来实现WebMvcConfigurer
这个接口。通过重写addInterceptors
方法来将上面定义的接口注册进去管理。
参考代码如下所示:
AppInterceptor
类
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
类
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("/**");
}
}
数据库文件
下面是视频分类的数据库文件,提供,仅供参考。
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='分类表';