无法在 Spring Boot Security 中登录到我的自定义登录页面

Posted

技术标签:

【中文标题】无法在 Spring Boot Security 中登录到我的自定义登录页面【英文标题】:Can't login to my custom login page in spring boot security 【发布时间】:2015-04-25 09:29:19 【问题描述】:

我的问题是:当我尝试在我的自定义登录页面上登录时,它会再次将我重定向到登录页面(我输入正确或错误的凭据都没有关系)。我在调试中没有达到我的自定义 UserDetailService 也很奇怪,我认为这意味着 spring 甚至不检查用户的凭据。

我有自定义登录表单/WEB-INF/jsp/login.jsp:

...    
<form name='loginForm' action="<c:url value='j_spring_security_check' />" method='POST'>

        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='j_username' value=''></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='j_password' /></td>
            </tr>
            <tr>
                <td colspan='2'><input name="submit" type="submit"
                                       value="submit" /></td>
            </tr>
        </table>

        <input type="hidden" name="$_csrf.parameterName"
               value="$_csrf.token" />

    </form>
...

这是配置:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        http
                .authorizeRequests().antMatchers("/", "/welcome", "/resources/**").permitAll()
                .anyRequest().authenticated().and()
                .formLogin().loginPage("/login").failureUrl("/login?error").permitAll().and()
                .logout().logoutUrl("/login?logout").permitAll();
    

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception 
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    

    @Bean
    public PasswordEncoder passwordEncoder() 
        return new BCryptPasswordEncoder();
    

这是在控制器内部:

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
        @RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout) 

    ModelAndView model = new ModelAndView();
    if (error != null) 
        model.addObject("error", "Invalid username and password!");
    

    if (logout != null) 
        model.addObject("msg", "You've been logged out successfully.");
    
    model.setViewName("login");

    return model;


src/main/resources/application.properties 我有:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

【问题讨论】:

【参考方案1】:

我通过在 HttpSecurity 配置中禁用 CSRF 解决了类似的问题:

.csrf().disable() 

如果您想启用 CSRF,则必须在所有表单中包含令牌

<input type="hidden" name="$_csrf.parameterName" value="$_csrf.token"/>

详解:http://www.baeldung.com/spring-security-csrf

【讨论】:

【参考方案2】:

那些j_spring_security_checkj_usernamej_password 名称都是旧的默认值(Spring Security 3.2 之前)。

现在的默认值为:loginusernamepassword

【讨论】:

以上是关于无法在 Spring Boot Security 中登录到我的自定义登录页面的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot + Security + JWT 无法生成令牌

Spring Boot + Spring Security解决UsernameNotFoundException无法被捕获的问题

如何解决在使用 Spring Boot 和 Spring Security 启用 CSRF 后无法正常工作的登录问题?

基于 Spring Boot / Spring Security 角色的授权无法正常工作 [重复]

使用 KeycloakAutoConfiguration 无法使用 Spring Boot Security UnsatisfiedDependencyException 设置 Keycloak

无法在 Spring Boot Security 中登录到我的自定义登录页面