zoukankan      html  css  js  c++  java
  • springboot项目配置拦截器,进行登陆等拦截

    新建拦截类:

    public class LoginInterceptor implements HandlerInterceptor{
    	private static Log logger = LogFactory.getLog(LoginInterceptor.class);
    	//handle 前
    	@Override
    	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    			throws Exception {
    		
    		SysUser user  = (SysUser) request.getSession().getAttribute("user");
    		logger.info("***********************执行了拦截器*************************");
    		if(user==null) {
                  //未登陆向前端返回数据 JsonResult jsonResult = new JsonResult("未登陆"); jsonResult.setFlag(0); String jsonString = JSONObject.toJSONString(jsonResult); PrintWriter writer = null; response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=utf-8"); try { writer = response.getWriter(); writer.print(jsonString); } catch (IOException e) { logger.info("未登陆",e); } finally { if (writer != null) writer.close(); } return false; }else { return true; } }     
        //post 时
    @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub }
        //controller 后 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } }

      

    新建配置类:

    对拦截器进行配置 因为springboot本来就是很大程度降低配置的繁琐,所以我觉得再去写配置文件去控制拦截器是否开启就没必要,就在类里面配了一个变量去控制是否开启。

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    @Configuration
    public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
    	//tag 为true 则为开启状态,为false 则为关闭状态
    	private boolean tag=false;
    
    	@Override
    	public void addInterceptors(InterceptorRegistry registry) {
    		
    		// addPathPatterns 用于添加拦截规则
            // excludePathPatterns 配置某些路径不拦截
    		if(tag==true) {
    			InterceptorRegistration addInterceptor  = registry.addInterceptor(new LoginInterceptor());
    			addInterceptor.excludePathPatterns("/test");
    			addInterceptor.addPathPatterns("/**");
    		}else {
    			
    		}
    		
            //如果有多个拦截器
            //InterceptorRegistration addInterceptor2  = registry.addInterceptor(new MyInterceptor2());
    		
    		super.addInterceptors(registry);
    	}
    }
    

      

  • 相关阅读:
    Google File System(中文翻译)
    Hadoop学习之路一 Single Node Setup
    大数据建模比赛--金融市场板块划分和轮动规律研究.
    华中建模-人脸识别
    计算循环队列的元素个数
    低价租用高性能GPU进行深度学习
    vscode+PyQt+QtDesigner
    mask_rcnn(Keras+TensorFlow)环境搭建_新手向(毕业设计使用,亲测可用)
    博客园美化
    Task1 赛题理解
  • 原文地址:https://www.cnblogs.com/blogwangwang/p/10684661.html
Copyright © 2011-2022 走看看