最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式

 更新時間:2021年08月23日 09:53:45   作者:趙小傑  
這篇文章主要介紹了SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理

微信小程序的接口驗(yàn)證防止非法請求,登錄的時候獲取openId生成一個七天有效期token存入redis中。

后續(xù)每次請求都需要把token作為參數(shù)傳給后臺接口進(jìn)行驗(yàn)證,為了方便使用@PathVariable 直接將參數(shù)做為路徑傳過來 不用每一次都添加param參數(shù)也方便前端接口的請求。

例如:

@ApiOperation(value = "小程序登錄")
@PostMapping("/login")
public AntdResponse login(@RequestParam String username, @RequestParam String password, @RequestParam String openId) throws Exception {
    String st = wxTokenService.passport(username, password);
    //省略。。。。。
    String wxToken = IdUtil.simpleUUID();
    data.put("wxToken", wxToken);
    wxTokenService.saveWxTokenToRedis(wxToken, openId);
    return new AntdResponse().success("登錄成功,登錄有效期七天").data(data);
}
@ApiOperation(value = "預(yù)約訂單")
@PostMapping("/{wxToken}/addOrder")
public AntdResponse addOrder(@PathVariable String wxToken, @RequestBody ProductOrderDto productOrderDto){
    String openId = wxTokenService.getOpenIdByWxToken(wxToken);
    orderService.addOrder(openId, productOrderDto);
    return new AntdResponse().success("預(yù)約訂單成功");
}

為了方便統(tǒng)一驗(yàn)證,基于切面來實(shí)現(xiàn)數(shù)據(jù)的驗(yàn)證

package cn.pconline.antd.smallshop.interceptor;
import cn.pconline.antd.common.exception.WxTokenException;
import cn.pconline.antd.smallshop.service.IWxTokenService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 * @Description 微信小程序登錄攔截
 * @Author jie.zhao
 * @Date 2020/10/26 18:08
 */
@Component
@Aspect
public class WxMiniInterceptor {
    @Autowired
    private IWxTokenService wxTokenService;
    
    //這里需要把登錄的請求控制器排除出去
    @Pointcut("within (cn.pconline.antd.smallshop.wxmini..*) && !within(cn.pconline.antd.smallshop.wxmini.WxMiniLoginController)")
    public void pointCut() {
    }
    @Around("pointCut()")
    public Object trackInfo(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        String wxToken = (String) pathVariables.get("wxToken");
        if (wxToken == null) {
            throw new WxTokenException("微信小程序令牌參數(shù)缺失!");
        }
        String openId = wxTokenService.getOpenIdByWxToken(wxToken);
        if (openId == null || "".equals(openId)) {
            throw new WxTokenException("登錄失效,請重新登錄!");
        }
        return joinPoint.proceed();
    }
}

全局異常處理

@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler {
    @ExceptionHandler(value = WxTokenException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public AntdResponse handleWxTokenException(WxTokenException e) {
        log.error("微信Token攔截異常信息:", e);
        return new AntdResponse().message(e.getMessage()).code(Code.C500.getCode().toString()).status(ResponseStat.ERROR.getText());
    }
}
package cn.pconline.antd.common.exception;
/**
 * 微信授權(quán)token異常
 */
public class WxTokenException extends RuntimeException  {
    private static final long serialVersionUID = -3608667856397125671L;
    public WxTokenException(String message) {
        super(message);
    }
}

這里需要注意的是 WxTokenException 要繼承RuntimeException而不是Exception,否則的話會報UndeclaredThrowableException。

java.lang.reflect.UndeclaredThrowableException
at com.insigmaunited.lightai.controller.UserController$$EnhancerBySpringCGLIB$$e4eb8ece.profile(<generated>)

異常原因:

我們的異常處理類,實(shí)際是 動態(tài)代理的一個實(shí)現(xiàn)。

如果一個異常是檢查型異常并且沒有在動態(tài)代理的接口處聲明,那么它將會被包裝成UndeclaredThrowableException.

而我們定義的自定義異常,被定義成了檢查型異常,導(dǎo)致被包裝成了UndeclaredThrowableException

java.lang.reflect.UndeclaredThrowableException的解決

這2天開始寫web接口,由于項(xiàng)目后端就我一個人,寫起來比較慢。,遇到好多奇怪的問題,也基本只能一個人去解決。今天下午給這個問題坑了半天。現(xiàn)在腦殼子還疼。

問題

業(yè)務(wù)上需要實(shí)現(xiàn)一個功能,攔截請求的參數(shù)。檢查是否包含token。項(xiàng)目是基于springmvc來實(shí)現(xiàn)的,這里很自然使用 spring 切面技術(shù)。攔截所有controller的請求,然后檢查是否攜帶了token參數(shù)。如果沒攜帶,則拋一個自定義異常。再調(diào)用 統(tǒng)一異常處理類來處理。

涉及的類如下:

package com.insigmaunited.lightai.base;
import com.insigmaunited.lightai.exception.TokenEmptyException;
import com.insigmaunited.lightai.result.Response;
import com.insigmaunited.lightai.util.StringUtil;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
 * 權(quán)限攔截AOP
 * @author Administrator
 *
 */
@Component
@Aspect
public class PermissionAop {
    private final Logger logger = LoggerFactory.getLogger(PermissionAop.class);
    // 定義切點(diǎn)Pointcut
    @Pointcut("execution(* com.insigmaunited.lightai.controller.*Controller.*(..))")
    public void pointCut(){}
    @Before("pointCut()")
    public void before() throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        System.out.println(url);
        System.out.println(method);
        System.out.println(uri);
        System.out.println(queryString);
        if (StringUtil.isNotEmpty(queryString) && queryString.indexOf("token") != -1 ){
        }else{
            throw new TokenEmptyException("token缺失");
        }
    }
}

