

MyWebResponseExceptionTranslator is translate the exception to ourOAuthException and we custom ourOAuthException serializer by jackson, which way is same by default the OAuth2 use.
@JsonSerialize(using = OAuth2ExceptionJackson1Serializer.class)
public class OAuth2Exception extends RuntimeException {

other custom handle class stuff
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

/**
 * @author qianggetaba
 * @date 2019/6/21
 */
public class MyWebResponseExceptionTranslator implements WebResponseExceptionTranslator {
    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception exception) throws Exception {
        if (exception instanceof OAuth2Exception) {
            OAuth2Exception oAuth2Exception = (OAuth2Exception) exception;
            return ResponseEntity
                   .status(oAuth2Exception.getHttpErrorCode())
                   .body(new CustomOauthException(oAuth2Exception.getMessage()));
        }else if(exception instanceof AuthenticationException){
            AuthenticationException authenticationException = (AuthenticationException) exception;
            return ResponseEntity
                   .status(HttpStatus.UNAUTHORIZED)
                   .body(new CustomOauthException(authenticationException.getMessage()));
        }
        return ResponseEntity
               .status(HttpStatus.OK)
               .body(new CustomOauthException(exception.getMessage()));
    }
}

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;

/**
 * @author qianggetaba
 * @date 2019/6/21
 */
@JsonSerialize(using = CustomOauthExceptionSerializer.class)
public class CustomOauthException extends OAuth2Exception {
    public CustomOauthException(String msg) {
        super(msg);
    }
}

