gpt4 book ai didi

java - Spring Boot安全请求方法 'POST'不支持

转载 作者:太空宇宙 更新时间:2023-11-04 10:14:57 25 4
gpt4 key购买 nike

我正在使用 Spring Boot Security 来处理登录功能。我不知道以某种方式登录的 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

@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;
}
}

登录.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;
}
}

关于java - Spring Boot安全请求方法 'POST'不支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51835908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com