tags: Springboot
Customize a normal interceptor
Here is the same as before to create a normal interceptor
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("My Interceptor");
return true;
}
@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 {
}
}
Create a configuration class
Create a configuration class, add the relevant interceptor in the configuration class, here you can configure multiple interceptors, add @Configuration class on this class, this will be managed by the spring container.
/**
* Interceptor configuration class
*/
@Configuration// indicates that the class will be created by the spring container
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration r1 = registry.addInterceptor(new PermissionInterceptor());
/ / Add an intercept request
r1.addPathPatterns("/*");
/ / Add unblocked requests
r1.excludePathPatterns("/login");
/ / The above is the same as the following
//registry.addInterceptor(new PermissionInterceptor()).addPathPatterns("/*").excludePathPatterns("/login");
}
}
Then enter the appropriate url in the browser to test.
method one:
Create a normal filter
@WebFilter(urlPatterns="/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("Enter filter filter");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
Add the annotation configuration filter to the entry method. @ServletComponentScan will scan the servlet-related annotations, such as @WebServlet, @WebFilter, @WebListener.
@SpringBootApplication
@ServletComponentScan(basePackages={"com.monkey1024.filter"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Method Two:
Create a configuration class configuration filter, where @Bean is equivalent to the bean tag in the previous spring configuration file
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean myFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(new MyFilter());
registration.addUrlPatterns("/*");
return registration;
}
}
method one:
Create a normal servlet
@WebServlet("/myServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = -4134217146900871026L;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("hello word");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
Add annotations to the main class and configure the package where the servlet is located
@SpringBootApplication
@ServletComponentScan(basePackages="com.monkey1024.servlet")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Method 2:
@Configuration
public class ServletConfig {
@Bean
public ServletRegistrationBean myServletRegistrationBean(){
ServletRegistrationBean registration = new ServletRegistrationBean(new MyServlet(), "/servlet/myServlet");
return registration;
}
}
Filter Intercept the client's HttpServletRequest before the HttpServletRequest reaches the servlet. Check the HttpServletRequest as needed, or modify the HttpServletRequest header and data. Intercept ...
The advantages of Spring-boot compared to Spring4: 1. Simplify the configuration process of Spring4, perform automatic configuration, and reduce the complexity of project construction (Spring4 configu...
Execution order Filter = "Interceptor =" Before = "Method =" After = "After filter Interceptor Registered interceptor Controller method Request URL http: // localhost: 8081 / ...
Original address:https://www.tsanyang.top/share-detail/836252049946443776.html Recently implemented a permission control function, you want to implement by interceptors, when the business is done as e...
Interceptors and filters Some reprinted fromSpring Boot uses filter Filter - Jianshu Filters are filtering and preprocessing processes for data. When we visit the website, we sometimes publish some se...
Implement the HandlerInterceptor interface Implement the WebMvcConfigurer interface Test Results Reference blog Spring Boot interceptor is invalid and does not work Spring-boot How to add an intercept...
Write an interceptor to implement the HandlerInterceptor. Configuration interceptor...
Interceptor Spring Boot Configuration ps. (probably not the best step, but this is probably a simple configuration, you can own the Internet to query) Step great view 1. Create an interceptor class an...
The function of HandlerInterceptor is similar to the filter, but it provides finer control capabilities: before the request is responded, after the request is responded, before the view is rendered, a...
Spring boot interceptor interceptor is executed in the servlet, Shiro should be Filter, execute before servlet. Springmvc interceptor is a priority below Shiro, Shiro is a Filter that is customized fo...