java - Handling JWT Exception in Spring MVC -


i trying implement token authentication on our rest api, , referring article. on article discusses on creating token jwt used, current problem every time invalid token being passed on application exception being created jwtexception.class , want catch exception using global exception handler class. tried wrapped jwtexception on application's exception class no avail exception not caught.

@controlleradvice public class globalexceptionhandler {  @exceptionhandler(value={jwtexception.class}) public responseentity<?> handletokenexception(jwtexception e){     return new responseentity<object>(httpstatus.unauthorized); }  @exceptionhandler(value={invalidauthtokenexception.class}) public responseentity<?> handletokenexception(invalidauthtokenexception e){     return new responseentity<object>(httpstatus.unauthorized);     } } 

your globalexceptionhandler isn't global, catch exceptions occur in controller (hence controlleradvice), exceptions running occurring in servlet filters, spring security pretty of work. little chart may explain talking about:

prefilters <- executed before entering controller, decryption of jwt happening here

controller <- controlleradvice catch exceptions thrown here

postfilters <- executed after exiting controller

luckily spring security has mechanisms in place handling exceptions occur when doing things decrypting jwt in filter. want update springsecurityconfig so. note important exceptiontranslationfilter after statelessauthenticationfilter (or whatever named filter jwt decryption occurring).

    @configuration     @enablewebsecurity     @order(2)     public class springsecurityconfig extends websecurityconfigureradapter {          @override         protected void configure(httpsecurity http) throws exception {             exceptiontranslationfilter = new exceptiontranslationfilter(new authenticationexceptionhandler());              http.addfilterafter(new statelessauthenticationfilter(tokenauthenticationservice),                             exceptiontranslationfilter.class);         }       }      public class authenticationexceptionhandler implements authenticationentrypoint {         public void commence(httpservletrequest request, httpservletresponse, authenticationexception e) throws ioexception, servletexception {             //logic on how handle jwt exception goes here         }     }      public class statelessauthenticationfilter extends genericfilterbean {         @override         public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {             try {                 //decrypt jwt             } catch (exception e) {                  throw new authenticationexception();//if exception wrap in authenticationexception (or class extends it)             }         }     } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -