Skip to content

Commit

Permalink
feat: autenticaçãovia token
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomsrs committed Jun 30, 2021
1 parent 62551d0 commit f35ce5a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package br.com.zupacademy.fabiano.mercadolivre.config.security;

import br.com.zupacademy.fabiano.mercadolivre.authentication.AutenticacaoService;
import br.com.zupacademy.fabiano.mercadolivre.authentication.TokenService;
import br.com.zupacademy.fabiano.mercadolivre.config.validation.AutenticacaoViaTokenFilter;
import br.com.zupacademy.fabiano.mercadolivre.repository.UsuarioRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@EnableWebSecurity
@Configuration
public class Security extends WebSecurityConfigurerAdapter {
@Autowired
private AutenticacaoService autenticacaoService;

@Autowired
private TokenService tokenService;

@Autowired
private UsuarioRepository usuarioRepository;

@Override
@Bean
protected AuthenticationManager authenticationManager() throws Exception {
Expand All @@ -32,8 +44,11 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**").permitAll()
.and().csrf().disable();
.antMatchers(HttpMethod.POST,"/usuarios").permitAll()
.antMatchers(HttpMethod.POST,"/auth").permitAll()
.anyRequest().authenticated().and().csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().addFilterBefore(new AutenticacaoViaTokenFilter(tokenService, usuarioRepository), UsernamePasswordAuthenticationFilter.class);
}

@Override
Expand Down
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());
}

}

0 comments on commit f35ce5a

Please sign in to comment.