Questions about sofaboot interceptor

My question: a sofaboot project, normal springboot project did write blocker or filters are not easy to use

I wording

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //每一个项目对于登陆的实现逻辑都有所区别,我这里使用最简单的Session提取User来验证登陆。
        //如果session中没有user,表示没登陆
        log.info("");
        return true;
    }

}

 

@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns("/**") 表示拦截所有的请求,
        // excludePathPatterns("/login", "/register") 表示除了登陆与注册之外,因为登陆注册不需要登陆也可以访问
        registry.addInterceptor(loginInterceptor).addPathPatterns("/service/00000/*").excludePathPatterns("/login", "/register");
    }
}

 I visited path

http://127.0.0.1:12100/m-pf-productfactory/service/00000/37000008

No matter what path behind addPathPatterns is two or * a * are tried

filter also tried not so that

No way to write facets

as follows

@Aspect
@Configuration
@Slf4j
@Component
public class LoginAspect {
    // 第一个*代表返回类型不限
    // 第二个*代表所有类
    // 第三个*代表所有方法
    // (..) 代表参数不限
    @Pointcut("execution(public * com.wish.biz.pf.productfactory.srv.c04320.*.*(..)) && !execution(public * com.wish.biz.pf.productfactory.srv.c04320.Srv37000048.*(..))")
    //Srv37000048为登陆类
    public void pointCut(){};

    @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            if(false){//根据业务逻辑改 可以改成true测试
                throw new BizRuntimeException(MpfProductfactoryErrCodeBussCheck.ERR_CODE_BUSS_CHECK_PF_000007,"1111111");
            }else {
                return proceedingJoinPoint.proceed();
            }
    }
}

Methods section above, there are two places you can learn

1. Exclude interception in addition to a class

& !execution

 @Pointcut("execution(public * com.wish.biz.pf.productfactory.srv.c04320.*.*(..)) && !execution(public * com.wish.biz.pf.productfactory.srv.c04320.Srv37000048.*(..))")

2.  @Before和@Around

Before being given the boot by @Before

ProceedingJoinPoint is only supported for around advice

 And JointPoint not proceed method

 

Published 50 original articles · won praise 2 · Views 9412

Guess you like

Origin blog.csdn.net/qq_35653822/article/details/104254419