1 自定义拦截器
下方的redisOperator就是个工具类,对redis api 操作进行简单的封装,如果有兴趣的,可以下方的地址下载:
import com.auskat.utils.JsonUtils;
import com.auskat.utils.R;
import com.auskat.utils.RedisOperator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
* 类文件: UserTokenInterceptor
*
*
* 类描述:用户请求拦截器
*
* 作 者: AusKa_T
*
* 日 期: 2021/2/19 0019
*
* 时 间: 22:08
*
*/
public class UserTokenInterceptor implements HandlerInterceptor {
public static final String REDIS_USER_TOKEN = "redis_user_token";
@Autowired
private RedisOperator redisOperator;
/**
* 拦截请求,在访问controller请求之前
* headerUserId headerUserToken
* @param request 请求
* @param response 响应
* @param handler 拦截
* @return 返回结果
* @throws Exception 异常
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// System.out.println("进入到拦截器被拦截...");
String userToken = request.getHeader("headerUserToken");
String userId = request.getHeader("headerUserId");
if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(userToken)) {
String uniqueToken = redisOperator.get(REDIS_USER_TOKEN + ":" + userId);
if (StringUtils.isBlank(uniqueToken)) {
returnErrorResponse(response, R.errorMsg("请登录..."));
return false;
} else {
if (!uniqueToken.equals(userToken)) {
returnErrorResponse(response, R.errorMsg("账号在异地登录..."));
return false;
}
}
} else {
returnErrorResponse(response, R.errorMsg("请登录..."));
return false;
}
/*
* false: 请求被拦截,被驳回,验证出现问题
* true: 请求在经过验证之后,是OK的,可以放行
*/
return true;
}
/**
* 处理异常返回信息
* @param response 响应
* @param result 结果
*/
public void returnErrorResponse(HttpServletResponse response, R result) {
OutputStream out = null;
try {
response.setCharacterEncoding("utf-8");
response.setContentType("text/json");
out = response.getOutputStream();
out.write(Objects.requireNonNull(JsonUtils.objectToJson(result)).getBytes(StandardCharsets.UTF_8));
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 请求访问Controller之后,渲染视图之前
* @param request 请求
* @param response 响应
* @param handler 拦截
* @param modelAndView 视图
* @throws Exception 异常
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* 拦截请求,在访问controller请求之后,渲染视图之后
* @param request 请求
* @param response 响应
* @param handler 拦截
* @throws Exception 异常
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
2 注册拦截器
import com.auskat.controller.interceptor.UserTokenInterceptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 类文件: WebMvcConfig
*
*
* 类描述:
*
* 作 者: AusKa_T
*
* 日 期: 2020/11/25 0025
*
* 时 间: 13:27
*
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public UserTokenInterceptor userTokenInterceptor() {
return new UserTokenInterceptor();
}
/**
* 注册拦截器
* @param registry 注册器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(userTokenInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/admin/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
3 相关信息
博文不易,辛苦各位猿友点个关注和赞,感谢