请问您的配置有什么问题吗?我无法使用正确的凭据登录,它总是将我重定向回登录页面(代码 302)。当输入http://localhost:8080时- Spring Security 重定向到/login,表单提交后重定向回/login。
protected void configure (AuthenticationManagerBuilder builder) throws Exception {
builder.inMemoryAuthentication().withUser("user").password("{noop}12345").roles("USER");
}
protected void configure (HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll().defaultSuccessUrl("/start.html").loginProcessingUrl("/authenticateMe");
@GetMapping("/login")
public String showLogin () {
return "index";
}
@PostMapping("/authenticateMe")
public String login () {
return "start";
}
<body>
<form method="post" th:action="@{/authenticateMe}" class="login">
<p>
<label th:for="username">Username: </label>
<input type="text" th:name="username" id="username"/>
</p>
<p>
<label th:for="password">Password: </label>
<input type="password" th:name="password" id="password"/>
</p>
最佳答案
您正在覆盖处理登录尝试的 URL。您的代码
@PostMapping("/authenticateMe")
public String login () {
return "start";
}
正在响应登录请求,但实际上并不对登录信息进行任何处理(例如,在 SecurityContext 上设置 Authentication 对象)。您已经配置了身份验证管理器,因此请从 Controller 中删除代码,这样您的身份验证就可以正确处理。
关于java - 无法登录 Spring Security - 总是将我重定向回登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62133534/