Skip to content

Commit

Permalink
Changed time of Token JWT and Changed secret of JWT
Browse files Browse the repository at this point in the history
- Changed the duration of time of JWT Token
- Changed the Secret of JWT
  • Loading branch information
LauroSilveira committed Feb 8, 2024
1 parent 492061c commit a4757ad
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

@Configuration
//Enable Custom configuration spring boot
@EnableWebSecurity
//enable @Secure("Role_XX")
@EnableMethodSecurity(securedEnabled = true)
Expand All @@ -31,9 +32,9 @@ public class SecurityConfigurations {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//disable cross site request forgery
//disable Cross Site Request Forgery
return http.csrf(csrf -> csrf.ignoringRequestMatchers("/login/**") )
//Disable Spring control and allow all endpoints
//Configure to be stateless
.sessionManagement(managementConfigurer ->
managementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(httpRequest -> httpRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected void doFilterInternal(final HttpServletRequest request,
SecurityContextHolder.getContext().setAuthentication(authentication);
log.info("{} User authenticated: {}", PREFIX_LOGGING, authentication.getPrincipal());
}
//continue the flow
filterChain.doFilter(request, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public String getSubject(String tokenJWT) {
}

}
//Create expire date of token, in this case is the current hour plus 2 hours
//Create expire date of token, in this case is the current hour plus 10 minutes
private Instant getExpireDate() {
return LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.UTC);
return LocalDateTime.now().plusMinutes(10).toInstant(ZoneOffset.of("Europe/Madrid"));

Check failure on line 64 in src/main/java/com/alura/aluraflixapi/infraestructure/security/TokenService.java

View workflow job for this annotation

GitHub Actions / Test Reporter Summary

com.alura.aluraflixapi.infraestructure.security.TokenServiceTest ► getSubject_test

Failed test found in: target/surefire-reports/TEST-com.alura.aluraflixapi.infraestructure.security.TokenServiceTest.xml Error: java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: Europe/Madrid
Raw output
java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: Europe/Madrid
	at java.base/java.time.ZoneOffset.of(ZoneOffset.java:243)
	at com.alura.aluraflixapi.infraestructure.security.TokenService.getExpireDate(TokenService.java:64)
	at com.alura.aluraflixapi.infraestructure.security.TokenService.generateTokenJWT(TokenService.java:41)
	at com.alura.aluraflixapi.infraestructure.security.TokenService.generateTokenJWT(TokenService.java:29)
	at com.alura.aluraflixapi.infraestructure.security.TokenServiceTest.getSubject_test(TokenServiceTest.java:70)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Check failure on line 64 in src/main/java/com/alura/aluraflixapi/infraestructure/security/TokenService.java

View workflow job for this annotation

GitHub Actions / Test Reporter Summary

com.alura.aluraflixapi.infraestructure.security.TokenServiceTest ► generateTokenJWT_test

Failed test found in: target/surefire-reports/TEST-com.alura.aluraflixapi.infraestructure.security.TokenServiceTest.xml Error: java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: Europe/Madrid
Raw output
java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: Europe/Madrid
	at java.base/java.time.ZoneOffset.of(ZoneOffset.java:243)
	at com.alura.aluraflixapi.infraestructure.security.TokenService.getExpireDate(TokenService.java:64)
	at com.alura.aluraflixapi.infraestructure.security.TokenService.generateTokenJWT(TokenService.java:41)
	at com.alura.aluraflixapi.infraestructure.security.TokenService.generateTokenJWT(TokenService.java:29)
	at com.alura.aluraflixapi.infraestructure.security.TokenServiceTest.generateTokenJWT_test(TokenServiceTest.java:41)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Set;
Expand All @@ -18,6 +19,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
class TokenServiceTest {

@SpyBean
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ server:
port: 8080

spring:
profiles:
active: "test"
application:
name: alura-flix-api
data:
Expand Down

0 comments on commit a4757ad

Please sign in to comment.