我正在使用Spring Boot安全性来处理登录功能。我不知道登录的post方法根本无法工作。它将始终引发以下异常。
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
有人可以建议吗?这是我的代码。您也可以从last
中找到它。
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>
答案 0 :(得分:0)
您的登录功能仅接受GET请求。您应该将GET更改为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;
}
}
答案 1 :(得分:0)
您应该这样更改Homecontroller登录方法:
@RequestMapping(value = { "/", "/login" }, method = RequestMethod.POST)
public ModelAndView login(Model model) {
或者您也可以使用:
@PostMapping( value = { "/", "/login" })