HTTP Basic 인증은 가장 간단한 형태의 HTTP 인증입니다. 사용자 이름과 비밀번호는 Base64 인코딩된 형태로 헤더에 포함되어 전송됩니다.
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("{noop}password").roles("USER");
}
}
폼을 통해 사용자 이름과 비밀번호를 입력 받아 인증을 진행합니다. Spring Security는 이를 위한 로그인 폼을 기본적으로 제공하지만 커스텀 폼도 쉽게 적용할 수 있습니다.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
OAuth2는 외부 서비스(예: Google, Facebook 등)를 통해 인증을 진행하는 프로토콜입니다. Spring Security는 이를 쉽게 적용할 수 있도록 여러 확장을 제공합니다.
@Configuration
@EnableOAuth2Sso
public class OAuth2Config extends WebSecurityConfigurerAdapter {
// OAuth2 설정
}
JWT는 자체적으로 정보를 담은 토큰을 사용해 인증을 진행합니다. 이 방식은 Stateless한 구조로 API를 구성할 때 유용하게 사용됩니다.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
}
Authentication
: 인증 요청과 인증된 주체를 나타내는 인터페이스입니다.AuthenticationManager
: 실제로 인증을 처리하는 인터페이스입니다.UserDetailsService
: 사용자의 세부 정보를 로드하는 방법을 정의합니다.PasswordEncoder
: 비밀번호의 암호화 또는 해싱을 처리합니다.