-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom JWT error messages for authentication failures #42
Add custom JWT error messages for authentication failures #42
Conversation
Here's the code health analysis summary for commits Analysis Summary
|
Hello @nmarulo, please check this PR also I have some queries:
Also please let me know if everything's alright. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR 😄.
When I finish reviewing it, I may comment on something else.
src/main/java/dev/nmarulo/depensaapp/security/JwtAuthenticationEntryPoint.java
Show resolved
Hide resolved
If this is correct, I want to be able to test the application without logging in or registering.
Yes, the idea is that if you want a private list, you will have to register and log in.
No, I just want if the token fails to return a custom message in the response body. |
Also I forgot to mention to be able to test if the authentication handling is correct or not we can customize the @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.securityMatcher(appProperties.getPathPrefix() + "/**")
.authorizeHttpRequests(authorize -> {
authorize.requestMatchers(appProperties.getPathPrefix() + "/auth/**")
.permitAll()
.anyRequest()
.authenticated();
})
.httpBasic(AbstractHttpConfigurer::disable)
.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.oauth2ResourceServer(configOAuth2())
.cors(value -> value.configurationSource(corsConfigurationSource()));
return http.build();
} This way we can get the bearer token after logging in then access all the other endpoints with the valid bearer token. If the bearer token is invalid (manipulation) or expired, the response body should output a custom error message with all the necessary details accordingly. Thanks! |
src/main/java/dev/nmarulo/depensaapp/security/JwtAuthenticationEntryPoint.java
Show resolved
Hide resolved
src/main/java/dev/nmarulo/depensaapp/security/JwtAuthenticationEntryPoint.java
Outdated
Show resolved
Hide resolved
src/main/java/dev/nmarulo/depensaapp/security/JwtAuthenticationEntryPoint.java
Outdated
Show resolved
Hide resolved
src/main/java/dev/nmarulo/depensaapp/security/JwtAuthenticationEntryPoint.java
Show resolved
Hide resolved
db74f3d
to
eef1393
Compare
eef1393
to
23b6b26
Compare
@JinnJarBurger Thanks 👍 |
Anytime! 😃 |
Fixes #22
Description
A custom
AuthenticationEntryPoint
has been implemented namedJwtAuthenticationEntryPoint
which intercepts any JWT Exceptions from theSecurityFilterChain
and sets up theProblemDetails
accordingly wrapped inside theErrorRes
Object . The response content type has been set to "application/json" for increased readability.Screenshots