Здесь после применения Spring Security я настроил некоторые из URL для доступности всех пользователей. Например, «Пользователь/» должен получить доступ к пользователям, а «Admin/» должен быть доступен администратором. Проблема возникает здесь. После попытки получить доступ, я успешно перенаправляюсь на страницу входа в систему, но после успешного входа пользователя я не могу перейти к «пользователю/индексу», почему ??.
Я вижу этот URL: http: // localhost: 8080/user/index? Продолжить
Код: Выделить всё
package com.smart.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@Configuration
public class MyConfig {
@Bean
public UserDetailsServiceimpl getUserDetails() {
return new UserDetailsServiceimpl();
}
@Bean
public BCryptPasswordEncoder bcryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(this.getUserDetails());
daoAuthenticationProvider.setPasswordEncoder(this.bcryptPasswordEncoder());
return daoAuthenticationProvider;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
// Only authenticated users can access the following paths
.requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/user/**").hasRole("USER")
// All other paths are public (no authentication needed)
.requestMatchers( "/**").permitAll()
)
.formLogin() // Default Spring Boot login page
.permitAll() // Allow everyone to access the login page
.and()
.logout(logout -> logout
.permitAll()) // Allow everyone to log out
.csrf().disable(); // Disable CSRF if you're not using sessions
return http.build();
}
}
введите здесь описание изображения
Подробнее здесь: https://stackoverflow.com/questions/793 ... ng-securit