Для контекста я хочу /зарегистрировать и / /вход в систему, чтобы быть видимым без аутентификации, в то время как все остальные страницы в моем проекте должны перенаправить пользователя, если не подтверждено на страницу /вход.
Код: Выделить всё
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(authz -> authz
// Public access (no authentication needed)
.requestMatchers(
new AntPathRequestMatcher("/register"),
new AntPathRequestMatcher("/login"),
new AntPathRequestMatcher("/api/auth/**"),
new AntPathRequestMatcher("/css/**"),
new AntPathRequestMatcher("/js/**"),
new AntPathRequestMatcher("/images/**")
).permitAll()
// Everything else requires authentication
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard", true) // Redirect to dashboard after login
.permitAll()
)
.logout(logout -> logout
.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.permitAll()
);
return http.build();
< /code>
ps- удаление < /p>
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard", true) // Redirect to dashboard after login
.permitAll()
)
.logout(logout -> logout
.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.permitAll()
)
Подробнее здесь: https://stackoverflow.com/questions/797 ... y-redirect