Spring security при запуске сайта все равно используется базовая конфигурация, хотя есть пользовательская
Код взят с гайда по spring security. Страницы "/", "/home", "/login" существуют и описаны в контроллере. По идее, страницы "/", "/home" должны быть доступны без авторизации, но они все равно защищены. Добавляла @Order(1) к моей конфигурации, но это не помогло, также пыталась отключать csrf. Все равно при запуске сайта высвечивается лог: INFO 22436 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with
Файл CustomSecurityConfig:
package org.example.securingweb;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.security.config.Customizer.withDefaults;
@Configuration
@EnableWebSecurity
public class CustomSecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.permitAll()
)
.logout((logout) -> logout.permitAll());
return http.build();
}
}
Файл MvcConfig:
package org.example.securingweb;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
Ответы (1 шт):
Автор решения: talex
→ Ссылка
Нужно разрешить анонимный доступ
.requestMatchers("/", "/home").anonymous()