Spring启动身份验证问题 - 始终重定向到/ login

时间:2018-05-20 10:08:26

标签: java spring spring-boot spring-security

我遇到了Spring Security的麻烦。我想从spring覆盖内置的/login端点,以便能够加载位于resources/templates/mylogin.html下的mylogin.html页面。我认为这个问题来自下面的代码。此外,该课程使用@EnableWebSecurity进行注释。

我项目中的安全bean配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    //The pages does not require login
    http.authorizeRequests().antMatchers("/","/welcome","logout").permitAll();
    // /userInfo page requires login as ROLE_ADMIN, ROLE_DOCTOR, ROLE_PATIENT
    http.authorizeRequests().antMatchers("/userInfo").access("hasAnyRole('ROLE_ADMIN','ROLE_DOCTOR','ROLE_PATIENT')");
    //For ADMIN only
    http.authorizeRequests().antMatchers("/admin").access("hasRole('ROLE_ADMIN')");
    http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/403");
    //Config for Login Form
    http.authorizeRequests().anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/mylogin.html")
            .permitAll(true)
            .and()
            .logout()
            .logoutSuccessUrl("/mylogin.html?logout")
            .permitAll();code here
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我建议你使用spring-boot-starter-thymeleaf库。你可以做到

.loginPage("/login")

然后使用Spring Controller,您可以在方法中捕获/ login请求。在此方法中,返回“mylogin”会将用户重定向到mylogin.html页面。

此处示例:https://github.com/cihangir-mercan/spring-boot/

相关问题