简体   繁体   English

Spring Security自定义登录错误

[英]Spring Security Custom Login Error

I have been trying to add Spring Security Custom Login(Java Config) into my application, but a strange error keeps popping up. 我一直在尝试将Spring Security Custom Login(Java Config)添加到我的应用程序中,但是一个奇怪的错误不断出现。

I have gone through the Creating a Custom Login Form tutorial and it works just fine. 我已经完成了创建自定义登录表单教程 ,它工作得很好。 I am not sure what is the issue with my application. 我不确定我的申请是什么问题。

Error 错误

Caused by: java.lang.IllegalArgumentException: 'login?error' is not a valid redirect URL 引起:java.lang.IllegalArgumentException:'login?error'不是有效的重定向URL

Security Config 安全配置

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

LoginController 的LoginController

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

Just commenting the method configure(HttpSecurity http) removes the exception. 只需注释方法configure(HttpSecurity http)可以删除异常。 I tried adding RequestMapping with value login?error , but it didn't help. 我尝试添加带有值login?error RequestMapping login?error ,但它没有帮助。 What am I missing here? 我在这里错过了什么?

Link to complete stack trace 链接到完整的堆栈跟踪

Your are missing a slash. 你错过了一个斜线。

This: 这个:

.loginPage("login")

Should be: 应该:

 .loginPage("/login")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM