-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7352bf0
commit 0874909
Showing
4 changed files
with
70 additions
and
4 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/com/jwtauthcruddemo/configs/JWTAuthFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.jwtauthcruddemo.configs; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/*** | ||
* Classe de configuração HTTP para interceptar | ||
* as solicitações recebidas e validar o JWT | ||
*/ | ||
public class JWTAuthFilter extends OncePerRequestFilter { | ||
|
||
private final LoginAuthProvider loginAuthProvider; | ||
|
||
public JWTAuthFilter(LoginAuthProvider loginAuthProvider) { | ||
this.loginAuthProvider = loginAuthProvider; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, | ||
HttpServletResponse response, | ||
FilterChain filterChain) throws ServletException, IOException { | ||
|
||
// Verificar se existe um cabeçalho de autorização | ||
String header = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
|
||
// Portador | ||
if (Objects.nonNull(header)) validateAndSetAuthentication(header); | ||
|
||
// Continue a cadeia de filtros | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private void validateAndSetAuthentication(String header) { | ||
String[] authElements = header.split(" "); | ||
if (authElements.length == 2 && "Bearer".equals(authElements[0])) { | ||
try { | ||
SecurityContextHolder.getContext().setAuthentication(loginAuthProvider.validateToken(authElements[1])); | ||
} catch (RuntimeException ex) { | ||
SecurityContextHolder.clearContext(); | ||
throw ex; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters