|
| 1 | +package org.jenkinsci.plugins.cas.spring.security; |
| 2 | + |
| 3 | +import javax.servlet.http.HttpSession; |
| 4 | + |
| 5 | +import org.jasig.cas.client.session.SessionMappingStorage; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.security.core.Authentication; |
| 9 | +import org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy; |
| 10 | + |
| 11 | +/** |
| 12 | + * Session fixation protection strategy that invalidates the existing session |
| 13 | + * and integrates with the Single Sign-Out session mapping storage. |
| 14 | + * |
| 15 | + * @author Fabien Crespel |
| 16 | + */ |
| 17 | +public class CasSessionFixationProtectionStrategy extends SessionFixationProtectionStrategy { |
| 18 | + |
| 19 | + private static final Logger LOG = LoggerFactory.getLogger(CasSessionFixationProtectionStrategy.class); |
| 20 | + |
| 21 | + protected SessionMappingStorage sessionStorage = null; |
| 22 | + |
| 23 | + public CasSessionFixationProtectionStrategy() { |
| 24 | + } |
| 25 | + |
| 26 | + public CasSessionFixationProtectionStrategy(SessionMappingStorage sessionStorage) { |
| 27 | + this.sessionStorage = sessionStorage; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void onSessionChange(String originalSessionId, HttpSession newSession, Authentication auth) { |
| 32 | + if (sessionStorage != null) { |
| 33 | + LOG.debug("Session changed, removing existing session with ID '{}'", originalSessionId); |
| 34 | + sessionStorage.removeBySessionById(originalSessionId); |
| 35 | + if (auth.getCredentials() instanceof String) { |
| 36 | + LOG.debug("Session changed, adding new session with ID '{}'", newSession.getId()); |
| 37 | + sessionStorage.addSessionById((String) auth.getCredentials(), newSession); |
| 38 | + } |
| 39 | + } |
| 40 | + super.onSessionChange(originalSessionId, newSession, auth); |
| 41 | + } |
| 42 | + |
| 43 | + public SessionMappingStorage getSessionStorage() { |
| 44 | + return sessionStorage; |
| 45 | + } |
| 46 | + |
| 47 | + public void setSessionStorage(SessionMappingStorage sessionStorage) { |
| 48 | + this.sessionStorage = sessionStorage; |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments