随笔 - 832  文章 - 2  评论 - 31  阅读 - 167万

springboot拦截器

1
1.spring boot拦截器默认有HandlerInterceptorAdapter、 AbstractHandlerMapping、UserRoleAuthorizationInterceptor、LocaleChangeInterceptor、ThemeChangeInterceptor

 1、拦截器代码(需要springboot扫描到注解)

复制代码
package com.yb.fw.app.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.yb.fw.core.helper.FwConstant;

@Component
public class AuthorityInterceptor implements HandlerInterceptor {

    //@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) throws Exception {        
        String url = request.getRequestURI();
//        System.out.println("AuthorityInterceptor preHandle="+url);
        if(url.indexOf("/app/login.do")>=0) {
            return true;
        }else if(url.indexOf("/app/land.do")>=0) {
            return true;
        }else if(url.indexOf("/app/land2.do")>=0) {
            return true;
        }else if(url.indexOf("/app/check.do")>=0) {
            return true;
        }else if(url.indexOf("/app/checkpage.do")>=0) {
            return true;
        }else if(url.indexOf("/app/utility/getVerifyCode.do")>=0) {
            return true;
        }
        HttpSession session = request.getSession();
        if(session.getAttribute(FwConstant.SESSION_USERINFO) == null) {
            response.sendRedirect(request.getContextPath()+"/app/land.do");
            return false;
        }else return true;
    }
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView mv) throws Exception {
    }
    
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception exception)
            throws Exception {
    }
}
复制代码

2、拦截器配置类(需要springboot扫描到注解)

复制代码
package com.yb.fw.config;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.DispatcherServlet;
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.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.yb.fw.app.interceptor.AuthorityInterceptor;
import com.yb.fw.app.interceptor.FrontAuthorityInterceptor;
import com.yb.fw.app.interceptor.UserOpInterceptor;
import com.yb.fw.core.helper.BaseRepositoryFactoryBean;

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter{


    @Autowired
    UserOpInterceptor userOpInterceptor;
    
    @Autowired
    AuthorityInterceptor authorityInterceptor;
    
    @Autowired
    FrontAuthorityInterceptor frontAuthorityInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //多个拦截器组成一个拦截器链
        // addPathPatterns用于添加拦截规则
        // excludePathPatterns用户排除拦截
        registry.addInterceptor(frontAuthorityInterceptor).addPathPatterns("/store/**"); //根据这个判断它要不要进入自己定义的拦截器
        registry.addInterceptor(authorityInterceptor).addPathPatterns("/app/**"); //对来自/** 全路径请求进行拦截
       registry.addInterceptor(userOpInterceptor).addPathPatterns("/app/**"); //对来自/** 全路径请求进行拦截       
        super.addInterceptors(registry);
    }
    

}
复制代码

 

posted on   小破孩楼主  阅读(408)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示