• javaweb添加拦截器


    js请求后台代码添加拦截器:

    package com.ctzj.biz.isale.deploy.controller;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.apache.curator.utils.ZKPaths;
    import org.springframework.beans.factory.annotation.Autowired;
    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.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.socket.WebSocketSession;
    
    import com.ctzj.biz.isale.deploy.config.ApplicationProperties;
    import com.ctzj.biz.isale.deploy.model.Menu;
    import com.ctzj.biz.isale.deploy.model.ResponseResult;
    import com.ctzj.biz.isale.deploy.model.dataobject.DeployConfigDo;
    import com.ctzj.biz.isale.deploy.model.query.DeployConfigQuery;
    import com.ctzj.biz.isale.deploy.service.DeployConfigService;
    import com.ctzj.biz.isale.deploy.service.MenuConfigService;
    import com.ctzj.biz.isale.deploy.util.Interceptor;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    
    @RestController
    @RequestMapping(value = "/dispatcher")
    public class DispatcherController extends WebMvcConfigurerAdapter{
    
        @Autowired
        private DeployConfigService deployConfigService;
    
        @Autowired
        private MenuConfigService menuConfigService;
        @Autowired
        private ApplicationProperties properties;
    
    
        /**
         * 登录验证
         * @param request
         * @param response
         * @return
         * @throws UnsupportedEncodingException
         */
        @RequestMapping("/login")
        @ResponseBody
        public ResponseResult<String> login(HttpServletRequest request,
                HttpServletResponse response,String username,String password) throws UnsupportedEncodingException {
            HttpSession session = request.getSession();
    
            if(properties.getPstUsername().equals(username)&&properties.getPstPassword().equals(password)){
                session.setAttribute("user", username);
            }else if(properties.getProdUsername().equals(username)&&properties.getProdPassword().equals(password)){
                session.setAttribute("user", username);
            }else if(properties.getSuperUsername().equals(username)&&properties.getSuperPassword().equals(password)){
                session.setAttribute("user", username);
            }else{
                username="用户名或密码错误!!!";
                return ResponseResult.buildFailure(username);
            }
             
            return ResponseResult.buildSuccessInstance
                    (username);
        }
    
        public void addInterceptors(InterceptorRegistry registry) {
            // 多个拦截器组成一个拦截器链
            // addPathPatterns 用于添加拦截规则
            // excludePathPatterns 用户排除拦截
            registry.addInterceptor(new Interceptor()).addPathPatterns("/dispatcher/*").excludePathPatterns("/dispatcher/login");
            super.addInterceptors(registry);
        }
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            super.addResourceHandlers(registry);
        }
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/toLogin").setViewName("login.html");
            super.addViewControllers(registry);
        }
    
    }

    只要继承

    WebMvcConfigurerAdapter类后重写addInterceptors方法
    再新建拦截器的类
    package com.ctzj.biz.isale.deploy.util;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.ctzj.biz.isale.deploy.websocket.LocalShellThread;
    
    /**
     * Created by 风流丶沙.
     * Date :2017/10/25 9:36
     */
    public class Interceptor implements HandlerInterceptor {
        private static final Logger LOG = LoggerFactory.getLogger(LocalShellThread.class);
    
        @Override
        public void afterCompletion(HttpServletRequest arg0,
                HttpServletResponse arg1, Object arg2, Exception arg3)
                throws Exception {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                Object arg2, ModelAndView arg3) throws Exception {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                Object arg2) throws Exception {
            LOG.info("method={}",arg0.getMethod());
            String role = (String) arg0.getSession().getAttribute("user");
            if(null==role||"".equals(role)){
                arg1.sendRedirect("/login.html");
    //            arg0.getRequestDispatcher("/login.html").forward(arg0, arg1);
                return false; 
            }else{
                return true;
            }
        }
    
    }
    preHandle这个方法再调用controller前调用,返回true继续执行下面代码,返回false不执行后面的代码!
     
  • 相关阅读:
    Screen Space Soft Shadows
    立体涂鸦的草图渲染
    什么是Civil3D
    Visual Studio 和 .NET Framework
    编程语言的选择
    Civil3D二次开发能做些什么
    Civil3D二次开发Cookbook
    Civil3D跟AutoCAD是什么关系
    C#启示录:尝鲜版
    Thinking In DesignPattern
  • 原文地址:https://www.cnblogs.com/chafe/p/7737402.html
Copyright © 2020-2023  润新知