当前位置:   article > 正文

springframework.web.servlet.HandlerInterceptor 拦截器的使用_import org.springframework.web.servlet.handlerinte

import org.springframework.web.servlet.handlerinterceptor;

定义拦截器

  1. package com.telnet.controller.interceptor;
  2. import com.telnet.utils.JSONResult;
  3. import com.telnet.utils.JsonUtils;
  4. import com.telnet.utils.RedisOperator;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.servlet.HandlerInterceptor;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. public class UserTokenInterceptor implements HandlerInterceptor {
  14. @Autowired
  15. private RedisOperator redisOperator;
  16. public static final String REDIS_USER_TOKEN = "redis_user_token";
  17. @Override
  18. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  19. String userId = request.getHeader("headerUserId");
  20. String userToken = request.getHeader("headerUserToken");
  21. if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(userToken)) {
  22. String token = redisOperator.get(REDIS_USER_TOKEN + ":" + userId);
  23. if (StringUtils.isBlank(token)) {
  24. returnErrorResponse(response, JSONResult.errorMsg("请登录"));
  25. return false;
  26. } else {
  27. if (!uniqueToken.equals(userToken)) {
  28. returnErrorResponse(response, JSONResult.errorMsg("异地登录"));
  29. return false;
  30. }
  31. }
  32. } else {
  33. returnErrorResponse(response, JSONResult.errorMsg("请登录"));
  34. return false;
  35. }
  36. return true;
  37. }
  38. public void returnErrorResponse(HttpServletResponse response,
  39. JSONResult result) {
  40. OutputStream out = null;
  41. try {
  42. response.setCharacterEncoding("utf-8");
  43. response.setContentType("text/json");
  44. out = response.getOutputStream();
  45. out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));
  46. out.flush();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. } finally {
  50. try {
  51. if (out != null) {
  52. out.close();
  53. }
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. @Override
  60. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  61. }
  62. @Override
  63. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  64. }
  65. }

配置拦截器

  1. package com.telnet.config;
  2. import com.telnet.controller.interceptor.UserTokenInterceptor;
  3. import org.springframework.boot.web.client.RestTemplateBuilder;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.client.RestTemplate;
  7. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  8. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  9. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  10. @Configuration
  11. public class WebMvcConfig implements WebMvcConfigurer {
  12. // 静态资源映射
  13. @Override
  14. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  15. registry.addResourceHandler("/**")
  16. .addResourceLocations("classpath:/META-INF/resources/") // 映射swagger2
  17. .addResourceLocations("file:/workspaces/images/"); // 映射本地静态资源
  18. }
  19. @Bean
  20. public RestTemplate restTemplate(RestTemplateBuilder builder) {
  21. return builder.build();
  22. }
  23. @Bean
  24. public UserTokenInterceptor userTokenInterceptor() {
  25. return new UserTokenInterceptor();
  26. }
  27. /**
  28. * 注册拦截器
  29. * @param registry
  30. */
  31. @Override
  32. public void addInterceptors(InterceptorRegistry registry) {
  33. registry.addInterceptor(userTokenInterceptor())
  34. .addPathPatterns("/test/add")
  35. .addPathPatterns("/common/*")
  36. .excludePathPatterns("/order/deliver");
  37. WebMvcConfigurer.super.addInterceptors(registry);
  38. }
  39. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/576386
推荐阅读
相关标签
  

闽ICP备14008679号