Springboot

[spring boot 3.1.4]spring security formLogin에서 로그인 안될때

siwoli 2023. 10. 10. 15:10

usernameParameter( input태그의 name속성 )

passwordParameter( input태그의 name속성 )
를 제대로 넣어줬는지 확인하자


spring boot 3.1.4는 spring security 6.1.4를 지원하는데, SecurityFilterChain에 deprecated 메서드들이 많아서 SecurityConfig를 수정했다.

@RequiredArgsConstructor
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
    @Bean
    public WebSecurityCustomizer configure() {
        return (web -> web.ignoring()
                .requestMatchers(toH2Console()));
    }

    //특정 HTTP요청에 대한 웹 기반 보안 구성
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(authorize -> {
                            authorize
                                    .requestMatchers(new AntPathRequestMatcher("/login")).permitAll()
                                    .requestMatchers(new AntPathRequestMatcher("/signup")).permitAll()
                                    .requestMatchers(new AntPathRequestMatcher("/user")).permitAll()
                                    .requestMatchers(new AntPathRequestMatcher("/static/**")).permitAll()
                                    .anyRequest().authenticated();
                        }
                )
                .formLogin(formLogin -> {
                            formLogin
                                    .loginPage("/login")
                                    .usernameParameter("email")
                                    .passwordParameter("password")
                                    .defaultSuccessUrl("/articles");
                        }
                )
                .logout(logout ->
                        logout
                                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                                .logoutSuccessUrl("/login")
                )
                .build();
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

회원가입을 하면 로그인 페이지로 넘어간다.

로그인을 하면 목록페이지로 넘어가야하는데 login?error가 떴다.

나름 테스트 코드도 작성해서 로그인 성공&실패를 테스트해봤지만 모두 통과했다.

그래서 thymeleaf를 살펴보던 중

<form th:action="@{/login}" method="POST">
    ...
    <input type="email" name="username" id="inputEmail">
    ...
    <input type="password" name="password" id="inputPassword">
    ...
</form>

이메일 input태그의 name속성은 "username"인데

비밀번호 input태그의 name속성은 "password"였다.

그래서 SecurityConfig의 usernameParameter의 인자를 "username"으로 수정했더니

...
.formLogin(formLogin -> {
                            formLogin
                                    .loginPage("/login")
                                    .usernameParameter("username")
                                    .passwordParameter("password")
                                    .defaultSuccessUrl("/articles", true);
                        }
                )
...

로그인에 성공했다.

name 속성은 폼(form)이 제출된 후 서버에서 폼 데이터(form data)를 참조하기 위해 사용되거나, 자바스크립트에서 요소를 참조하기 위해 사용됩니다. - tcpschool.com

input태그를 쓸때는 항상 name속성을 잘 지정하자