![](/img/trans.png)
[英]Spring security 4.0.1 Request method 'POST' not supported
[英]Spring Boot Security Request method 'POST' not supported
我正在使用Spring Boot安全性來處理登錄功能。 我不知道登錄的post方法根本無法工作。 它將始終拋出以下異常。
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
有人可以建議嗎? 這是我的代碼。 您也可以從GitHub找到它。
安全配置
@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());
}
}
家庭控制器
@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>
您的登錄功能僅接受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;
}
}
您應該像這樣更改您的Homecontroller登錄方法:
@RequestMapping(value = { "/", "/login" }, method = RequestMethod.POST)
public ModelAndView login(Model model) {
或者您也可以使用:
@PostMapping( value = { "/", "/login" })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.