springboot过滤器和拦截器以及监听器的代码怎么写


springboot过滤器和拦截器以及监听器的代码怎么写,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。简单的springboot过滤器,拦截器,监听器 使用pom.xml 4.0.0
sp
spboot
0.0.1-SNAPSHOT

org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE


org.springframework.boot
spring-boot-starter-web

org.springframework.boot
spring-boot-maven-plugin

过滤器,使用的servlet3 规范package com.soft.m;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;@WebFilter(filterName=”MyFilter”,urlPatterns=”/*”)
public class MyFilter implements Filter {
private static Logger LOG = LoggerFactory.getLogger(MyFilter.class);
public void init(FilterConfig filterConfig) throws ServletException {
LOG.info(“初始化过滤器”);
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String requestURI = req.getRequestURI();
LOG.info(“过滤到的请求—>”+requestURI);
chain.doFilter(request, response);

} public void destroy() {
LOG.info(“destroy过滤器”);
}
}
监听器 也是servlet 规范servletContextListener 监听package com.soft.m;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Component;@Component
@WebListener(“WebListener”)
@ServletComponentScan(“com.soft.m”)
public class MyListener implements ServletContextListener {
private static Logger LOG = LoggerFactory.getLogger(MyListener.class);
public void contextInitialized(ServletContextEvent sce) {
;
LOG.info(“init:”+sce.getSource().getClass().toString());
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
LOG.info(“contextDestroyed”);

}
}
session监听器package com.soft.m;import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebListener
public class MySessionListener implements HttpSessionListener {
private static Logger LOG = LoggerFactory.getLogger(MySessionListener.class); public void sessionCreated(HttpSessionEvent se) {
LOG 香港云主机.info(“sessionCreated:”+se.getSession().getId()); } public void sessionDestroyed(HttpSessionEvent se) {
LOG.info(“sessionDestroyed:”+se.getSession().getId()); }}
servlet拦截器package com.soft.m;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;public class MyInterceptor implements HandlerInterceptor {
private static Logger logger = LoggerFactory.getLogger(MyFilter.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HandlerMethod m=(HandlerMethod)handler;

logger.info(“preHandle:”+ m.getBeanType().toString());
return true;
} public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
logger.info(“postHandle”);

} public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
logger.info(“afterCompletion”); }}
拦截起配置package com.soft.m;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer { public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(new MyInterceptor()).addPathPatterns(“/*”);
} public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler(“/*.ico”);

}}
springboot 启动的类package com.soft.m;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class MApplication {
public static void main(String[] args) {
SpringApplication.run(MApplication.class, args);
}
}
controler 类例子package com.soft.m;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class HelloController {
@RequestMapping(“/hello”)
public String hello(){
return “HelloController中的hello方法”;
}
@RequestMapping(“/create”)
public String create(HttpServletRequest request){
request.getSession().setAttribute(“aaa”, “234”);
return “HelloController中的create方法”;
}
@RequestMapping(“/destory”)
public String destory(HttpServletRequest request){
request.getSession().invalidate();
return “HelloController中的destory方法”;
}}
看完上述内容,你们掌握springboot过滤器和拦截器以及监听器的代码怎么写的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注开发云行业资讯频道,感谢各位的阅读!

相关推荐: 笔记本电脑缺少一个或者多个网络协议的解决方法

小编给大家分享一下笔记本电脑缺少一个或者多个网络协议的解决方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1、到网络和共享中心,将当前的网络连接禁用,然后再重启启用。进行路由器连接…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

Like (0)
Donate 微信扫一扫 微信扫一扫
Previous 08/12 19:49
Next 08/12 19:50

相关推荐