SpringBoot拦截器的使用与配置
SpringBoot拦截器的使用与配置
一、新建拦截器实现HandlerInterceptor接口
判断是否登录的拦截器
package com.briup.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
public class isLoginInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession session = request.getSession();
Object obj = session.getAttribute("customer");
System.out.println("isLoginInterceptor");
if(obj!=null) {
return true;
}else {
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
}
}
二、新建WebConfigurer配置拦截器
- - 方法一:实现WebMvcConfigurer接口
推荐使用
package com.briup.interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Autowired
private isLoginInterceptor isLoginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(isLoginInterceptor).addPathPatterns("/user/*");
}
}
注意: 使用@Autowired注入,拦截器isLoginInterceptor上必须有 @Component 注解
否则会出现如下错误:
Could not resolve view with name ‘index’ in servlet with name ‘dispatcherServlet’
Description:
Field isLoginInterceptor in com.briup.interceptor.InterceptorConfig required a bean of type 'com.briup.interceptor.isLoginInterceptor' that could not be found.
- - 方法二:继承WebMvcConfigurationSupport类
非常不推荐使用,在spring boot的自定义配置类继承 WebMvcConfigurationSupport 后,
自动配置的静态资源路径(classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。
原因:
容易出现问题
这是因为在 springboot的web自动配置类 WebMvcAutoConfiguration 上有条件注解
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
这个注解的意思是在项目类路径中 缺少 WebMvcConfigurationSupport类型的bean时该自动配置类才会生效
使用:
package com.briup.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebConfigurer extends WebMvcConfigurationSupport {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new isLoginInterceptor()).addPathPatterns("/user/*");
}
}
注意: WebMvcConfigurationSupport 类使用 @Autowired 或者 @bean 自动注入页面可能出现如下错误,直接new对象也可能出现如下错误,并且可能会失去css样式:
Could not resolve view with name ‘index’ in servlet with name ‘dispatcherServlet’
Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'
- - 方法三:继承WebMvcConfigurerAdapter 类
不推荐使用,在spring5.0版本后这个类WebMvcConfigurerAdapter被丢弃了 ,虽然还可以用,但是看起来不好
package com.briup.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebConfigurer extends WebMvcConfigurerAdapter {
@Bean
public isLoginInterceptor isLoginInterceptor() {
return new isLoginInterceptor();
};
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(isLoginInterceptor()).addPathPatterns("/user/*");
}
}
注意: 使用Bean,拦截器类上不能有@Component注解,否则会出现如下错误:
Description:
The bean ‘isLoginInterceptor’, defined in class path resource [com/briup/interceptor/InterceptorConfig.class], could not be registered. A bean with that name has already been defined in file [F:\sts\estore_spingboot\target\classes\com\briup\interceptor\isLoginInterceptor.class] and overriding is disabled.
Description:
The bean 'isLoginInterceptor', defined in class path resource [com/briup/interceptor/InterceptorConfig.class], could not be registered. A bean with that name has already been defined in file [F:\sts\estore_spingboot\target\classes\com\briup\interceptor\isLoginInterceptor.class] and overriding is disabled.
本文地址:https://blog.csdn.net/qq_48225834/article/details/112644887
上一篇: 宫女侍寝怀上皇子,太后帮助她当上贵妃