SpringBoot学习笔记:登录拦截器
程序员文章站
2024-03-17 11:47:10
...
HandlerInterceptor
是SpringWebMVC
的拦截器,类似于Servlet
开发中的过滤器Filter
,用于对请求进行拦截和处理。可以应用的场景如下:
(1) 权限检查:检测请求是否具有登录权限,如果没有直接返回到登录页面;
(2) 性能监控:用请求处理前和请求处理后的时间差计算整个请求响应完成所有消耗的时间;
(3) 日志记录:可以记录请求信息的日志,以便进行信息监控、信息统计等。
1. LoginInterceptor
实现一个简单的用户登陆拦截新建类LoginInterceptor实现HandlerInterceptor接口,实现内容代码:
package com.yuancan.blog.until;
import com.alibaba.druid.util.StringUtils;
import com.yuancan.blog.common.Result;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class LoginInterceptor implements HandlerInterceptor {
/**
* 在请求处理之前进行调用(Controller方法调用之前)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//统一拦截
String jwt = request.getHeader("Authorization");
if(StringUtils.isEmpty(jwt)){
response401(response);
return false;
}
return true;
}
/**
* 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
/**
* 无需转发,直接返回Response信息
*/
private void response401(ServletResponse response) {
HttpServletResponse httpServletResponse = WebUtils.toHttp(response);
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json; charset=utf-8");
try (PrintWriter out = httpServletResponse.getWriter()) {
Result result = new Result();
result.setCode(401);
result.setMessage("请先登录");
String data = JsonConvertUtil.objectToJson(result);
out.append(data);
} catch (IOException e) {
}
}
}
2. WebConfig
在实际的项目开发中,其实并不是所有的请求都需要被拦截。对于不需要被拦截的请求我们可以通过WebMvcConfigurer
中的addInterceptors
方法设置需要被排除拦截的请求路径。
package com.yuancan.blog.until;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册拦截器
InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
//所有路径都被拦截
registration.addPathPatterns("/**");
//添加不拦截路径
registration.excludePathPatterns("/admin/login"); //登录
registration.excludePathPatterns("/**/*.html"); //html静态资源
registration.excludePathPatterns("/**/*.js"); //js静态资源
registration.excludePathPatterns("/**/*.css"); //css静态资源
}
}