Web Framework/Spring Boot
Spring Security - configure method override
Linzyseo
2019. 7. 3. 17:31
Spring security 에서 Login process 가 어떤 방식으로 이루어지는지에 대한 설정을 살펴보자.
1. 가장 먼저 Controller에서 url이 mapping된 부분을 살펴보자.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SimpleController {
@GetMapping("/usr/login")
public String login() {
return "/login";
}
}
/usr/login 으로 GET방식의 요청이 들어올 경우 WEB-INF/jsp/login.jsp 를 return 한다.
2. login.jsp에서 form 형태를 살펴보자.
<form action="/loginProcess" method="POST">
<fieldset>
<section>
<label><input type="text" name="username" placeHolder="ID"></label>
</section>
<section>
<label><input type="password" name="password" placeHolder="비밀번호"></label>
</section>
</fieldset>
</form>
form action을 "/loginProcess"로 준 것에 주목하자.
3. 위의 2번에서 설정한 form Login 설정은 아래의 Spring Security 관련 설정 파일에서 설정되어지고 있다.
Spring Seurity 에서 WebSecurityConfigureAdapter class를 상속받아 오버라이드된 Configure 함수는 아래와 같다.
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 WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/usr/login").permitAll();
http.formLogin()
.loginProcessingUrl("/loginProcess")
.loginPage("/usr/login")
.defaultSuccessUrl("/list");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
}
}