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

Refactor guest routes #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -9,6 +9,7 @@
import java.beans.Transient;
import java.io.IOException;
import java.security.Security;
import java.util.Arrays;

nekitbr marked this conversation as resolved.
Show resolved Hide resolved
import jakarta.transaction.TransactionScoped;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -36,17 +37,19 @@ protected void doFilterInternal(
@NonNull HttpServletResponse response,
@NonNull FilterChain filterChain
) throws ServletException, IOException {
if (request.getServletPath().contains("/api/v1/auth")) {
filterChain.doFilter(request, response);
return;
}
final String authHeader = request.getHeader("Authorization");
final String jwt;
final String userEmail;
if (authHeader == null ||!authHeader.startsWith("Bearer ")) {

if(
Arrays.asList(SecurityConfiguration.whiteListedRoutes).contains(request.getServletPath()) ||
nekitbr marked this conversation as resolved.
Show resolved Hide resolved
authHeader == null ||
!authHeader.startsWith("Bearer ")
) {
filterChain.doFilter(request, response);
return;
}

jwt = authHeader.substring(7);
userEmail = jwtService.extractUsername(jwt);
if (userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alibou.security.config;

import jakarta.servlet.Filter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -21,14 +20,15 @@ public class SecurityConfiguration {
private final JwtAuthenticationFilter jwtAuthFilter;
private final AuthenticationProvider authenticationProvider;
private final LogoutHandler logoutHandler;
public static final String[] whiteListedRoutes = new String[]{"/api/v1/auth/**"};

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeHttpRequests()
.requestMatchers("/api/v1/auth/**")
.requestMatchers(whiteListedRoutes)
.permitAll()
.anyRequest()
.authenticated()
Expand Down