Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add croos origin config #84

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.List;

/**
* Main Spring Security class configuration In Spring 3.0 the security configuration is done by
Expand All @@ -32,15 +38,15 @@ public class SecurityConfigurations {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//disabel cross site request forgery
//disable cross site request forgery
return http.csrf(AbstractHttpConfigurer::disable)
//Disable Spring controll and expone all endpoints
//Disable Spring control and allow all endpoints
.sessionManagement(managementConfigurer ->
managementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(httpRequest -> httpRequest
.requestMatchers(HttpMethod.POST, "/login").permitAll()
.requestMatchers("/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**").permitAll()
//any other requeste has to be authenticated
//any other request has to be authenticated
.anyRequest().authenticated()
)
//tell to spring to user our filter SecurityFilter.class instead their
Expand All @@ -49,6 +55,21 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
}


/**
* Configure Cross
* @return CorsConfigurationSource
*/
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("https://alura-flix-api-production.up.railway.app"));
configuration.setAllowedMethods(Arrays.asList("GET","POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}


/**
* Crete @Bean AuthenticationManager to authenticate a user
* @return AuthenticationManager
Expand All @@ -61,7 +82,7 @@ public AuthenticationManager authenticationManager(
}

/**
* Bean to encript and decript password
* Bean to encrypt and decrypt password
* @return new PasswordEncoder
*/
@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Component
public class SecurityFilter extends OncePerRequestFilter {

private static final String PREFIX_LOGGIN = "[SecurityFilter]";
private static final String PREFIX_LOGGING = "[SecurityFilter]";
public static final String AUTHORIZATION = "Authorization";
private final TokenService tokenService;

Expand All @@ -38,7 +38,7 @@ protected void doFilterInternal(final HttpServletRequest request,
final HttpServletResponse response,
final FilterChain filterChain) throws ServletException, IOException {

log.info("{} request received intercepeted by Internal Filter", PREFIX_LOGGIN);
log.info("{} request received intercepted by Internal Filter", PREFIX_LOGGING);
final var tokenJWT = this.getTokenJWT(request);

if (tokenJWT != null) {
Expand All @@ -48,11 +48,11 @@ protected void doFilterInternal(final HttpServletRequest request,

//after retrieve the user we need to tell to Spring framework to authenticate him in the context
//this is done by calling UsernamePasswordAuthenticationToken and SecurityContextHolder methods
log.info("{} Authenticating user: {} ", PREFIX_LOGGIN, user.getUsername());
log.info("{} Authenticating user: {} ", PREFIX_LOGGING, user.getUsername());
var authentication = new UsernamePasswordAuthenticationToken(user, null,
user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
log.info("{} User authenticated: {}", PREFIX_LOGGIN, authentication.getPrincipal());
log.info("{} User authenticated: {}", PREFIX_LOGGING, authentication.getPrincipal());
}
filterChain.doFilter(request, response);
}
Expand Down