zoukankan      html  css  js  c++  java
  • springboot拦截器配置

    1.新增一个配置中心(创建一个配置类)

    package com.sjcf.common.Interceptor;
    
    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.Resource;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
        @Resource
        private LoginInterceptor loginInterceptor;//新增的的拦截器依赖注入
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                    .maxAge(3600).allowCredentials(true);
        }
    
        /**
         * Description : Group :
         * <p>
         * 实现自定义拦截器只需要3步 1、创建我们自己的拦截器类并实现 HandlerInterceptor 接口。
         * 2、创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。
         * 3、实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)。
         *
         * @param registry
         * @author honghh
         * @date 2019/3/22 0022 10:08
         * @author luoc
         * @date 2018/8/13 13:56
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    
            InterceptorRegistration registration = registry.addInterceptor(loginInterceptor);
            // 拦截配置
            registration.addPathPatterns("/**/**");
            List<String> list = new ArrayList<String>();
            // 排除配置
            list.add("/weixin/**");
            list.add("/case/**");
    
            list.add("/swagger-ui.html");
            list.add("/swagger-resources");
            list.add("/swagger-resources/**");
            list.add("/v2/api-docs");
            list.add("/swagger/**");
            list.add("/**/v2/api-docs");
            list.add("/swagger-resources/**/**");
            list.add("/**/*.js");
            list.add("/**/*.css");
            list.add("/**/*.png");
            list.add("/**/*.ico");
            list.add("/**/*.js");
            list.add("/**/*.js");
    
            list.add("/item/getItemClassOne");
            list.add("/item/getItemClassTwo");
            list.add("/winvk/**");
    //        registration.excludePathPatterns("/weixin/**");
    //        registration.excludePathPatterns("/case/**");
            registration.excludePathPatterns(list);
        }
    }

    2.这里是新增的拦截器loginInterceptor

    package com.sjcf.common.Interceptor;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    import com.sjcf.common.ResultData;
    import cn.hutool.core.util.StrUtil;
    import cn.hutool.crypto.SecureUtil;
    import cn.hutool.json.JSONObject;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * 验证是否登录
     * 
     * @author luoc
     *
     */
    @Slf4j //这个是日志,不用取消就行
    @Component
    public class LoginInterceptor extends HandlerInterceptorAdapter {
    
        /**
         * 验证是否登录
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
    
            boolean bool = true;
    //        String userid = request.getParameter("userid");
    //        if (StrUtil.isEmpty(userid)) {
    //            bool =  false;
    //        }
            String session_key = request.getParameter("session_key");
            if (StrUtil.isEmpty(session_key)) {
                bool =  false;
            }
            String token = request.getParameter("token");
            if (StrUtil.isEmpty(token)) {
                bool =  false;
            }
            // token验证
            if (!SecureUtil.md5(session_key).equals(token)) {
                bool =  false;
            }
            if (!bool) {
    //             String path = request.getScheme() //当前链接使用的协议
    //                        +"://" + request.getServerName()//服务器地址 
    //                        + ":" + request.getServerPort(); //端口号 
    //             log.info(path+"/weixin/error");
    //             response.sendRedirect(path+"/weixin/error");
                PrintWriter writer = null;
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html; charset=utf-8");
                try {
                    writer = response.getWriter();
                    writer.print(new JSONObject(ResultData.loginError()));
    
                } catch (IOException e) {
                    log.error("response error", e);
                } finally {
                    if (writer != null)
                        writer.close();
                }
            }
            return bool;
        }
    }

    最后重启测试这样就配置好了

  • 相关阅读:
    发邮件遇到 Failure sending mail.The remote name could not be resolved: 'www.youdomain.com' 问题的解决方法
    machine.config, inetinfo.exe, aspnet_wp.exe, aspnet_state.exe这些文件的作用于位置.
    IIS的变迁(IIS3, IIS4, IIS5, IIS6, IIS7)
    精简代码 (转)
    新年第一帖——元旦这天骑车迷路了
    我是月亮,也想做那天上的太阳
    记几点吧
    谈谈电影
    闺蜜
    大气
  • 原文地址:https://www.cnblogs.com/study-together/p/13781599.html
Copyright © 2011-2022 走看看