重回起点

导航

 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}

 

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fd.demo.SpringBootDemo02.filter.MyFilter;
import com.fd.demo.SpringBootDemo02.filter.MyHttpSessionListener;
import com.fd.demo.SpringBootDemo02.filter.MyInterceptor;

@Configuration
public class MywebConfig implements WebMvcConfigurer{

@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/zxc/foo").setViewName("foo");
}

@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/asd/**");
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Bean
public FilterRegistrationBean filterRegist(){
FilterRegistrationBean frBean = new FilterRegistrationBean();
frBean.setFilter(new MyFilter());
frBean.addUrlPatterns("/*");
System.out.println("filter");
return frBean;
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Bean
public ServletListenerRegistrationBean listenerRegist(){
ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean();
srb.setListener(new MyHttpSessionListener());
System.out.println("listener");
return srb;
}

}

 

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fd.demo.SpringBootDemo02.filter.MyHttpSessionListener;

@Controller
public class UserController {
private final Logger logger = LoggerFactory.getLogger(UserController.class);

@Value("${application.message:Hello World}")
private String message;

@GetMapping("/asd/{name}")
@ResponseBody
public String welcome(@PathVariable String name, Map<String, Object> model){
logger.info("welcome------------------------");
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}

@RequestMapping("/login")
@ResponseBody
public Object foo(){
logger.info("打印日志------------------------");
return "login";
}

@RequestMapping("/index")
@ResponseBody
public Object index(HttpServletRequest request){
HttpSession session = request.getSession(true);
session.setAttribute("zxc", "zxc");
return "index";
}

@RequestMapping("online")
@ResponseBody
public Object online(){
return "当前在线人数: " + MyHttpSessionListener.online + "人";
}
}

 

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class MyFilter implements Filter{

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
System.out.println("doFilter name = " + servletRequest.getParameter("name"));
HttpServletRequest hrequest = (HttpServletRequest)servletRequest;
HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse)servletResponse);

if(hrequest.getRequestURI().indexOf("/index") != -1
|| hrequest.getRequestURI().indexOf("/asd") != -1
|| hrequest.getRequestURI().indexOf("/online") != -1
|| hrequest.getRequestURI().indexOf("/login") != -1){
System.out.println("doFilter = hrequest.");
filterChain.doFilter(servletRequest, servletResponse);
}else{
wrapper.sendRedirect("/login");
}

}

@Override
public void destroy(){
}

@Override
public void init(FilterConfig filterConfig){
}

}

 

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyHttpSessionListener implements HttpSessionListener{

public static int online = 0;

@Override
public void sessionCreated(HttpSessionEvent se){
System.out.println("创建session");
online++;
}

@Override
public void sessionDestroyed(HttpSessionEvent se){
System.out.println("销毁session");
}
}

 

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o)throws Exception{
System.out.println("preHandle被调用");
Map map = (Map)httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
System.out.println("map.get('name') = " + map.get("name"));
System.out.println("username = " + httpServletRequest.getParameter("username"));

if(map.get("name").equals("zhangsan")){
return true;
}else{
PrintWriter printWriter = httpServletResponse.getWriter();
printWriter.write("please login again!");
return false;
}
}

@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o, ModelAndView modelAndView) throws Exception{
System.out.println("postHandle被调用");
}

@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o, Exception e) throws Exception{
System.out.println("afterCompletion被调用");
}

}

 

posted on 2019-07-02 14:55  重回起点  阅读(125)  评论(0编辑  收藏  举报