Java Spring Security, Session & Csrf Attack

떡ol·2023년 5월 16일
0

Spring Security는 Session-Fixation 과 CSRF Attack을 방어할 수 있는 기능을 제공하고 있습니다.

Session-Fixation Attack

세션을 이용한 공격 방법입니다. 사용자의 인증받은 세션 토큰값을 그대로 가져와 사용함으로써 동일한 인증상태를 유지하게 됩니다. 사용자에게 토큰을 헤더로 둔 링크를 보내는 식으로 공격이 발생합니다.

자바에서는 이러한 공격을 방어하기 위해 인증시 세션값을 변경할 수 있습니다.

protected void configure(HttpSecurity http) throws Exception {
	http.sessionManagement()
                .sessionFixation().changeSessionId() // 기본값
							      //.none, migrateSession, newSession
}

추가로 사용할 수 있는 SessionPolicy가 있습니다. Session 존재 유무에따라 컨드롤이 가능하게 합니다.

  • SessionCreationPolicy. Always : 스프링 시큐리티가 항상 세션 생성
  • SessionCreationPolicy. If_Required : 스프링 시큐리티가 필요 시 생성(기본값)
  • SessionCreationPolicy. Never : 스프링 시큐리티가 생성하지 않지만 이미 존재하면 사용
  • SessionCreationPolicy. Stateless : 스프링 시큐리티가 생성하지 않고 존재해도 사용하지 않음
protected void configure(HttpSecurity http) throws Exception {
	http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy. If_Required ) //기본값
}

Csrf Attack

인증된 사용자에게 해당 사이트에 관련된 요청을 보내게 유도하여 작업하게 만듭니다.
최종적으로 돈이 자동으로 인출될 수도, 자동으로 구매가 될 수도 있게됩니다.

하지만 SpringSecurity는 이 또한 기본값으로 보안을 해놨습니다. 요청을 보내기전 랜덤 토큰 값을 요구하고, 요구한 랜덤토큰 값이 존재하고, 일치하면 진행하게 됩니다.

protected void configure(HttpSecurity http) throws Exception {
	http.csrf() // 기본값 : 활성화되어 있음
	http.csrf().disabled() // 비활성화
}

따라서 토큰을 담아줄 수 있는 hidden값을 만들어야합니다.

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

번외, sessionManagement()

sessionManagement()를 이용하면 한 아이디에 두명이 접속시 발생할 수 있는 문제점을 선택하여 해결가능 합니다.

protected void configure(HttpSecurity http) throws Exception {     
http.sessionManagement()
	 .maximumSessions(1)                 // 최대 허용 가능 세션 수 , -1 : 무제한 로그인 세션 허용
      .maxSessionsPreventsLogin(true)    // 동시 로그인 차단함,  false : 기존 세션 만료(default)
      .invalidSessionUrl("/invalid")     // 세션이 유효하지 않을 때 이동 할 페이지
      .expiredUrl("/expired ")  	     // 세션이 만료된 경우 이동 할 페이지
}

결론

이처럼 Spring WebSecurity사용하면 기본적이 보안을 해결해줍니다.

profile
하이

0개의 댓글