SPRING SEQURITY - Form Login 인증 API

2021. 4. 15. 00:23SPRING/SPRING SECURITY

사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package io.security.basicsecurity;
 
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest()
                .authenticated();
        http
                .formLogin()
                .loginPage("/loginPage")
                .defaultSuccessUrl("/aa")
                .failureUrl("/vv")
                .usernameParameter("userId")
                .passwordParameter("pwd")
                .loginProcessingUrl("/login_proc")
                .successHandler((httpServletRequest, httpServletResponse, authentication) -> {
                    System.out.println("authentication: " + authentication.getName());
                    httpServletResponse.sendRedirect("/");
                })
                .failureHandler((httpServletRequest, httpServletResponse, exception) -> {
                    System.out.println("exception: " + exception.getMessage());
                    httpServletResponse.sendRedirect("/login");
                })
                .permitAll();
    }
}
 
cs
API 설명
.formLogin() Form 태그를 통한 로그인을 활성화하며 Form관련 API를 사용할 수 있음.
.loginPage("/loginPage") 사용자 정의 로그인 페이지를 정의.
.defaultSuccessUrl("/aa") 로그인 성공시 디폴트로 사용할 URL을 정의.
.failureUrl("/vv") 로그인 실패시 사용할 URL을 정의.
.usernameParameter("userId") Form으로 넘어오는 파라미터 중 username과 매핑되는 파라미터 이름을 세팅.
.passwordParameter("pwd") Form으로 넘어오는 파라미터 중 password와 매핑되는 파라미터 이름을 세팅.
.loginProcessingUrl("/login_proc") 로그인 Form Action URL 세팅. 실제 로그인 로직 URL
.successHandler() 로그인 성공시 수행할 핸들러 정의.
.failureHandler() 로그인 실패시 수행할 핸들러 정의
.permitAll() 인증 필요없이 모든 사용자에게 로그인 페이지 접근을 허용함.