Answer the question
In order to leave comments, you need to log in
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("/");
}
}
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);
}
}
<!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>
Answer the question
In order to leave comments, you need to log in
Try after .loginProcessingUrl("/login")
adding:
.usernameParameter("email")
.passwordParameter("password")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question