You will be fine

<Spring Security> 0. 스프링 시큐리티란

by BFine
반응형

https://docs.spring.io/spring-security/site/docs/current/reference/html5/

 

Spring Security Reference

In Spring Security 3.0, the codebase was sub-divided into separate jars which more clearly separate different functionality areas and third-party dependencies. If you use Maven to build your project, these are the modules you should add to your pom.xml. Ev

docs.spring.io

가.  무엇인가

 a.  서버 보안?!

  -  스프링 시큐리티는 인증(authentication)인가(authorization), 공격에 대한 보호(protection)를 지원한다.

  -  여러 Servlet 필터를 chain 형태로 연결하여 요청을 인터셉트하여 처리를 구조로 되어있다.

 

 b.  특징

  -  Java 8+ 에서 사용가능하다.

  -  개발자가 직접 인증, 인가를 정책 파일을 만들어 관리할 필요가 없이 self-contained 방식으로 처리해준다.

 

 c.  기타

  -  레포지토리

       =>  https://github.com/spring-projects/spring-security/

  -  예제

       => https://github.com/spring-projects/spring-security/tree/5.4.x/samples

 

나. 인증 (Authentication)

 a.  무엇인가?!

  -  인증은 서버에 접근하려 할때 Identity를 검증하는 것이다. (ex. 로그인시 ID,PW 검증)

  -  스프링 시큐리티는 이러한 인증에 대한 부분들 처리할 수 있도록 서포트하는 역할을 한다.

 

 b.  Password Storage

  -  PasswordEncoder 인터페이스는 비밀번호를 특정알고리즘을 이용하여 안전하게 암호화해서 저장한다. 

      => {암호화방식}인코딩비밀번호 형태로 저장된다. 

  private PasswordEncoder passwordEncoder;

    @BeforeEach
    void setUp(){
        passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        // bcrypt로 암호화 한다. 
    }

    @Test
    void encode(){
        final String password = "1234";
        final String encode = passwordEncoder.encode(password);
        System.out.println(encode);
    }
    
 
 #### 결과
 {bcrypt}$2a$10$4uQgqIvwHEdJr6a25TMzj.ry6lirOUKq0PxN7G36y.pRh8LD6hb8e

 

다. 인가 (Authorization)

 a.  무엇인가?!

  -  인가는 어떤 특정한 리소스 접근 할수있는 유저에 대한 권한을 설정하는 것이다. (ex. ADMIN )

 

 b.  예시

  -  권한을 부여하여 이후 모든 요청에 대해서 인증 처리시에 접근을 제한 할 수 있다.

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password("1234").roles("USER")
                .and()
                .withUser("admin").password("1235").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/my").hasRole("USER")
                .antMatchers("/setting").hasRole("ADMIN")
                .anyRequest()
                .authenticated();
    }
}

 

다. 보호(Protection)

 a. CSRF

  -  Cross Site Request Forgery (사이트간 요청 위조)

  -  공격자가 공격코드를 만들어 사용자의 이미 인증된 정보를 이용해 타깃서버로 http 요청을 보내는 공격방법  

  -  Spring Security는 CSRF 토큰을 통해 CSRF 공격에 대해서 보호가 가능하다.

      1. 요청마다 서버는 CSRF 토큰을 발행하여 Front로 보낸다.

      2. Front는 해당 토큰을 모든 요청에 추가하여 보내야한다.

      3. 토큰이 일치하지 않는 경우에 요청은 실패된다.

  

라. Spring Security Auto Configuration

 a.  Dependency 추가만 해도 자동설정?! (Spring Boot)

  -  Spring Boot 구동시 Servlet 필터인 SpringSecurityFilterChain 빈을 생성한다.

      => SpringSecurityFilterChain은 모든 보안을 담당하는 역할을 한다. 

  -  UserDetailsService 빈을 생성해주며 디폴트로 아이디는 user고 비밀번호는 로딩시 랜덤으로 생성한다.

  -  SpringSecurityFilterChain 에 Security 관련 필터들을 등록한다.

반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기