自定義異常類

package com.insigmaunited.lightai.exception;
public class TokenEmptyException extends Exception {
    public TokenEmptyException(String message) {
        super(message);
    }
}

異常統(tǒng)一處理類

package com.insigmaunited.lightai.exception;
import com.insigmaunited.lightai.base.BaseException;
import com.insigmaunited.lightai.result.Response;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.insigmaunited.lightai.exception.TokenEmptyException;
import javax.xml.bind.ValidationException;
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice extends BaseException {
    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ValidationException.class)
    public Response handleValidationException(ValidationException e) {
        logger.error("參數(shù)驗(yàn)證失敗", e);
        return new Response().failure("validation_exception");
    }
    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Response handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        logger.error("不支持當(dāng)前請求方法", e);
        return new Response().failure("request_method_not_supported");
    }
    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public Response handleHttpMediaTypeNotSupportedException(Exception e) {
        logger.error("不支持當(dāng)前媒體類型", e);
        return new Response().failure("content_type_not_supported");
    }
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(TokenEmptyException.class)
    public Response handleTokenEmptyException(Exception e) {
        logger.error("token參數(shù)缺少", e);
        return new Response().failure("token參數(shù)缺少");
    }
    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Response handleException(Exception e) {
        logger.error("服務(wù)運(yùn)行異常", e);
        return new Response().failure("服務(wù)運(yùn)行異常");
    }
}

此時調(diào)用接口,期望返回的是應(yīng)該

{
    "success": false,
    "message": "token參數(shù)缺少",
    "data": null
}

實(shí)際返回的是

{
    "success": false,
    "message": "服務(wù)運(yùn)行異常",
    "data": null
}

控制臺的錯誤如下:

http://localhost:8080/user/3/profile
GET
/user/3/profile
null
[ ERROR ] 2017-12-08 18:29:19 - com.insigmaunited.lightai.exception.ExceptionAdvice - ExceptionAdvice.java(63) - 服務(wù)運(yùn)行異常
java.lang.reflect.UndeclaredThrowableException
    at com.insigmaunited.lightai.controller.UserController$$EnhancerBySpringCGLIB$$e4eb8ece.profile(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.Nio2Endpoint$SocketProcessor.doRun(Nio2Endpoint.java:1688)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.net.AbstractEndpoint.processSocket(AbstractEndpoint.java:914)
    at org.apache.tomcat.util.net.Nio2Endpoint$Nio2SocketWrapper$4.completed(Nio2Endpoint.java:536)
    at org.apache.tomcat.util.net.Nio2Endpoint$Nio2SocketWrapper$4.completed(Nio2Endpoint.java:514)
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
    at sun.nio.ch.Invoker$2.run(Invoker.java:218)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.insigmaunited.lightai.exception.TokenEmptyException: token缺失
    at com.insigmaunited.lightai.base.PermissionAop.before(PermissionAop.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:620)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:602)
    at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(AspectJMethodBeforeAdvice.java:41)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    ... 45 more

這很奇怪了。為何我拋的自定義異常變成了 UndeclaredThrowableException,導(dǎo)致不能被統(tǒng)一異常處理器正常處理。

原因

通過搜索引擎,最終找到的原因:

我們的異常處理類,實(shí)際是 動態(tài)代理的一個實(shí)現(xiàn)。

