操作步骤:
1、导入knife4j的maven坐标
2、导入knife4j相关配置类
3、设置静态资源、否则接口文档页面无法访问
4、在LoginCheckFilter中设置不需要处理的请求路径
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
在WebMvcConfig中添加注解@EnableSwagger2、@EnableKnife4j并添加如下代码:
@Bean
public Docket createRestApi() {
// 文档类型
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.haimeng.reggie.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("海梦外卖")
.version("1.0")
.description("海梦外卖接口文档")
.build();
}
设置静态资源、否则接口文档页面无法访问:
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("开始静态资源映射");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
在LoginCheckFilter中设置不需要处理的请求路径:
package com.haimeng.reggie.filter;
import com.alibaba.fastjson.JSON;
import com.haimeng.reggie.common.BaseContext;
import com.haimeng.reggie.common.R;
import com.sun.xml.internal.bind.v2.TODO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 检查用户是否完成登录
*/
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
//路径匹配器,支持通配符
public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// 1、获取本次请求的URI
String requestURI = request.getRequestURI();
log.info("拦截到请求" + requestURI);
//定义不需要处理的请求路径
//TODO 这里存在bug需要修复
String[] urls = new String[]{
"/employee/login",
"/employee/logout",
"/backend/**",
"/front/**",
"/common/**",
"/user/sendMsg", //移动端发送短信
"/user/login", //移动端登录
"/doc.html",
"/webjars/**",
"/swagger-resources",
"/v2/api-docs"
};
// 2、判断本次请求是否需要处理
boolean check = check(urls, requestURI);
// 3、如果不需要处理,则直接放行
if(check){
log.info("本次请求不需要处理:" + requestURI);
filterChain.doFilter(request,response);
return;
}
// 4-1、判断登录状态,如果已登录,则直接放行
if(request.getSession().getAttribute("employee") !=null){
log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("employee"));
// -----------------------------------------
Long empId = (Long) request.getSession().getAttribute("employee");
//通过过滤器获取当前线程中用户的id
// 并且赋值给当前线程
// 在当前线程结束前都可以通过getCurrentId方法获取当前用户的id
BaseContext.setCurrentId(empId);
// -------------------------------------------
filterChain.doFilter(request,response);
return;
}
//4-2 判断移动端登录状态,如果已登录,直接放行
if(request.getSession().getAttribute("user") !=null){
log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("user"));
// -----------------------------------------
Long userId = (Long) request.getSession().getAttribute("user");
//通过过滤器获取当前线程中用户的id
// 并且赋值给当前线程
// 在当前线程结束前都可以通过getCurrentId方法获取当前用户的id
BaseContext.setCurrentId(userId);
// -------------------------------------------
filterChain.doFilter(request,response);
return;
}
log.info("用户未登录");
// 5、如果未登录则返回未登录结果,通过输出流方式向客户端页面响应数据
response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
return; //这句代码不会被执行
}
/**
* 路径匹配,检查此次请求是否需要放行
* @param urls
* @param requestURI
* @return
*/
public boolean check(String[] urls,String requestURI){
for (String url : urls) {
boolean match = PATH_MATCHER.match(url, requestURI);
if(match){
return true;
}
}
return false;
}
}