Back End/Spring && Spring Boot

[Spring Security] 세션 관련 설정하기 - 세션 만료 시간, 최대 세션 갯수, 세션 고정 설정

Im_Hayden 2024. 6. 4. 14:59

이번에는 스프링 시큐리티를 통해 세션 관련 설정하는 방법을 알아보고자 한다.
스프링 시큐리티를 통해 간편하게 설정할 수 있다.
하나씩 살펴보자.

✨ 세션 만료시간 설정하기

application.properties

# 세션 만료 시간 설정
# 초 단위 설정: 단위X ex) timeout=1800     1800초
# 분 단위 설정: m ex) timeout=90m          90분
sever.servlet.session.timeout=90m

 

application.properties 파일에 위와 같은 코드를 작성함으로써 세션 만료 시간을 설정할 수 있다.

 

🎈 세션 만료 시간

  • 사용자가 로그인시 발급받은 세션에 대한 지속 시간
  • 사용자의 마지막 동작부터 만료 시간이 카운팅 됨

 


✨ 동시 세션 최대 갯수 설정하기

SecurityConfig.java

package com.example.jwt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity  // Spring Security 인가 활성화
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{

        // 다중 로그인 설정
        /**
         * sessionManagement(): 세션에 관련된 설정
         * maximumSessions(): 최대 동시 접속 가능 세션 수
         * maxSessionsPreventsLogin(): 최대 세션 수를 초과했을때의 동작
         *      true: 초과시 새로운 로그인 차단
         *      false: 초과시 이전 세션 정보 삭제
         */
        http.
                sessionManagement((auth)->{
                   auth.maximumSessions(1)
                           .maxSessionsPreventsLogin(true);
                });

        return http.build();
    }
}

 

여러 사용자가 동시에 동일한 유저정보에 대하여 요청을 했을 때, 최대로 발급 가능한 세션 갯수를 설정하는 코드이다.
Configuration클래스에 위의 FilterChain 클래스를 등록하면 된다.

  • auth.maximumSession(Integer): 동시에 발급 가능한 세션 갯수를 설정한다.
  • maxSessionsPreventsLogin(Boolean): 최대 세션 발급 갯수를 초과하는 요청이 들어왔을 때 동작 방식을 설정한다.
    • true: 새로운 로그인 요청을 차단
    • false: 새로운 로그인 요청을 수락하고, 기존의 세션 정보를 삭제

✨ 세션 고정 설정

만약 사용자(Client)가 로그인 이전에 발급받은 세션과, 로그인 이후에 발급받은 세션이 동일하다면 세션 고정 약점 문제가 발생할 수 있다.

🎈 세션 고정 약점
사용자의 로그인 전/후의 세션이 동일할 때, 외부의 사용자가 해당 세션을 악용하여 허용되지 않은 권한에 접근하는 문제
예시는 아래와 같다.

  1. 공격자는 웹 사이트에 접근하여 세션 아이디를 발급 받음
  2. 공격자는 발급받은 세션 아이디를 피해자(admin)의 쿠키에 심음(ex. 쿠키가 심어지는 링크가 포함된 메일 전송 등)
  3. 피해자는 공격자와 동일한 세션 정보로 웹 사이트에 로그인 및 admin 권한 취득, 이때 피해자의 세션 정보는 로그인 전과 동일
  4. 공격자피해자는 동일한 세션 정보를 가지고 있음, 또한 해당 세션은 피해자의 로그인 요청으로 인해 admin권한을 가지고 있음
  5. 이제 공격자는 해당 세션 정보로 admin 권한에 접근 가능

 

이러한 세션 고정 약점 문제 해결을 위해 사용자의 로그인 후에 세션을 새로 발급해주는 방법이 있다.
Spring Security에서는 이러한 설정을 간단하게 진행할 수 있다.

 

SecurityConfig.java

package com.example.jwt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity  // Spring Security 인가 활성화
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{

        // 세션 고정 공격 방어
        /**
         * none(): 로그인시 세션 정보 변경 안함(세션 고정 약점에 취약)
         * newSession(): 로그인시 새로운 세션 생성
         * changeSessionId(): 로그인시 동일한 세션에 대한 아이디 변경(주로 사용)
         */
        http.sessionManagement((auth)->{
            auth.sessionFixation().changeSessionId();
        });

        return http.build();
    }
}

관련 메소드는 아래와 같다.

  • sessionFixation().none(): 세션 정보를 변경하지 않음
    • 세션 고정 약점에 취약
  • sessionFixation().newSession(): 사용자가 로그인시, 새로운 세션을 생성하여 사용자에게 부여
  • sessionFixation().changeSessionId(): 사용자가 로그인시, 세션ID 값만 변경. 이로 인해 공격자의 세션 정보는 무용지물
    • 현재 주로 사용되고 있는 방법

 

Reference