org.springframework.session.ExpiringSession Java Examples

The following examples show how to use org.springframework.session.ExpiringSession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FixedMapSessionRepository.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ExpiringSession getSession(final String id)
{
	final ExpiringSession saved = sessions.get(id);
	if (saved == null)
	{
		return null;
	}
	if (saved.isExpired())
	{
		final boolean expired = true;
		deleteAndFireEvent(saved.getId(), expired);
		return null;
	}

	return new MapSession(saved);
}
 
Example #2
Source File: FixedMapSessionRepository.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 6 votes vote down vote up
private void deleteAndFireEvent(final String id, boolean expired)
{
	final ExpiringSession deletedSession = sessions.remove(id);

	// Fire event
	if (deletedSession != null)
	{
		if (expired)
		{
			applicationEventPublisher.publishEvent(new SessionExpiredEvent(this, id));
		}
		else
		{
			applicationEventPublisher.publishEvent(new SessionDeletedEvent(this, id));
		}
	}
}
 
Example #3
Source File: FixedMapSessionRepository.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 6 votes vote down vote up
public void purgeExpiredSessions()
{
	final Stopwatch stopwatch = Stopwatch.createStarted();
	int countExpiredSessions = 0;

	final List<ExpiringSession> sessionsToCheck = new ArrayList<>(sessions.values());
	for (final ExpiringSession session : sessionsToCheck)
	{
		if (session.isExpired())
		{
			deleteAndFireEvent(session.getId(), true /* expired */);
			countExpiredSessions++;
		}
	}

	logger.debug("Purged {}/{} expired sessions in {}", countExpiredSessions, sessionsToCheck.size(), stopwatch);
}
 
Example #4
Source File: FixedMapSessionRepository.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ExpiringSession createSession()
{
	final ExpiringSession result = new MapSession();
	if (defaultMaxInactiveInterval != null)
	{
		result.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);
	}

	// Fire event
	applicationEventPublisher.publishEvent(new SessionCreatedEvent(this, result.getId()));

	return result;
}
 
Example #5
Source File: SpringSessionBackedSessionInformation.java    From spring-session-concurrent-session-control with Apache License 2.0 5 votes vote down vote up
public SpringSessionBackedSessionInformation(ExpiringSession session, SessionRepository<? extends ExpiringSession> sessionRepository) {
    super(resolvePrincipal(session), session.getId(), new Date(session.getLastAccessedTime()));
    this.sessionRepository = sessionRepository;
    if (session.isExpired()) {
        super.expireNow();
    }
}
 
Example #6
Source File: SpringSessionBackedSessionRegistry.java    From spring-session-concurrent-session-control with Apache License 2.0 5 votes vote down vote up
@Override
public SessionInformation getSessionInformation(String sessionId) {
    ExpiringSession session = sessionRepository.getSession(sessionId);
    if (session != null) {
        return new SpringSessionBackedSessionInformation(session, sessionRepository);
    }
    return null;
}
 
Example #7
Source File: App.java    From spring-session-concurrent-session-control with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/")
String listSessions(Principal principal, HttpSession session, Model model) {
    Collection<? extends ExpiringSession> userSessions = sessions.findByIndexNameAndIndexValue(PRINCIPAL_NAME_INDEX_NAME, principal.getName()).values();
    model.addAttribute("sessions", userSessions);
    // this gives us the correct session ID, as the HttpSession is backed by Spring Session:
    model.addAttribute("currSessionId", session.getId());
    return "index";
}
 
Example #8
Source File: RedisConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
/**
 * SpringSession config
 * @return
 * @throws Exception
 */
@Bean
public RedisTemplate<String,ExpiringSession> redisTemplate() throws Exception {
    RedisTemplate<String, ExpiringSession> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #9
Source File: CookieController.java    From SpringCloud-Shop with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/test/findByUsername")
@ResponseBody
public Map findByUsername(@RequestParam String username) {
    Map<String, ? extends ExpiringSession> usersSessions = sessionRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, username);
    return usersSessions;
}
 
Example #10
Source File: BasicAuthSecurityConfiguration.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
@Bean
public SessionRepository<ExpiringSession> sessionRepository() {
	return new MapSessionRepository();
}
 
Example #11
Source File: BasicAuthSecurityConfiguration.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(HttpSecurity http) throws Exception {
	final RequestMatcher textHtmlMatcher = new MediaTypeRequestMatcher(
			contentNegotiationStrategy,
			MediaType.TEXT_HTML);

	final String loginPage = dashboard("/#/login");

	final BasicAuthenticationEntryPoint basicAuthenticationEntryPoint = new BasicAuthenticationEntryPoint();
	basicAuthenticationEntryPoint.setRealmName(securityProperties.getBasic().getRealm());
	basicAuthenticationEntryPoint.afterPropertiesSet();

	http
		.csrf()
		.disable()
		.authorizeRequests()
		.antMatchers("/")
		.authenticated()
		.antMatchers(
				dashboard("/**"),
				"/authenticate",
				"/security/info",
				"/features",
				"/assets/**").permitAll()
	.and()
		.formLogin().loginPage(loginPage)
		.loginProcessingUrl(dashboard("/login"))
		.defaultSuccessUrl(dashboard("/")).permitAll()
	.and()
		.logout().logoutUrl(dashboard("/logout"))
			.logoutSuccessUrl(dashboard("/logout-success.html"))
		.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()).permitAll()
	.and().httpBasic()
		.and().exceptionHandling()
		.defaultAuthenticationEntryPointFor(
				new LoginUrlAuthenticationEntryPoint(loginPage),
				textHtmlMatcher)
		.defaultAuthenticationEntryPointFor(basicAuthenticationEntryPoint,
				AnyRequestMatcher.INSTANCE)
	.and()
		.authorizeRequests()
		.anyRequest().authenticated();

	final SessionRepositoryFilter<ExpiringSession> sessionRepositoryFilter = new SessionRepositoryFilter<ExpiringSession>(
			sessionRepository());
	sessionRepositoryFilter
			.setHttpSessionStrategy(new HeaderHttpSessionStrategy());

	http.addFilterBefore(sessionRepositoryFilter,
			ChannelProcessingFilter.class).csrf().disable();
	http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
}
 
Example #12
Source File: FixedMapSessionRepository.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void save(final ExpiringSession session)
{
	sessions.put(session.getId(), new MapSession(session));
}
 
Example #13
Source File: SpringSessionBackedSessionRegistry.java    From spring-session-concurrent-session-control with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
public SpringSessionBackedSessionRegistry(FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository) {
    this.sessionRepository = sessionRepository;
}
 
Example #14
Source File: PreAuthenticatedTokenCacheService.java    From bearchoke with Apache License 2.0 4 votes vote down vote up
@Autowired
public PreAuthenticatedTokenCacheService(Environment environment, CacheManager cacheManager, RedisTemplate<String, ExpiringSession> redisTemplate) {
    this.environment = environment;
    this.cacheManager = cacheManager;
    this.redisTemplate = redisTemplate;
}