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

Issue with SecurityFilterChain - PermitAll not working as desired #1

Open
nopaperwork opened this issue Aug 16, 2023 · 0 comments
Open

Comments

@nopaperwork
Copy link

Good Morning. I am sharing a combination of checks which are working and which are not. Ideally, your code, as per the video should work for Spring Boot 3.1.2

PS: I am using Spring Cloud Config

NOT WORKING
(1) With requestMatcher(UN_SECURED_URLs).permitAll()
(2) With direct URL/ indirect URL
(3) With WebSecurityCustomizer
@bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
// .requestMatchers(UN_SECURED_URLs).permitAll()
// .requestMatchers(SECURED_URLs).authenticated()
.anyRequest().authenticated()) // permitAll())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authenticationProvider)
.addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter.class)
.logout(logout -> logout
.logoutUrl("/security/api/v1/logout").addLogoutHandler(logoutHandler)
.logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext())
.logoutSuccessUrl("http://nopaper.work"))
.build();
}

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
    return (web) -> web.ignoring().requestMatchers(UN_SECURED_URLs);
}

WORKING:
(1) With anyRequest - Permit All
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
// .requestMatchers(UN_SECURED_URLs).permitAll()
// .requestMatchers(SECURED_URLs).authenticated()
.anyRequest().permitAll()) // WORKING
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authenticationProvider)
.addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter.class)
.logout(logout -> logout
.logoutUrl("/security/api/v1/logout").addLogoutHandler(logoutHandler)
.logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext())
.logoutSuccessUrl("http://nopaper.work"))
.build();
}

(2) With requestMatchers("/**") - Permit All
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
// .requestMatchers(UN_SECURED_URLs).permitAll()
// .requestMatchers(SECURED_URLs).authenticated()
.anyRequest().permitAll()) // WORKING
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authenticationProvider)
.addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter.class)
.logout(logout -> logout
.logoutUrl("/security/api/v1/logout").addLogoutHandler(logoutHandler)
.logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext())
.logoutSuccessUrl("http://nopaper.work"))
.build();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant