기본 콘텐츠로 건너뛰기

라벨이 Spring Security인 게시물 표시

Spring Security 설정 완벽 가이드 — web.xml + security-context.xml 예제 총정리

Spring Security 설정 완벽 가이드 — web.xml + security-context.xml 예제 총정리 Spring Security 설정 완벽 가이드 — web.xml + security-context.xml 예제 총정리 Spring Security는 Java 웹 애플리케이션에서 인증(Authentication)과 인가(Authorization)을 처리하는 강력한 프레임워크입니다. 이번 포스팅에서는 web.xml 의 필터 등록부터 security-context.xml 의 구체적인 설정까지, 예제 코드와 함께 하나씩 살펴보겠습니다. 1️⃣ web.xml에 Spring Security 필터 등록 Spring Security를 사용하려면, 먼저 DelegatingFilterProxy 를 web.xml 에 등록해야 합니다. 이 필터는 Spring 컨테이너에서 관리되는 springSecurityFilterChain 을 서블릿 필터 체인에 연결하는 역할을 합니다. <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 위 설정을 통해 모든 요청( /* )이 Spring Security 필터 체인을 거치게 됩니다. 2️⃣ security-context.xml 설정 다음은 Spring Security의 주요 설정을 담은 s...

[Spring Security] SecurityContext로 강제 로그인 처리하기 (SSO·백오피스 연동 팁)

[Spring Security] SecurityContext로 강제로 로그인 처리하기 (SSO·백오피스 연동) [Spring Security] SecurityContext로 강제로 로그인 처리하기 (SSO·백오피스 연동) 내부 시스템이나 SSO 연동 , 또는 이미 별도 인증 과정을 거친 뒤 Spring Security 세션만 강제로 만들어야 하는 상황 이 종종 있습니다. 이 글에서는 SecurityContext 와 UsernamePasswordAuthenticationToken 을 사용해 프로그램적으로 로그인(강제 로그인) 처리 하는 방법을 예제와 함께 정리합니다. 1. 왜 강제 로그인(프로그램 로그인)이 필요할까? 대표적인 사용 시나리오는 다음과 같습니다. 사내 SSO 서버에서 이미 인증을 완료했고, 결과로 내려온 ID로만 Spring Security 컨텍스트를 세팅 하고 싶을 때 별도 로그인 페이지 없이, 백오피스·관리자 화면에서 바로 내부 계정으로 진입 시켜야 할 때 테스트·운영 도구 등에서 특정 계정으로 자동 로그인 이 필요할 때 ⚠️ 단, 이런 강제 로그인은 보안적으로 매우 주의 해야 합니다. 외부에서 이 로직에 접근할 수 없도록 IP 제한, 별도 권한 체크, 토큰 검증 등을 반드시 거쳐야 합니다. 2. 기존 강제 로그인 코드 이해하기 질문에서 주신 예제 코드를 조금 다듬어서 먼저 살펴보겠습니다. 2-1. 예제 코드 SecurityContext securityContext = SecurityContextHolder.getContext(); UsernamePasswordAuthenticationToken result = null; List<Gra...