赞
踩
定义拦截器
- package com.telnet.controller.interceptor;
-
- import com.telnet.utils.JSONResult;
- import com.telnet.utils.JsonUtils;
- import com.telnet.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;
-
- public class UserTokenInterceptor implements HandlerInterceptor {
-
- @Autowired
- private RedisOperator redisOperator;
-
- public static final String REDIS_USER_TOKEN = "redis_user_token";
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
-
- String userId = request.getHeader("headerUserId");
- String userToken = request.getHeader("headerUserToken");
-
- if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(userToken)) {
- String token = redisOperator.get(REDIS_USER_TOKEN + ":" + userId);
- if (StringUtils.isBlank(token)) {
- returnErrorResponse(response, JSONResult.errorMsg("请登录"));
- return false;
- } else {
- if (!uniqueToken.equals(userToken)) {
- returnErrorResponse(response, JSONResult.errorMsg("异地登录"));
- return false;
- }
- }
- } else {
- returnErrorResponse(response, JSONResult.errorMsg("请登录"));
- return false;
- }
-
- return true;
- }
-
- public void returnErrorResponse(HttpServletResponse response,
- JSONResult result) {
- OutputStream out = null;
- try {
- response.setCharacterEncoding("utf-8");
- response.setContentType("text/json");
- out = response.getOutputStream();
- out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
-
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
-
- }
- }
配置拦截器
- package com.telnet.config;
-
- import com.telnet.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;
-
- @Configuration
- public class WebMvcConfig implements WebMvcConfigurer {
-
- // 静态资源映射
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/**")
- .addResourceLocations("classpath:/META-INF/resources/") // 映射swagger2
- .addResourceLocations("file:/workspaces/images/"); // 映射本地静态资源
- }
-
- @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("/test/add")
- .addPathPatterns("/common/*")
- .excludePathPatterns("/order/deliver");
-
- WebMvcConfigurer.super.addInterceptors(registry);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。