网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBoot2.0设置拦截器-创新互联

所有功能完成 配置登录认证

配置拦截器

在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截
  • 新建包com.example.interceptor;

    成都创新互联从2013年开始,先为旌德等服务建站,旌德等地企业,进行企业商务咨询服务。为旌德企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
  • 创建login拦截类

package com.example.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginInterceptor implements HandlerInterceptor {    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {       //请求进入这个拦截器
        HttpSession session = request.getSession();        if(session.getAttribute("user") == null){       //判断session中有没有user信息//            System.out.println("进入拦截器");
            if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))){
                response.sendError(401);
            }
            response.sendRedirect("/");     //没有user信息的话进行路由重定向
            return false;
        }        return true;        //有的话就继续操作
    }    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
  • 在com.example包中添加拦截控制器

package com.example;import com.example.interceptor.LoginInterceptor;import com.example.interceptor.RightsInterceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.*;@Configuration          //使用注解 实现拦截public class WebAppConfigurer implements WebMvcConfigurer   {    @Autowired
    RightsInterceptor rightsInterceptor;    @Override
    public void addInterceptors(InterceptorRegistry registry) {        //登录拦截的管理器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());     //拦截的对象会进入这个类中进行判断
        registration.addPathPatterns("/**");                    //所有路径都被拦截
        registration.excludePathPatterns("/","/login","/error","/static/**","/logout");       //添加不拦截路径

    }

}
  • 在WebAppConfigurer.java中增加内容

package com.example;import com.example.interceptor.LoginInterceptor;import com.example.interceptor.RightsInterceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.*;@Configuration          //使用注解 实现拦截public class WebAppConfigurer implements WebMvcConfigurer   {    @Autowired
    RightsInterceptor rightsInterceptor;    @Override
    public void addInterceptors(InterceptorRegistry registry) {        //登录拦截的管理器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());     //拦截的对象会进入这个类中进行判断
        registration.addPathPatterns("/**");                    //所有路径都被拦截
        registration.excludePathPatterns("/","/login","/error","/static/**","/logout");       //添加不拦截路径//        super.addInterceptors(registry);


        //权限拦截的管理器
        InterceptorRegistration registration1 = registry.addInterceptor(rightsInterceptor);
        registration1.addPathPatterns("/**");                    //所有路径都被拦截
        registration1.excludePathPatterns("/","/login","/error","/static/**","/logout");       //添加不拦截路径
    }

}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前标题:SpringBoot2.0设置拦截器-创新互联
文章转载:http://bjjierui.cn/article/hgios.html

其他资讯