Spring Security 기반 세션 인증

moon.kick·2025년 4월 16일

그렇지! 👍
"로그인 + 로그아웃 기능"은 어떤 인증 방식이든 기본 중의 기본,
특히 Spring Security 기반 세션 인증은 배우기 가장 좋은 첫걸음이야.
지금부터 완전 핵심 흐름이랑 코드 예시까지 쫙 보여줄게!


🔐 세션 기반 로그인 + 로그아웃 흐름 (Spring Security 기준)

💡 핵심 흐름 요약

[1] 로그인 요청 (POST /login)
    ↓
[2] Spring Security가 ID/PW 인증
    ↓
[3] 인증 성공 시 → 세션 생성 + 인증 객체 저장
    ↓
[4] 이후 요청마다 → 세션 쿠키로 인증 유지
    ↓
[5] 로그아웃 요청 (POST /logout)
    ↓
[6] 세션 무효화 + 쿠키 삭제

⚙️ Spring Boot 기반 기본 설정 예시

1. 📦 build.gradle 의존성

implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'

2. 🧱 SecurityConfig (세션 방식 기본 제공)

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable() // 개발단계에서만 비활성화
            .authorizeRequests()
                .antMatchers("/login", "/").permitAll()
                .anyRequest().authenticated()
            .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/home", true)
            .and()
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login?logout")
                .invalidateHttpSession(true)  // 세션 무효화
                .deleteCookies("JSESSIONID"); // 쿠키 삭제
    }
}

3. 👩‍💻 UserDetailsService 구현

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 실제 DB에서 유저 찾기 생략
        if (!username.equals("test")) {
            throw new UsernameNotFoundException("User not found");
        }

        return User.builder()
                .username("test")
                .password("{noop}1234") // 비밀번호 암호화 안 함 (연습용)
                .roles("USER")
                .build();
    }
}

4. 🖥️ Controller 예시

@Controller
public class HomeController {

    @GetMapping("/")
    public String index() {
        return "index"; // 로그인 페이지
    }

    @GetMapping("/home")
    public String home() {
        return "home"; // 로그인 성공 시 이동
    }

    @GetMapping("/login")
    public String loginPage() {
        return "login"; // 커스텀 로그인 폼
    }
}

5. 🖼️ login.html (Thymeleaf 템플릿 예시)

<form th:action="@{/login}" method="post">
  <input type="text" name="username" placeholder="아이디">
  <input type="password" name="password" placeholder="비밀번호">
  <button type="submit">로그인</button>
</form>

6. ✅ 로그아웃 처리 흐름

<form th:action="@{/logout}" method="post">
  <button type="submit">로그아웃</button>
</form>

또는 버튼 하나로 처리하고 싶다면 자바스크립트로 POST 요청을 보내면 돼.


📦 요약정리: 세션 로그인 시스템

기능구현 방식
✅ 로그인Spring Security + formLogin
✅ 인증 상태 유지세션 (서버 메모리 or Redis 등)
✅ 로그아웃POST /logout 요청 + 세션 삭제

🙋‍♀️ 그래서, 로그인/로그아웃은 꼭 들어가야 돼?

👉 YES!

  • 기초지만 필수 기능이라 거의 모든 프로젝트에서 요구됨
  • 나중에 JWT든 OAuth2든 넘어가도 인증 흐름의 뼈대가 같음
  • 이걸 익혀두면 Spring Security 커스터마이징, DB 연동 로그인, 소셜 로그인, 토큰 인증 다 이해하기 쉬워져

다음 단계로:

  • ✅ DB 기반 회원가입/로그인
  • ✅ Redis 세션 저장 방식
  • ✅ 로그인 실패 메시지 처리
  • ✅ 로그인 상태 확인 (세션에서 사용자 이름 가져오기)

이런 것도 해볼 수 있어!
지금 이 코드로 실습해보고 싶어? 아니면 DB연동 버전으로 확장해볼까? 🛠️

profile
@mgkick

0개의 댓글