Skip to content

Commit 4b9e423

Browse files
committed
Add actuator endpoint to find and delete sessions
See gh-407
1 parent 6720141 commit 4b9e423

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ include 'spring-session-data-gemfire'
2828
include 'spring-session-data-redis'
2929
include 'spring-session-jdbc'
3030
include 'spring-session-data-mongo'
31+
include 'spring-session-boot'

spring-session-boot/build.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
dependencies {
6+
classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
7+
}
8+
}
9+
10+
apply from: JAVA_GRADLE
11+
apply plugin: 'spring-io'
12+
13+
description = "Aggregator for Spring Session and Spring Boot"
14+
15+
dependencies {
16+
compile project(':spring-session')
17+
optional "org.springframework.boot:spring-boot-actuator",
18+
"org.springframework.boot:spring-boot-starter-jdbc",
19+
"org.springframework.boot:spring-boot-starter-web"
20+
21+
testCompile "org.springframework.boot:spring-boot-starter-test",
22+
"com.h2database:h2"
23+
}
24+
25+
dependencyManagement {
26+
springIoTestRuntime {
27+
imports {
28+
mavenBom "io.spring.platform:platform-bom:${springIoVersion}"
29+
}
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2014-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session.endpoint;
18+
19+
import org.springframework.boot.actuate.endpoint.Endpoint;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.session.FindByIndexNameSessionRepository;
25+
26+
/**
27+
* {@link EnableAutoConfiguration Auto-configuration} for session management
28+
* {@link Endpoint}s.
29+
*
30+
* @author Eddú Meléndez
31+
* @since 1.2.0
32+
*/
33+
@Configuration
34+
@ConditionalOnBean(FindByIndexNameSessionRepository.class)
35+
public class EndpointConfiguration {
36+
37+
@Bean
38+
public SessionEndpoint sessionEndpoint() {
39+
return new SessionEndpoint();
40+
}
41+
42+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2014-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session.endpoint;
18+
19+
import java.util.Collection;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.actuate.endpoint.Endpoint;
23+
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
24+
import org.springframework.session.ExpiringSession;
25+
import org.springframework.session.FindByIndexNameSessionRepository;
26+
import org.springframework.web.bind.annotation.PathVariable;
27+
import org.springframework.web.bind.annotation.RequestMapping;
28+
import org.springframework.web.bind.annotation.RequestMethod;
29+
30+
/**
31+
* {@link MvcEndpoint} to expose actuator session.
32+
*
33+
* @author Eddú Meléndez
34+
* @since 1.2.0
35+
*/
36+
public class SessionEndpoint implements MvcEndpoint {
37+
38+
@Autowired
39+
private FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository;
40+
41+
@RequestMapping(path = "/{username}", method = RequestMethod.GET)
42+
public Collection<? extends ExpiringSession> result(@PathVariable String username) {
43+
return this.sessionRepository.findByIndexNameAndIndexValue(
44+
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, username)
45+
.values();
46+
}
47+
48+
@RequestMapping(path = "/{sessionId}", method = RequestMethod.DELETE)
49+
public void delete(@PathVariable String sessionId) {
50+
this.sessionRepository.delete(sessionId);
51+
}
52+
53+
public String getPath() {
54+
return "/session";
55+
}
56+
57+
public boolean isSensitive() {
58+
return true;
59+
}
60+
61+
public Class<? extends Endpoint> getEndpointType() {
62+
return Endpoint.class;
63+
}
64+
65+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration=\
2+
org.springframework.session.endpoint.EndpointConfiguration
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.springframework.session.endpoint;/*
2+
* Copyright 2014-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import static org.hamcrest.CoreMatchers.notNullValue;
18+
import static org.hamcrest.core.Is.is;
19+
import static org.junit.Assert.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Test;
23+
24+
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
25+
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
26+
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.session.FindByIndexNameSessionRepository;
31+
import org.springframework.session.SessionRepository;
32+
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
33+
import org.springframework.transaction.PlatformTransactionManager;
34+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
35+
36+
import javax.sql.DataSource;
37+
38+
/**
39+
* Tests for {@link SessionEndpoint}.
40+
*
41+
* @author Eddú Meléndez
42+
*/
43+
public class SessionEndpointTests {
44+
45+
private AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
46+
47+
@After
48+
public void close() {
49+
if (this.applicationContext != null) {
50+
this.applicationContext.close();
51+
}
52+
}
53+
54+
@Test
55+
public void test() {
56+
this.applicationContext.register(EmbeddedDataSourceConfiguration.class,
57+
DataSourceTransactionManagerAutoConfiguration.class,
58+
ManagementServerProperties.class, SessionAutoConfiguration.class,
59+
EndpointConfiguration.class, EndpointWebMvcAutoConfiguration.class);
60+
this.applicationContext.refresh();
61+
FindByIndexNameSessionRepository sessionRepository = this.applicationContext
62+
.getBean(FindByIndexNameSessionRepository.class);
63+
assertThat(sessionRepository, is(notNullValue()));
64+
}
65+
66+
@Test
67+
public void test2() {
68+
this.applicationContext.register(EmbeddedDataSourceConfiguration.class,
69+
DataSourceTransactionManagerAutoConfiguration.class,
70+
ManagementServerProperties.class, EndpointConfiguration.class,
71+
EndpointWebMvcAutoConfiguration.class);
72+
this.applicationContext.refresh();
73+
String[] sessionRepository = this.applicationContext
74+
.getBeanNamesForType(FindByIndexNameSessionRepository.class);
75+
assertThat(sessionRepository.length, is(0));
76+
}
77+
78+
@Configuration
79+
static class SessionAutoConfiguration {
80+
81+
@Bean
82+
public FindByIndexNameSessionRepository sessionRepository(DataSource dataSource,
83+
PlatformTransactionManager transactionManager) {
84+
return new JdbcOperationsSessionRepository(dataSource, transactionManager);
85+
}
86+
}
87+
88+
}

0 commit comments

Comments
 (0)