弹簧安全自定义登录错误

2022-09-02 09:04:54

我一直在尝试将Spring Security Custom Login(Java Config)添加到我的应用程序中,但是一个奇怪的错误不断弹出。

我已经完成了创建自定义登录表单教程,它工作得很好。我不确定我的应用程序有什么问题。

错误

由以下原因引起:java.lang.IllegalArgumentException: 'login?error' 不是有效的重定向 URL

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    public void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/", "/about").permitAll();
        http
        .authorizeRequests()
        .antMatchers("/admin","/admin/**").hasRole("ADMIN")
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("login")
        .permitAll();
    }
}

登录控制器

@Controller
public class LoginController {

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String loginView() {
        return "login";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(@ModelAttribute Login login) {
        return "home";
    }
}

只需注释该方法即可删除异常。我试图增加价值,但它没有帮助。我在这里错过了什么?configure(HttpSecurity http)RequestMappinglogin?error

链接到完整的堆栈跟踪


答案 1

您缺少斜杠。

这:

.loginPage("login")

应该是:

 .loginPage("/login")

答案 2

推荐