Spring boot - POST method not allowed
So the problem was that one of the parameter was null.
It has been solved adding required=null at the request parameter annotation, like that:
@RequestParam(value = "yourParamName", required = false)
this cause a 405, as defined here:
6.5.5. 405 Method Not Allowed
The 405 (Method Not Allowed) status code indicates that the method
received in the request-line is known by the origin server but not
supported by the target resource. The origin server MUST generate an
Allow header field in a 405 response containing a list of the target
resource's currently supported methods.
A 405 response is cacheable by default; i.e., unless otherwise
indicated by the method definition or explicit cache controls (see
Section 4.2.2 of [RFC7234]).
when the "target resource" are defined here:
Spring Boot - Post Method Not Allowed, but GET works
The method post will be not Allowed if you have csrf configured or enabled
then your need to provided a valid csrf while posting your form or data
Check your spring security config for this
For example
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = CustomUserDetailsService.class)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.....
RequestMatcher csrfRequestMatcher = new RequestMatcher() {
// Enabled CSFR protection on the following urls:
//@formatter:off
private AntPathRequestMatcher[] requestMatchers =
{
new AntPathRequestMatcher("/**/verify"),
new AntPathRequestMatcher("/**/login*")
};
//@formatter:off
@Override
public boolean matches(final HttpServletRequest request) {
// If the request match one url the CSFR protection will be enabled
for (final AntPathRequestMatcher rm : requestMatchers) {
if (rm.matches(request)) {
System.out.println();
/* return true; */
}
}
return false;
} // method matches
};
@Override
protected void configure(final HttpSecurity http) throws Exception {
//@formatter:off
http.headers().frameOptions().sameOrigin()
.and()
.authorizeRequests()
.antMatchers("/","/css/**", "/static/**", "/view/**", "**/error/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/mvc/login").permitAll()
.authenticationDetailsSource(authenticationDetailsSource())
.successHandler(authenticationSuccessHandler)
.usernameParameter("username").passwordParameter("password")
.and()
.logout().permitAll()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.addLogoutHandler(customLogoutHandler)
.logoutSuccessHandler(customLogoutSuccessHandler)
.logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling()
.accessDeniedPage("/403")
.and()
.csrf()/* .requireCsrfProtectionMatcher(csrfRequestMatcher) */
.ignoringAntMatchers("/crud/**","/view/**")
;
// @formatter:off
}
Thanks
Spring Boot 405 POST method not supported but GET method works
I've finally got it working with the POST method. I just added a "redirect:" to the return parameters of the controller to redirect to the required views and of course changed to the POST methods for the calling form and the controller. Here is my modified code below:
//login
@RequestMapping(value= "/login", method= RequestMethod.POST)
public String login(@RequestParam("txtUserName") String usrName, @RequestParam("txtPassword") String password) {
System.out.println("LOGIN UserName : " + usrName + " Password : " + password);
//Process the admin password - hardcoded
if(usrName.equals("admin") && password.equals("admin123"))
{ return "redirect:main.html"; }
else
{ return "redirect:invalid_login.html"; }
}
Why does not the post method work when the Get method works in RestController?
Why you are trying to use both (POST, GET) Methods for the same functionality.
using @RequestBody with a GET method is not a good practice.
Have you added spring security dependency and configuration?
If yes, then adding method name may help you as below.
.antMatchers(HttpMethod.POST,"/yourRequestURL").permitAll()
Related Topics
How to Make Sonar Ignore Some Classes for Codecoverage Metric
How to Run Multiple Jobs in Spring Batch Using Annotations
Chrome Browser Is Not Opening in Selenium Webdriver
How to Replace Every N-Th Character X from an String
How to Import Two Classes With the Same Name in Different Packages
Mockito - Nullpointerexception When Stubbing Method
Reading a Resource File from Within Jar
How to Get the Position of a Element in a Queue
Regex to Validate 3 Repeating Characters
Spring Rest Post Json Requestbody Content Type Not Supported
Split a List into Sublists Based on a Condition With Stream API
How to Download a File from S3 Using Provided Url
In Java: How to Zip File from Byte[] Array
Can We Use Multipart and @Requestbody Together in Spring
Calculate the Number of Items Displayed by Recyclerview and Place in a Textview