403 error when connecting to "localhost:8080" or "localhost:8080/" in the following code.
It works well for other URL.
Only not work root URL.
I thought I could access localhost:8080 without authentication.
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final UserService userService;
private final PasswordEncoder passwordEncoder;
@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authprovider = new DaoAuthenticationProvider();
authprovider.setUserDetailsService(userService.userDetailsService());
authprovider.setPasswordEncoder(passwordEncoder);
return authprovider;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/").permitAll()
.anyRequest().authenticated()
)
.authenticationProvider(authenticationProvider())
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
Please double check if Postman is not adding a token. Go to Auth tab, and make sure it says "No Auth".
Also, do you have a controller for root("/")? If not, try to create one. Most likely, these two steps will resolve your problem.