Reputation: 11
I have API for blogging application. Working fine When i send request for login it give me token like below
eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoibmFtZSIsImVtYWlsIjoiaG9zdDIyQGdtYWlsLmNvbSIsInVzZXJfaWQiOjEsInN1YiI6Imhvc3QyMkBnbWFpbC5jb20iLCJpYXQiOjE2Njg0NDc5NzEsImV4cCI6MTY2ODQ2NTk3MX0.rhSK6nxgnoMg-7Urdvw2v-4Mk9U75eYxeK_FrgJgDr4
But when I send token by changing first latter of token i.e. 'e' with 'f' i should be getting error like " "Unable to read JSON value"
I am getting exception in console - log but issue is i should be getting it in response. io.jsonwebtoken.MalformedJwtException: Unable to read JSON value: "alg":"HS256"}
i tried like by creating class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint but it is not good idea as i am still getting exception in console log
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("---------------JwtAuthenticationEntryPoint----------------------");
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
OutputStream responseStream = response.getOutputStream();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(responseStream, "Unable to read JSON value");
responseStream.flush();
//response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, authException.getStackTrace());
}
}
i belive i have to handel exception but don't know how The condition is exception should be handel as well as
To genrate error
Console logs are as
io.jsonwebtoken.MalformedJwtException: Unable to read JSON value: "alg":"HS256"}
at io.jsonwebtoken.impl.DefaultJwtParser.readValue(DefaultJwtParser.java:554) \~\[jjwt-0.9.1.jar:0.9.1\]
at io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:252) \~\[jjwt-0.9.1.jar:0.9.1\]
at io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:481) \~\[jjwt-0.9.1.jar:0.9.1\]
at io.jsonwebtoken.impl.DefaultJwtParser.parseClaimsJws(DefaultJwtParser.java:541) \~\[jjwt-0.9.1.jar:0.9.1\]
at com.app.jwtApp.config.JwtTokenUtil.getAllClaimsFromToken(JwtTokenUtil.java:43) \~\[classes/:na\]
at com.app.jwtApp.config.JwtTokenUtil.getClaimFromToken(JwtTokenUtil.java:37) \~\[classes/:na\]
at com.app.jwtApp.config.JwtTokenUtil.getUsernameFromToken(JwtTokenUtil.java:28) \~\[classes/:na\]
at com.app.jwtApp.config.JwtRequestFilter.doFilterInternal(JwtRequestFilter.java:49) \~\[classes/:na\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:112) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:82) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.session.DisableEncodeUrlFilter.doFilterInternal(DisableEncodeUrlFilter.java:42) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:346) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:221) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:186) \~\[spring-security-web-5.7.4.jar:5.7.4\]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:354) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:267) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) \~\[spring-web-5.3.23.jar:5.3.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) \~\[tomcat-embed-core-9.0.68.jar:9.0.68\]
at java.base/java.lang.Thread.run(Thread.java:833) \~\[na:na\]
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ((CTRL-CHAR, code 127)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at \[Source: (String)"\\u007F"alg":"HS256"}"; line: 1, column: 2\]
at com.fasterxml.jackson.core.JsonParser.\_constructError(JsonParser.java:2391) \~\[jackson-core-2.13.4.jar:2.13.4\]
at com.fasterxml.jackson.core.base.ParserMinimalBase.\_reportError(ParserMinimalBase.java:735) \~\[jackson-core-2.13.4.jar:2.13.4\]
at com.fasterxml.jackson.core.base.ParserMinimalBase.\_reportUnexpectedChar(ParserMinimalBase.java:659) \~\[jackson-core-2.13.4.jar:2.13.4\]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.\_handleOddValue(ReaderBasedJsonParser.java:2005) \~\[jackson-core-2.13.4.jar:2.13.4\]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:802) \~\[jackson-core-2.13.4.jar:2.13.4\]
at com.fasterxml.jackson.databind.ObjectMapper.\_initForReading(ObjectMapper.java:4761) \~\[jackson-databind-2.13.4.2.jar:2.13.4.2\]
at com.fasterxml.jackson.databind.ObjectMapper.\_readMapAndClose(ObjectMapper.java:4667) \~\[jackson-databind-2.13.4.2.jar:2.13.4.2\]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629) \~\[jackson-databind-2.13.4.2.jar:2.13.4.2\]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3597) \~\[jackson-databind-2.13.4.2.jar:2.13.4.2\]
at io.jsonwebtoken.impl.DefaultJwtParser.readValue(DefaultJwtParser.java:552) \~\[jjwt-0.9.1.jar:0.9.1\]
... 59 common frames omitted
I have tried by using @ExceptionHandler but not able to capture exception and handle it
Expectation is it should send response in JSON when try to authorize user "data":"Unable to read JSON value"; As well as Exception should be handled i.e. should be in Console log
Upvotes: 1
Views: 879