如果一個異常是檢查型異常并且沒有在動態(tài)代理的接口處聲明,那么它將會被包裝成UndeclaredThrowableException.

而我們定義的自定義異常,被定義成了檢查型異常,導(dǎo)致被包裝成了UndeclaredThrowableException

官方的文檔解釋

解決

知道原因就很簡單了。要么 拋 java.lang.RuntimeException or java.lang.Error 非檢查性異常, 要么接口要聲明異常。

這里選擇 修改 自定義異常為 運(yùn)行時異常即可。

package com.insigmaunited.lightai.exception;
public class TokenEmptyException extends RuntimeException {
    public TokenEmptyException(String message) {
        super(message);
    }
}

教訓(xùn)

1、自定義異常盡可能定義成 運(yùn)行時異常。

2、對異常的概念不清晰?;A(chǔ)不扎實(shí)。

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java網(wǎng)上商城項(xiàng)目第1篇之用戶注冊模塊

    java網(wǎng)上商城項(xiàng)目第1篇之用戶注冊模塊

    這篇文章主要為大家詳細(xì)介紹了java網(wǎng)上商城項(xiàng)目第1篇之用戶注冊模塊,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 在SpringBoot中使用ResponseBodyAdvice自定義響應(yīng)的代碼實(shí)現(xiàn)

    在SpringBoot中使用ResponseBodyAdvice自定義響應(yīng)的代碼實(shí)現(xiàn)

    ResponseBodyAdvice是Spring Framework中的一個接口,允許您在將響應(yīng)寫入客戶端之前自定義響應(yīng),它通常與@ControllerAdvice注釋結(jié)合使用,以跨多個控制器將全局更改應(yīng)用于響應(yīng)主體,本文介紹了如何使用ResponseBodyAdvice的基本概述,需要的朋友可以參考下
    2024-12-12
  • 深入淺析java中finally的用法

    深入淺析java中finally的用法

    finally自己由關(guān)鍵字finally和后面的finally塊組成。這篇文章重點(diǎn)給大家介紹java中finally的用法,需要的朋友參考下吧
    2018-06-06
  • Spring 配置文件字段注入到List、Map

    Spring 配置文件字段注入到List、Map

    這篇文章主要介紹了Spring 配置文件字段注入到List、Map,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • spring裝配bean的3種方式總結(jié)

    spring裝配bean的3種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于spring裝配bean的3種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 淺談java中文本框和文本區(qū)

    淺談java中文本框和文本區(qū)

    本文給大家介紹的是java中的文本框和文本區(qū)的概念和使用方法,以及簡單的示例,十分實(shí)用,有需要的小伙伴可以參考下。
    2015-06-06
  • Java:詳解Java中的異常

    Java:詳解Java中的異常

    這篇文章主要介紹了java中的異常,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-08-08
  • Java中的小知識點(diǎn)總結(jié)

    Java中的小知識點(diǎn)總結(jié)

    最近在復(fù)習(xí)Java的基礎(chǔ),遇到了一些比較偏的考核題目,特地總結(jié)一下需要注意的知識點(diǎn)!不過在使用IDE編程的時候,這些問題都會馬上被IDE識別出來,編譯是通不過的。我在這里提出來就相當(dāng)于給初學(xué)者一些貢獻(xiàn)吧
    2013-07-07
  • Spring?Data?JPA?映射VO/DTO對象方式

    Spring?Data?JPA?映射VO/DTO對象方式

    這篇文章主要介紹了Spring?Data?JPA?映射VO/DTO對象方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)順序表用法詳解

    Java數(shù)據(jù)結(jié)構(gòu)順序表用法詳解

    順序表是計算機(jī)內(nèi)存中以數(shù)組的形式保存的線性表,線性表的順序存儲是指用一組地址連續(xù)的存儲單元依次存儲線性表中的各個元素、使得線性表中在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中,即通過數(shù)據(jù)元素物理存儲的相鄰關(guān)系來反映數(shù)據(jù)元素之間邏輯上的相鄰關(guān)系
    2021-10-10

最新評論

潼南县| 西林县| 上杭县| 惠来县| 柳江县| 高淳县| 乌恰县| 连州市| 苗栗县| 永德县| 沙河市| 五河县| 博白县| 阜康市| 临朐县| 阿瓦提县| 新晃| 兴海县| 克东县| 磴口县| 洛浦县| 瑞昌市| 南丹县| 泗洪县| 丹棱县| 拉萨市| 车致| 龙泉市| 东乡族自治县| 顺昌县| 松溪县| 岑溪市| 喀喇| 浦城县| 通江县| 岐山县| 大方县| 贵阳市| 清新县| 卓尼县| 区。|