html前台返回错误,基于AOP做一个拦截并能够返回错误信息到前端的示例程序

最近基于AOP做了一个拦截并能够返回错误信息到前端的示例程序,目标

1.通过自定义注解所带参数,进入切面判断是否合法

2.合法的继续访问

3.不合法的通过自定义异常向前端返回错误信息

我能想到的有三个思路

1.filter实现

2.基于HttpServletResponse,也可以重定向

[email protected]

第一种方法,类似SpringSecurity,我没那么做。

第二种方法,无法在切面里获取HttpServletResponse对象,或是HttpServletResponse为null,所以我放弃了,我尝试了如下方法

ServletWebRequest servletContainer =(ServletWebRequest)RequestContextHolder.getRequestAttributes();

HttpServletResponse response= servletContainer.getResponse();

HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

ServletWebRequest servletWebRequest=newServletWebRequest(request);

HttpServletResponse response=servletWebRequest.getResponse();

都不行

第三种方法,调通了,自己还无法评估优劣

源代码地址如下:https://github.com/flyingJiang/InterceptorDemo

接下来我挑主要的说,顺便各位帮忙看看这样做是否有缺陷

首先,是切面。我希望实现,满足条件就禁止用户进入api,[email protected],用Before能实现吗?我没能实现,是不是我太纠结return?

packagecom.flying.aspect;importcom.flying.annotation.Auth;importcom.flying.exception.MyException;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.reflect.MethodSignature;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;

@Aspect

@Componentpublic classAuthInterceptor {private static final Logger LOGGER = LoggerFactory.getLogger(AuthInterceptor.class);private static final String NO_AUTH = "没有权限哦!这是一个切面!";

@Autowired

ControllerExceptionHandler controllerExceptionHandler;

@Around("execution(public * com.flying.controller.*.*(..)) && @annotation(auth)")public Object checkAuth(ProceedingJoinPoint pjp, Auth auth) throwsThrowable{

MethodSignature signature=(MethodSignature) pjp.getSignature();

String authString=auth.value();

LOGGER.info("checkAuth,method: {}, clientCode={}", signature.getMethod().getName(), authString);//Assume that only yes and no permissions are required for authentication

if (authString.equals("yes")){

LOGGER.warn("checkAuth,{}", NO_AUTH);return controllerExceptionHandler.handleNoAuthException(newMyException(NO_AUTH));

}else{returnpjp.proceed();

}

}

}

再者就是我引入切面异常,这样做是不是不好,还有什么号方法吗?

packagecom.flying.aspect;importcom.flying.constant.HttpCodeEnum;importcom.flying.entity.Result;importcom.flying.exception.MyException;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.web.bind.annotation.ControllerAdvice;importorg.springframework.web.bind.annotation.ExceptionHandler;importorg.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvicepublic classControllerExceptionHandler {private static final Logger LOGGER = LoggerFactory.getLogger(ControllerExceptionHandler.class);

@ExceptionHandler({ MyException.class})

@ResponseBodypublicResult handleNoAuthException(MyException e) {

LOGGER.error("handle handleNoAuthException, ex={}", e.getMessage(), e);

Result result=Result.build(HttpCodeEnum.CODE_401.getValue(),e.getMessage());returnresult;

}

}

结果截图

4112ed7455d5618a42a1968c6b8c90b3.png

480a7a019803d6a3c176701ded2f585f.png

29b4297d443d76e7b42ccd5d7b7499ce.png

这是bug吗,这个怎么删除?

原文:https://www.cnblogs.com/jianfeijiang/p/12177219.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值