spring-boot 加入拦截器Interceptor

1.spring boot拦截器默认有

  • HandlerInterceptorAdapter
  • AbstractHandlerMapping
  • UserRoleAuthorizationInterceptor
  • LocaleChangeInterceptor
  • ThemeChangeInterceptor

2.配置spring mvc的拦截器WebMvcConfigurerAdapter

  1. public class WebAppConfig extends WebMvcConfigurerAdapter

3.实现添加拦截器方法

  1. public void addInterceptors(InterceptorRegistry registry){
  2. }
  3. registry.addInterceptor可以通过此方法添加拦截器, 可以是spring提供的或者自己添加的

4.实例部分

    1. public class WebAppConfig extends WebMvcConfigurerAdapter{
    2. public static void main(String[] args) {
    3. SpringApplication.run(WebAppConfig.class, args);
    4. }
    5. /**
    6. * 配置拦截器
    7. * @author lance
    8. * @param registry
    9. */
    10. public void addInterceptors(InterceptorRegistry registry) {
    11. registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns("/user/**");
    12. }
    13. }
    14. UserSecurityInterceptor代码
    15. public class UserSecurityInterceptor implements HandlerInterceptor {
    16. @Override
    17. public boolean preHandle(HttpServletRequest request,
    18. HttpServletResponse response, Object handler) throws Exception {
    19. return true;
    20. }
    21. @Override
    22. public void postHandle(HttpServletRequest request,
    23. HttpServletResponse response, Object handler,
    24. ModelAndView modelAndView) throws Exception {
    25. }
    26. @Override
    27. public void afterCompletion(HttpServletRequest request,
    28. HttpServletResponse response, Object handler, Exception ex)
    29. throws Exception {
    30. }
    31. }

5.demo地址 
https://github.com/leelance/spring-boot-all/tree/master/spring-boot-samples

 
想问下,我配置的这个拦截器为啥没有生效呢?
转自:http://lihao312.iteye.com/blog/2078139
 
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  6. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10. @Configuration
  11. public class WebSecurityConfig extends WebMvcConfigurerAdapter {
  12. /**
  13. * 登录session key
  14. */
  15. public final static String SESSION_KEY = "user";
  16. @Bean
  17. public SecurityInterceptor getSecurityInterceptor() {
  18. return new SecurityInterceptor();
  19. }
  20. @Override
  21. public void addInterceptors(InterceptorRegistry registry) {
  22. InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
  23. // 排除配置
  24. //        addInterceptor.excludePathPatterns("/error");
  25. //        addInterceptor.excludePathPatterns("/login**");
  26. // 拦截配置
  27. addInterceptor.addPathPatterns("/**");
  28. }
  29. private class SecurityInterceptor extends HandlerInterceptorAdapter {
  30. @Override
  31. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
  32. throws Exception {
  33. HttpSession session = request.getSession();
  34. if (session.getAttribute(SESSION_KEY) != null)
  35. return true;
  36. // 跳转登录
  37. String url = "/login";
  38. response.sendRedirect(url);
  39. return false;
  40. }
  41. }
  42. }
 
上一篇:运行android程序的时分出现了No compatible targets were found.Do you wish to.


下一篇:微信小程序 base64 图片 canvas 画布 drawImage 实现