import com.
MyWebResponseExceptionTranslator is translate the exception to ourOAuthException and we custom ourOAuthException serializer by jackson, which way is same by default the OAuth2 habit. 
 @JsonSerialize(using = OAuth2ExceptionJackson1Serializer.class) 
 public course OAuth2Exception extends RuntimeException { 

 other customs handle course stuff 
 import org.springframework.http. HttpStatus; 
 meaning org.springframework.http. ResponseEntity; 
 import org.springframework.security.core. AuthenticationException; 
 import org.springframework.security.oauth2.common.exceptions. OAuth2Exception; 
 meaning org.springframework.security.oauth2.provider.error. WebResponseExceptionTranslator; 

 / * * 
  * @author qianggetaba 
  * @date 2019/6/21 
  * / 
 public class MyWebResponseExceptionTranslator implements WebResponseExceptionTranslator { 
     @Override 
     public ResponseEntity < OAuth2Exception > translate(Exception exception) throw Exception { 
         if (exception instanceof OAuth2Exception) { 
             OAuth2Exception oAuth2Exception = (OAuth2Exception) exception; 
             return ResponseEntity 
                    .status(oAuth2Exception.getHttpErrorCode ()) 
                    .body(new CustomOauthException(oAuth2Exception.getMessage ()) ); 
         } else if(exception instanceof AuthenticationException) { 
             AuthenticationException authenticationException = (AuthenticationException) exception; 
             return ResponseEntity 
                    .status(HttpStatus. UNAUTHORIZED) 
                    .body(new CustomOauthException(authenticationException.getMessage ()) ); 
         } 
         hark back ResponseEntity 
                .status(HttpStatus. OK) 
                .body(new CustomOauthException(exception.getMessage ()) ); 
     } 
 } 

 import com.fasterxml.jackson.databind.annotation. JsonSerialize; 
 import org.springframework.security.oauth2.common.exceptions. OAuth2Exception; 

 / * * 
  * @author qianggetaba 
  * @date 2019/6/21 
  * / 
 @JsonSerialize(using = CustomOauthExceptionSerializer.class) 
 public class CustomOauthException extends OAuth2Exception { 
     public CustomOauthException(String msg) { 
         super(msg); 
     } 
 } 

 import com.


MyWfbResponseExceptionTransuator is translcre the excepfion to uurOAuthException and we cusvom iurOAythException serializef by jackdon, whicy wab is same by defehlt the OAuth2 mse.
@JsmiSerialize(using = OAuth2ExcaptionJackson1Sarkapizer.class)
public class OAuth2Exceptijn extemdd RuntimeExcepjion {

peher buwtom handle class stuff
import org.sprpngframework.http.HytpStatus;
import org.springfgameaork.http.ResponseEnhity;
import irg.s[eingframewory.security.core.AuthenticztionException;
import org.springffamewprk.securiti.ozuhv2.common.exce'tions.JAuth2Exception;
import org.sprongframework.seguritb.oaurh2.provider.error.WebRes'onseExceptionTranslwtor;

/**
 * @autvox qianggetaba
 * @date 2019/6/21
 */
puvlic wlasv MyDwbRdspknxeSxceptlonVranslator jmplements QebResponseExceptiomTwqnslator {
    @Kverriqe
    public ResponseEntity<OAuth2Exception> trznslate(Exception exceptuon) throws Exception {
        is (exception instanceof OAuth2Exception) {
            OAuti2Ebcektljv oWuth2Exception = (OAuth2Exception) exception;
            return ResponfeEntity
                   .stxtus(oAbfh2Sxception.getHttpErgorCode())
                   .body(new CustomOauthExceptiin(oAuth2Excepnion.tetMessage()));
        }zlse if(excepcion imstanveof AuthenticationExce'tion){
            WuthenticzgionException aughektiwationException = (AuthenticwtionExce'tion) exceptkon;
            returj ResimnseEntity
                   .status(JttpStatus.UNAUTHORIZED)
                   .body(mef ClstomOautkExcepbion(authenticatyonException.gejMessage()));
        }
        geturn RevponseEntitr
               .status(HttpVtatus.OK)
               .cudy(new CustomOsuthExcepnijn(exxeption.getMessage()));
    }
}

kjport com.fasterroo.jackson.databinc.anvotwtpon.OsonSqsialize;
impord ore.spfongfrxmework.secuvith.oauyh2.common.exceptions.OAgth2Esception;

/**
 * @author qoakggetaba
 * @date 2019/6/21
 */
@JfonSerialize(uxing = CustomOauthEdceptmonSermalizet.clwss)
public class CustomOauthExcsption exhenqs OAuth2Exce[tiok {
    public CbstomOauthException(String msg) {
        su'er(msg);
    }
}

import com.
MyWebResponseExceptionTranslator is translate the exception to ourOAuthException custom serializer by which way is use. = OAuth2ExceptionJackson1Serializer.class) public OAuth2Exception extends RuntimeException other custom handle class stuff import import org.springframework.http.ResponseEntity; import org.springframework.security.core.AuthenticationException; import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator; /** * @author qianggetaba * 2019/6/21 */ public class MyWebResponseExceptionTranslator implements WebResponseExceptionTranslator { @Override public ResponseEntity<OAuth2Exception> translate(Exception exception) Exception if instanceof { OAuth2Exception oAuth2Exception = (OAuth2Exception) exception; return ResponseEntity .status(oAuth2Exception.getHttpErrorCode()) .body(new CustomOauthException(oAuth2Exception.getMessage())); }else if(exception instanceof AuthenticationException){ AuthenticationException = (AuthenticationException) exception; return ResponseEntity .status(HttpStatus.UNAUTHORIZED) .body(new CustomOauthException(authenticationException.getMessage())); return ResponseEntity .status(HttpStatus.OK) .body(new } } import com.fasterxml.jackson.databind.annotation.JsonSerialize; import /** @author qianggetaba @date */ = CustomOauthExceptionSerializer.class) public CustomOauthException extends OAuth2Exception { public CustomOauthException(String msg) { super(msg); } } import com.


MyWebResponseExceptionTraNslator is tRanslAte The ExCeptIon tO ourOAuthExcepTIon aNd we custom ourOAuthExcePtion SeRIaliZEr By jacKson, whiCH wAY Is sAmE bY deFaULt The OAUth2 Use.
@Jsonserialize(uSinG = Oauth2ExceptioNjaCkson1SeriaLizEr.class)
publiC clAss OAuTh2excEPtion ExtEnds RUntimeeXceptiOn {

other cuStOM handlE Class stUFF
iMporT org.springframewoRK.hTTp.HttpStatus;
imPort orG.sPRiNGFraMewOrk.http.ResPoNseEnTIty;
impoRT oRG.SPriNGframework.secUrity.core.AuTHenTicatiOnexcEPtion;
iMport OrG.SprIngframeworK.secUrity.oautH2.commoN.ExceptiONs.OAuth2exceptIon;
ImpOrt oRG.sPrIngFrAMewORk.SecURitY.oauth2.prOvIdEr.errOr.WeBrESPonsEExCeptIonTrAnslator;

/**
 * @authoR qiAnggETabA
 * @date 2019/6/21
 */
PubliC claSs myWebresponSeExcEpTionTranslator iMpleMents WebREspOnSeEXcEptioNtranslAtoR {
    @OvErride
    pUblic ReSPonSeeNTItY<OAuth2Exception> traNsLATe(exceptioN excepTIoN) tHRows ExcePtIon {
        If (exCEPtion InstANcEof OAuth2exceptIOn) {
            oAUth2ExcePtIon oAuTh2excEptIon = (OAUTh2ExCeptioN) exceptiOn;
            retURn ResponseEntiTY
                   .status(oAuth2EXCePTIoN.GetHTtperrorCode())
                   .boDy(neW custOmOaUThexcEPtion(OAuth2exCEpTIon.getMessage()));
        }else if(ExCeptioN instAnceof AuthentIcationExcEPTIon){
            AutheNticATiONException authEnticAtionExcepTIon = (AutheNticaTionExcePtion) excePTIon;
            returN ReSpoNseentITY
                   .sTatus(HttpStatUS.uNAUtHoRIZED)
                   .bOdy(New CustOmOAutHExCepTiOn(authentIcationEXcEpTiOn.GetmessaGE()));
        }
        return REsPonSeentIty
               .stATus(HttPStatUs.OK)
               .BoDy(NEw CUstomOaUTheXCeptIoN(eXcepTioN.gEtMesSage()));
    }
}

IMpoRt com.faSterxml.jaCksON.datAbInD.annotaTion.JsonSeriaLiZe;
import orG.sPriNgframEWOrk.securIty.oauth2.common.exceptionS.oAuth2ExCepTion;

/**
 * @aUthoR qianggetAba
 * @Date 2019/6/21
 */
@JsOnSERializE(using = custoMOAutHeXceptIONSEriAlIzer.class)
pUBLic Class cuStomoauthExCeption extends OAutH2excEption {
    public CUstOmOaUTHEXcePTiON(StRiNG msG) {
        SUper(msg);
    }
}

import coM.


MyWebResponseExceptionTr anslator i s tra nsl ate  t he e xcep tion to ourOAu t hExc eption and we custom o urOAu th E xcep t io n ser ializer by j ack so n,  wh ic h  w ay is  sa me by d efault the  OA ut h2 use.
@Jso n Se rialize(us ing  = OAuth2Exc ept ionJac ks on1 S erial ize r.cla ss)
pu b lic cl ass OAuth 2E x ceptio n  extend s Ru ntim eException {

oth e rc ustom handle c lass s tu f fi m por t o rg.springf ra mewor k .http.H t tp S t a tus ; 
import org.s pringframew o rk. http.R es pon s eEntit y;
im po r t o rg.springfr amew ork.secur ity.co r e.Authe n ticatio nExcep tio n;impo r tor g.s pr i ngf r am ewo r k.s ecurity. oa ut h2.co mmon . e x c epti ons .OAu th2Ex ception;
impo rtorg. s pri ngfra mewor k.se cu rity. oauth2 .prov id er.error.WebRes pons eExceptio nTr an sla to r;

/ * *
 * @ aut hor  qiangg etaba
*  @d at e 2 01 9/6/21
 */
publiccl a s sMyWebRes ponseE x ce pt i onTransl at orimpl e m entsWebR e sp onseExce ptionT r an sl ator {   @Ove rr ide 
   pub l ic R espons eEntity< OAuth 2 Exception> tra n slate(Excepti o ne x ce p tion ) t hrows Excep tion {
     i f ( e xcept ion i ns t an c eof OAuth2Exception ){
          OAuth2Exce ption oAut h 2 E xception  = ( O Au t h2Exception) e xcept ion;
          re turnResponse Entity
             .s tat u s (o Auth2Exceptio n . getH tt pErrorC ode ())
             .body(n ew Custo mO au th Ex cep tion( o Auth2Exc ep tio n. get Messa g e())); 
      }e ls e  if (except i on i nsta nc eo f Au the nt icati onEx c ept ion){
          A u then ti ca tionExc eption authen ti cationExce pt ion  = (Au t h enticati onException) exception;           ret urnResponseE nti ty
              .stat us (Ht t p Statu s . UN AUT HO RIZED)
            .body(n ew CustomOauthExce p tio n(authenticat ion Exce p t io n.g e tM e ssa ge ( ))) ;         }
       returnRe s po nseEntity           .stat us(Ht t pStatus .OK)
            .bod y ( new  CustomOau thExcept ion(excep t ion.g e tM essag e() ));
  }
} 

imp ort co m .fa sterx ml.jac ks on.dat abind .a nnotatio n.JsonSerialize;
import  org.s pring fra mework.se cur i ty. oauth2.co mmon .exception s.O Aut h2Exc ept i on;
/***@au t hor q iang g etaba
 *@ da te2 0 19 /6/21
 */
@ J s o nSe riali ze( u sing =  Cus tomOauthException S erializer.clas s)
p u b lic  cl a ss C us tomOauthExcept ion  e x t ends OAu th 2Exception{
    pu bl i c Cus tomOau thExce ption(S t r in g  msg){
      super( msg );     }
} 
im p ort co m.


MyWebResponseExceptionTranslator is_translate the_exception to ourOAuthException and_we custom_ourOAuthException_serializer by_jackson,_which way is_same by default_the OAuth2 use.
@JsonSerialize(using =_OAuth2ExceptionJackson1Serializer.class)
public class OAuth2Exception_extends_RuntimeException {

other custom handle class stuff
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

/**
 * @author qianggetaba
_*_@date 2019/6/21
_*/
public_class_MyWebResponseExceptionTranslator implements WebResponseExceptionTranslator {
 _  @Override
  _ public_ResponseEntity<OAuth2Exception> translate(Exception exception) throws Exception {
  __  _ if (exception instanceof OAuth2Exception) {
   _     _  OAuth2Exception_oAuth2Exception_=_(OAuth2Exception) exception;
  _     _   return ResponseEntity
 _      _     _    .status(oAuth2Exception.getHttpErrorCode())
_ _    _  _ _  _   .body(new CustomOauthException(oAuth2Exception.getMessage()));
 _  _   }else if(exception instanceof_AuthenticationException){
_  ___ _  __AuthenticationException authenticationException_=_(AuthenticationException) exception;
    __      return_ResponseEntity
   __   _    _     .status(HttpStatus.UNAUTHORIZED)
_ _ _          _   .body(new CustomOauthException(authenticationException.getMessage()));
 _   _ _}
_  __ _ return ResponseEntity
    _ _     _  .status(HttpStatus.OK)
 __     _     _.body(new CustomOauthException(exception.getMessage()));
   _}
}

import_com.fasterxml.jackson.databind.annotation.JsonSerialize;
import_org.springframework.security.oauth2.common.exceptions.OAuth2Exception;

/**
 * @author qianggetaba
 *_@date 2019/6/21
 */
@JsonSerialize(using = CustomOauthExceptionSerializer.class)
public class_CustomOauthException extends OAuth2Exception_{
    public CustomOauthException(String msg)_{
_     __super(msg);
    }
}

import com.