Spring Security — Не работает POST метод?

Приветствую уважаемые форумчане.

Подскажите пожалуйста, почему не работает POST Method? Не могу выловить параметры пришедшие через POST. Ни каких ошибок не выдаёт, только возвращает null

SecurityConfig.class

package com.myfilter.security;
 
import com.myfilter.filter.CustomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
 
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 
    // Подключаю свой CustomFilter где буду вылавливать POST параметры
    @Autowired
    CustomFilter customFilter;
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
 
        http.
 
                // Добавляю фильтр где буду вылавливать POST параметры
                addFilterBefore(customFilter, AnonymousAuthenticationFilter.class)
 
                .authorizeRequests()
                .mvcMatchers("/login", "/").permitAll()
 
                // Указываю что будет работать метод POST
                .mvcMatchers(HttpMethod.POST,"/login").permitAll()
 
 
                .and()
                .csrf().disable()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/");
 
    }
 
}


CustomFilter.class (Это фильтр где буду вылавливать POST параметры)

package com.myfilter.filter;
 
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;
 
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
@Component
public class CustomFilter extends GenericFilterBean {
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
 
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        
        // Получаю параметр
        String email = httpServletRequest.getParameter("email");
 
        // Просто вывожу в консоль
        System.out.println(email);
        
        chain.doFilter(request, response);
    }
}


login.html

<!DOCTYPE html>
<html lang="en">
<head>
 
    <meta charset="utf-8">
    <title>Sign in</title>
 
</head>
<body>
 
    <form name="user" method="post" action="/login">
 
            <input type="text" name="email">
            <input type="password" name="password">
        
        <button type="submit">Sign in</button>
        
    </form>
    
</body>
</html>


В консоль получаю ответ null
  • Вопрос задан
  • 653 просмотра
Решения вопроса 1
azerphoenix
@azerphoenix Куратор тега Spring
Java Software Engineer
Попробуйте после .loginProcessingUrl("/login") добавить:
.usernameParameter("email")
.passwordParameter("password")

https://www.websparrow.org/spring/spring-boot-secu...
По дефолту прибегает Spring Security ожидает username & password, а у вас email & password
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы