Skip to content

Commit

Permalink
Adicionando autenticacao de token
Browse files Browse the repository at this point in the history
  • Loading branch information
RaianNolaco committed Nov 26, 2023
1 parent 149aa08 commit d865688
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
<artifactId>java-jwt</artifactId>
<version>4.2.1</version>
</dependency>

</dependencies>

<build>
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/pi/procurarteapi/app/auth/config/Configurations.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class Configurations {

@Autowired
//@Autowired
//private

@Autowired
private FilterToken filter;

//Faz a liberação das paginas que podem ser acessadas com ou sem autenticação
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http)throws Exception{
Expand All @@ -30,9 +34,19 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http)throws Exceptio
.permitAll()
.antMatchers(HttpMethod.GET, "/musician")
.permitAll()
.antMatchers(HttpMethod.GET, "/musician/{id}")
.permitAll()
.antMatchers(HttpMethod.POST, "/musician")
.permitAll()
.anyRequest().authenticated().and().build();
.antMatchers(HttpMethod.GET, "/musicstyle")
.permitAll()
.antMatchers(HttpMethod.GET, "/instrument")
.permitAll()
.anyRequest().authenticated()
.and().addFilterBefore(filter,UsernamePasswordAuthenticationFilter.class)
.build();


}

@Bean
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/pi/procurarteapi/app/auth/config/FilterToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pi.procurarteapi.app.auth.config;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import pi.procurarteapi.app.auth.services.TokenService;
import pi.procurarteapi.infra.repositories.IMusicianRepository;

@Component
public class FilterToken extends OncePerRequestFilter{

@Autowired
private TokenService tokenService;

@Autowired
private IMusicianRepository musicianRepository;

@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

String token;
var authorizationHeader = request.getHeader("Authorization");

if(authorizationHeader != null){
token = authorizationHeader.replace("Bearer ", "");
var subject = tokenService.getSubject(token);

var musician = musicianRepository.findByEmail(subject);

var authentication = new UsernamePasswordAuthenticationToken(musician,null, musician.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
}

filterChain.doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ public String gererToken(Musician musician) {
.withSubject(musician.getUsername())
.withClaim("id", musician.getId())
.withExpiresAt(LocalDateTime.now()
.plusMinutes(10)
.plusMinutes(30)
//.plusSeconds(30)
.toInstant(ZoneOffset.of("-03:00"))
).sign(Algorithm.HMAC256("secreta"));
}

public String getSubject(String token){
return JWT.require(Algorithm.HMAC256("secreta"))
.withIssuer("Musico").build().verify(token).getSubject();
}

}

0 comments on commit d865688

Please sign in to comment.