I am using spring boot security to handle login function. I don't know somehow the post method for login can't work at all. It will always throw the following exception.
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
Can somebody suggest? Here is my code. You can find it from GitHub as well.
SecurityConfiguration
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
}
HomeController
@Controller
public class HomeController {
@Autowired
private UserService userService;
@RequestMapping(value = { "/", "/login" }, method = RequestMethod.GET)
public ModelAndView login(Model model) {
ModelAndView modelAndView = new ModelAndView("auth/login");
User user = new User();
modelAndView.addObject(user);
return modelAndView;
}
}
login.html
<form class="form-horizontal" role="form" th:action="@{login}" method="post">
<div class="form-group">
<label class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input type="text" class="form-control" name="username" th:placeholder="Username"/>
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password" th:placeholder="Password"/>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-sign-in"></i> Login
</button>
</div>
</div>
<div align="center" th:if="${param.error}">
<p style="font-size: 20; color: #FF1C19;">Email or Password invalid, please verify</p>
</div>
</form>
Your Login function only accept GET requests. You should change GET to POST
@RestController
public class HomeController {
@Autowired
private UserService userService;
@RequestMapping(value = { "/", "/login" }, method = RequestMethod.POST)
public ModelAndView login(Model model) {
ModelAndView modelAndView = new ModelAndView("auth/login");
User user = new User();
modelAndView.addObject(user);
return modelAndView;
}
}
You should change your Homecontroller login method like this:
@RequestMapping(value = { "/", "/login" }, method = RequestMethod.POST)
public ModelAndView login(Model model) {
or you could also use:
@PostMapping( value = { "/", "/login" })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.