自定义拦截器

/**
 * UserSecurityInterceptor
 * Created with IntelliJ IDEA.
 * Author: yangyongkang
 * Date: 2018/8/22
 * Time: 14:20
 */
@Component
public class UserSecurityInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserModel info = (UserModel) redisCacheTemplate.opsForValue().get(request.getSession().getId());
        if (info == null || StringUtils.isEmpty(info)) {
            response.sendRedirect(request.getContextPath() + "/view/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}

配置访问路径及静态资源

/**
 * 登陆拦截控制类
 * Created with IntelliJ IDEA.
 * Author: yangyongkang
 * Date: 2018/8/22
 * Time: 14:17
 */
@Configuration
public class WebMvcConfig implements  WebMvcConfigurer {
    @Autowired
    private UserSecurityInterceptor securityInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addInterceptor = registry.addInterceptor(securityInterceptor);
        // 排除配置
        addInterceptor.excludePathPatterns("/error");
        addInterceptor.excludePathPatterns("/static/**");//排除静态资源
        addInterceptor.excludePathPatterns("/view/login");
        addInterceptor.excludePathPatterns("/login/check");
        // 拦截配置
        addInterceptor.addPathPatterns("/**");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");//
    }
}

相关文章:

  • 2021-09-16
  • 2021-10-24
  • 2021-11-30
  • 2021-09-10
  • 2023-01-12
  • 2022-12-23
  • 2021-06-19
  • 2021-03-31
猜你喜欢
  • 2021-06-03
  • 2022-02-22
  • 2022-01-12
  • 2021-07-21
  • 2022-12-23
  • 2021-07-18
  • 2021-05-31
相关资源
相似解决方案