-
Notifications
You must be signed in to change notification settings - Fork 59
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
Showing
2 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
.../br/com/zupacademy/fabiano/mercadolivre/config/validation/AutenticacaoViaTokenFilter.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,55 @@ | ||
package br.com.zupacademy.fabiano.mercadolivre.config.validation; | ||
|
||
import br.com.zupacademy.fabiano.mercadolivre.authentication.TokenService; | ||
import br.com.zupacademy.fabiano.mercadolivre.modelo.Usuario; | ||
import br.com.zupacademy.fabiano.mercadolivre.repository.UsuarioRepository; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class AutenticacaoViaTokenFilter extends OncePerRequestFilter { | ||
|
||
private TokenService tokenService; | ||
|
||
private UsuarioRepository usuarioRepository; | ||
|
||
public AutenticacaoViaTokenFilter(TokenService tokenService, UsuarioRepository usuarioRepository) { | ||
this.tokenService = tokenService; | ||
this.usuarioRepository = usuarioRepository; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
String token = recuperarToken(request); | ||
boolean valido = tokenService.isTokenValido(token); | ||
if(valido) { | ||
autenticarCliente(token); | ||
} | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private void autenticarCliente(String token) { | ||
Long idUsuario = tokenService.getIdUsuario(token); | ||
Usuario usuario = usuarioRepository.findById(idUsuario).get(); | ||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(usuario, null, usuario.getAuthorities()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
|
||
private String recuperarToken(HttpServletRequest request) { | ||
String token = request.getHeader("Authorization"); | ||
if(token == null || token.isEmpty() || !token.startsWith("Bearer ")) { | ||
return null; | ||
} | ||
return token.substring(7, token.length()); | ||
} | ||
|
||
} |