관리 메뉴

IT.FARMER

Spring Security Session 제어 본문

Spring/Spring Security

Spring Security Session 제어

아이티.파머 2014. 11. 6. 17:00
반응형

Spring Security 의 Session

당연히 우리는 web.xml 에

<session-config> 이녀석을 통해 session 설정을 한다.

 

하지만 간혹 세션 설정을  할 수 있기를 바라는 사람들이 있는데

일반 적인 프로그래밍으로는

 

...

request.getSession().setMaxInactiveInterval( second time)

...

 

이렇게 사용 할 수 있다.

 

 

Security 도 별다른건 없다. 로그인 성공시 사용되는 핸들러인

SavedRequestAwareAuthenticationSuccessHandler 를 상속 받아  다음과 같이 구현 하도록 한다.

@Service
public class AuthLoginSuccessHandler extends  SavedRequestAwareAuthenticationSuccessHandler {
 
 
 @Autowired private SysConfigDao SysConfigDao;
 @Override
 public void onAuthenticationSuccess(HttpServletRequest request,
   HttpServletResponse response, Authentication authentication)
   throws IOException, ServletException {
  
  SysConfig maxSessionMinuteConfig = SysConfigDao.selectOne( new SfSession(), SysCfgLogin.MAX_SESSION_MINUTE);
  request.getSession().setMaxInactiveInterval(NumberUtils.toInt(maxSessionMinuteConfig.getEnvValue(), 10) * 60);
  
        super.onAuthenticationSuccess(request, response, authentication);
 }
}

 

 

위 파일을 security-context.xml 에 등록함으로 마무리 한다.

<http>

.... 

<form-login login-page="/auth/auth_form.do"
      login-processing-url="/login"
      default-target-url="/home/homepage.do"
      always-use-default-target="true"
      authentication-failure-handler-ref="authLoginFailureHandler"
      authentication-success-handler-ref="authLoginSuccessHandler"
      password-parameter="authCode"
      username-parameter="userCode" />

...

</http>

 

SavedRequestAwareAuthenticationSuccessHandler  이 핸들러에 대한 설명은 생략.

 

이제 원하는 시간으로 세션 유지시간 설정이 가능하다~~

 

 

 

 

반응형