Когда я пытаюсь войти в систему, я получаю следующую ошибку:
Код: Выделить всё
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' is not supported]Вот конфигурация безопасности :
Код: Выделить всё
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfiguration {
@Autowired
private LoginSuccessHandler loginSuccessHandler;
@Autowired
private LoginFailureHandler loginFailureHandler;
@Bean
public UserDetailsService userDetailsService() {
return new MyUserDetailsService();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder());
provider.setUserDetailsService(userDetailsService());
return new ProviderManager(provider);
}
@Bean
@Order(1)
public SecurityFilterChain adminFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.securityMatcher(new AntPathRequestMatcher("/admin/**"))
.authorizeHttpRequests(auth -> auth
.anyRequest()
.hasAuthority("ADMIN")
)
.formLogin(form -> form
.loginPage("/login")
.failureHandler(loginFailureHandler)
.successHandler(loginSuccessHandler)
.permitAll()
)
.logout(logout -> logout
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
);
return http.build();
}
@Bean
public SecurityFilterChain defaultFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(def -> def
.requestMatchers(new AntPathRequestMatcher("/**"))
.permitAll()
);
return http.build();
}
Код: Выделить всё
Log In
Код: Выделить всё
@GetMapping(value="login")
public ModelAndView displayLoginPage() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
Код: Выделить всё
@Component
public class LoginFailureHandler extends SimpleUrlAuthenticationFailureHandler {
@Autowired
private UserRepository userRepository;
@Autowired
private LoginAttemptService loginAttemptService;
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
String email = request.getParameter("email");
User user = userRepository.findByEmail(email);
String role = request.getParameter("role");
if (user != null) {
if (user.isEnabled()) {
if (user.getFailedAttempts() < LoginAttemptService.MAX_FAILED_ATTEMPTS - 1) {
loginAttemptService.increaseFailedAttempts(user);
} else {
loginAttemptService.lock(user);
exception = new LockedException("Your account has been locked due to " + LoginAttemptService.MAX_FAILED_ATTEMPTS + " failed attempts." + " It will be unlocked after 24 hours.");
}
} else if (!user.isEnabled()) {
if (loginAttemptService.unlockWhenTimeExpired(user)) {
exception = new LockedException("Your account has been unlocked. Please try to login again.");
}
}
}
if (role.equals("USER")){
super.setDefaultFailureUrl("/login?errorUser");
} else {
super.setDefaultFailureUrl("/login?errorAdmin");
}
super.onAuthenticationFailure(request, response, exception);
}
}
Код: Выделить всё
@Component
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Autowired
private LoginAttemptService loginAttemptService;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
User user = userDetails.getUser();
if (user.getFailedAttempts() > 0) {
loginAttemptService.resetFailedAttempts(user.getEmail());
}
String redirectURL = request.getContextPath();
String submittedRole = request.getParameter("role");
if (submittedRole.equals("ADMIN") && userDetails.hasRole("ADMIN")) {
redirectURL = "/admin/home";
} else if (submittedRole.equals("ADMIN") && !userDetails.hasRole("ADMIN")) {
redirectURL = "/login?wrongrole";
}
if (!user.isEnabled() && user.getLockTime() != null) {
redirectURL = "/login?disabled";
}
response.sendRedirect(redirectURL);
}
}
Метод «POST» не поддерживается в Spring Security 6
а также документ о конфигурации Spring Java для нескольких конфигураций:
https://docs.spring.io/spring-security/ ... tpsecurity
Однако мне не удалось заставить мою часть входа работать должным образом.
Подробнее здесь: https://stackoverflow.com/questions/787 ... form-login