springboot添加过滤器
1、
package com.gooney.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebFilter(urlPatterns = "/api/*",filterName = "loginFilter") public class LoginFileter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter"); String name=servletRequest.getParameter("name"); System.out.println(name); if(name.equals("wjl")){ filterChain.doFilter(servletRequest,servletResponse); }else { HttpServletResponse response=(HttpServletResponse)servletResponse; response.sendRedirect("/upload.html"); return; } } @Override public void destroy() { } }
2、添加@ServletComponentScan注解,否则不起作用
package com.gooney; import com.gooney.config.InterceptorConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Import; @SpringBootApplication @Import(InterceptorConfig.class) @ServletComponentScan// public class SpringbootStartApplication { public static void main(String[] args) { SpringApplication.run(SpringbootStartApplication.class, args); } }

浙公网安备 33010602011771号