E
E
Evgeny Krasnov2020-09-26 17:33:02
Spring
Evgeny Krasnov, 2020-09-26 17:33:02

Spring Security - POST method not working?

Greetings dear forum users.

Can you please tell me why the POST Method does not work ? I can not catch the parameters that came through POST. It does not give any errors, it only returns 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 (This is the filter where I will catch POST parameters)

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>


I get a null response in the console

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-09-26
@Favorskij

Try after .loginProcessingUrl("/login") adding:

.usernameParameter("email")
.passwordParameter("password")

https://www.websparrow.org/spring/spring-boot-secu...
By default, Spring Security expects username & password, and you have email & password

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question