
스프링 시큐리티에 대해 간단하게 개념 정리를 하고 단순 시큐리티를 적용해봅니다.
이외에 여러 기능들이 존재합니다.
시큐리티 사용을 앞서 인증(Authentication)과 인가(Authorization)이라는 개념을 알아야 합니다.
인증은 '증명하다'라는 의미로 예를 들어, 유저 아이디와 비밀번호를 이용하여 로그인 하는 과정 을 말합니다.
인가는 '권한부여'나 '허가'와 같은 의미로 사용됩니다. 즉, 어떤 대상이 특정 목적을 실현하도록 허용(Access) 하는 것을 의미합니다.
Web에서 인증은 해당 URL은 보안 절차를 거친 사용자들만 접근할 수 있다는 의미이고,
인가란 URL에 접근한 사용자가 특정한 자격이 있다는 것을 의미합니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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;
import org.springframework.security.web.header.writers.StaticHeadersWriter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import javax.sql.DataSource;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/guest/**").permitAll()
.antMatchers("/manager/**").hasRole("MANAGER")
.antMatchers("/admin/**").hasRole("ADMIN");
http.formLogin();
http.exceptionHandling().accessDeniedPage("/accessDenied");
http.logout().logoutUrl("/logout").invalidateHttpSession(true);
}
로그인을 위해서는 SecurityConfig 클래스에 AuthenticationManagerBuilder를 주입해서 인증에 대한 처리를 해야 합니다. AuthenticationManagerBuilder는 인증에 대한 다양한 설정을 생성할 수 있습니다. 예를 들어, 메모리상 정보를 이용하거나, JDBC, LDAP등의 정보를 이용해서 인증 처리가 가능합니다. 다음 예제는 메모리를 이용하는 예제입니다.
참고로 AuthenticationManagerBuilder는 인증 매니저를 생성하는 빌더입니다.
위의 SecurityConfig.java 클래스에 아래 메소드를 추가합니다.
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
log.info("build Auth global.......");
auth.inMemoryAuthentication()
.withUser("manager")
.password("{noop}1111")
.roles("MANAGER");
}
그러면 아이디는 manager 패스워드는 1111로 로그인이 가능합니다.