spring mvc拦截器/ spring boot拦截器

Spring mvc

使用拦截器控制登录

 

public class AuditingInterceptor extends HandlerInterceptorAdapter {
   Logger
logger = Logger.getLogger(AuditingInterceptor.class);

  
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        
throws Exception {
      String currentPath = request.getRequestURI();
      String queryString = request.getQueryString();
      queryString = queryString ==
null ? "" : "?" + queryString;
     
logger.info(currentPath + queryString);
      Stu isLogin=(Stu) request.getSession().getAttribute(
"islogin");
     
     
if (isLogin == null) {
         currentPath = request.getServletContext().getContextPath();
         response.sendRedirect(currentPath +
"/stu/login.action");
        
return false;
      }
     
return true;
   }

}

 

spring mvc配置拦截器

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <mvc:exclude-mapping path="/stu/login.action" />
        <mvc:exclude-mapping path="/func1/showFunc1.action" />
        <mvc:exclude-mapping path="/func1/ajaxsel.action" />
        <bean class="com.springmy.interceptor.AuditingInterceptor">
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

 

 

spring boot

未配置

 

拦截器

package com.boothtml.interceptor;

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

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuditingInterceptor extends HandlerInterceptorAdapter {
   Logger logger = Logger.getLogger(AuditingInterceptor.class);

   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
         throws Exception {
      String currentPath = request.getRequestURI();
      String queryString = request.getQueryString();
      queryString = queryString == null ? "" : "?" + queryString;
      logger.info(currentPath + queryString);
      String isLogin=(String) request.getSession().getAttribute("islogin");
     
      if (isLogin == null) {
         currentPath = request.getServletContext().getContextPath();
         response.sendRedirect(currentPath + "/testa");
         return false;
      }
      return true;
   }

}

配置拦截器

package com.boothtml;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.boothtml.interceptor.AuditingInterceptor;

@Controller
@SpringBootApplication
@MapperScan(basePackages = "com.boothtml.mapper")
@EnableCaching
public class BootHtml implements WebMvcConfigurer {
	Logger log = Logger.getLogger(BootHtml.class);

	@RequestMapping("/testa")
	@ResponseBody
	String test() {
		log.info("======================");
		return "无权访问";
	}

	@RequestMapping("/")
	String home(Model model, HttpServletRequest request) {
		log.info("log4j properties   home() to index...............");

		model.addAttribute("req", "bootreqattr");
		request.getSession().setAttribute("session", "session attr");
		return "index";
	}

	public static void main(String[] args) {

		SpringApplication.run(BootHtml.class, args);
	}

	@Value("${spring.servlet.multipart.location}")
	String upload;

	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		log.info("+++++++++ static resource  config........................");
		registry.addResourceHandler("/upload/**").addResourceLocations("file:///" + upload);

	}

	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new AuditingInterceptor()).addPathPatterns("/**")
		.excludePathPatterns("/testa")
		.excludePathPatterns("/func1")
		.excludePathPatterns(new String[] {"/*.html","/*.js","/jss/**"});
		
		}


}

方法二

application.yml

入口

 日志

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值