Skip to content

Commit 661f9ca

Browse files
committed
Add actuator endpoint for finding and deleting sessions
1 parent 714c533 commit 661f9ca

File tree

18 files changed

+615
-19
lines changed

18 files changed

+615
-19
lines changed

spring-boot-actuator-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@
311311
<artifactId>spring-security-web</artifactId>
312312
<optional>true</optional>
313313
</dependency>
314+
<dependency>
315+
<groupId>org.springframework.session</groupId>
316+
<artifactId>spring-session-core</artifactId>
317+
<optional>true</optional>
318+
</dependency>
314319
<!-- Annotation processing -->
315320
<dependency>
316321
<groupId>org.springframework.boot</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2017 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.boot.actuate.autoconfigure.session;
18+
19+
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
20+
import org.springframework.boot.actuate.session.SessionsEndpoint;
21+
import org.springframework.boot.actuate.session.SessionsWebEndpointExtension;
22+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
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.Session;
32+
33+
/**
34+
* {@link EnableAutoConfiguration Auto-configuration} for {@link SessionsEndpoint}.
35+
*
36+
* @author Vedran Pavic
37+
* @since 2.0.0
38+
*/
39+
@Configuration
40+
@ConditionalOnClass(FindByIndexNameSessionRepository.class)
41+
@AutoConfigureAfter(SessionAutoConfiguration.class)
42+
public class SessionsEndpointAutoConfiguration {
43+
44+
@Bean
45+
@ConditionalOnBean(FindByIndexNameSessionRepository.class)
46+
@ConditionalOnMissingBean
47+
@ConditionalOnEnabledEndpoint
48+
public SessionsEndpoint sessionEndpoint(
49+
FindByIndexNameSessionRepository<? extends Session> sessionRepository) {
50+
return new SessionsEndpoint(sessionRepository);
51+
}
52+
53+
@Bean
54+
@ConditionalOnMissingBean
55+
@ConditionalOnEnabledEndpoint
56+
@ConditionalOnBean(SessionsEndpoint.class)
57+
public SessionsWebEndpointExtension sessionsWebEndpointExtension(
58+
SessionsEndpoint sessionsEndpoint) {
59+
return new SessionsWebEndpointExtension(sessionsEndpoint);
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2017 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+
/**
18+
* Auto-configuration for actuator Spring Sessions concerns.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.session;

spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration,
2929
org.springframework.boot.actuate.autoconfigure.mongo.MongoHealthIndicatorAutoConfiguration,\
3030
org.springframework.boot.actuate.autoconfigure.neo4j.Neo4jHealthIndicatorAutoConfiguration,\
3131
org.springframework.boot.actuate.autoconfigure.redis.RedisHealthIndicatorAutoConfiguration,\
32+
org.springframework.boot.actuate.autoconfigure.session.SessionsEndpointAutoConfiguration,\
3233
org.springframework.boot.actuate.autoconfigure.solr.SolrHealthIndicatorAutoConfiguration,\
3334
org.springframework.boot.actuate.autoconfigure.system.DiskSpaceHealthIndicatorAutoConfiguration,\
3435
org.springframework.boot.actuate.autoconfigure.trace.TraceEndpointAutoConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2017 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.boot.actuate.autoconfigure.session;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.actuate.session.SessionsEndpoint;
22+
import org.springframework.boot.actuate.session.SessionsWebEndpointExtension;
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.session.FindByIndexNameSessionRepository;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.Mockito.mock;
31+
32+
/**
33+
* Tests for {@link SessionsEndpointAutoConfiguration}.
34+
*
35+
* @author Vedran Pavic
36+
*/
37+
public class SessionsEndpointAutoConfigurationTests {
38+
39+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
40+
.withConfiguration(
41+
AutoConfigurations.of(SessionsEndpointAutoConfiguration.class))
42+
.withUserConfiguration(SessionConfiguration.class);
43+
44+
@Test
45+
public void runShouldHaveEndpointBean() {
46+
this.contextRunner.run(
47+
(context) -> assertThat(context).hasSingleBean(SessionsEndpoint.class));
48+
}
49+
50+
@Test
51+
public void runShouldHaveWebExtensionBean() {
52+
this.contextRunner.run((context) -> assertThat(context)
53+
.hasSingleBean(SessionsWebEndpointExtension.class));
54+
}
55+
56+
@Test
57+
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointOrExtensionBean()
58+
throws Exception {
59+
this.contextRunner.withPropertyValues("endpoints.sessions.enabled:false").run(
60+
(context) -> assertThat(context).doesNotHaveBean(SessionsEndpoint.class));
61+
}
62+
63+
@Configuration
64+
static class SessionConfiguration {
65+
66+
@Bean
67+
public FindByIndexNameSessionRepository sessionRepository() {
68+
return mock(FindByIndexNameSessionRepository.class);
69+
}
70+
71+
}
72+
73+
}

spring-boot-actuator/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@
199199
<artifactId>spring-security-web</artifactId>
200200
<optional>true</optional>
201201
</dependency>
202+
<dependency>
203+
<groupId>org.springframework.session</groupId>
204+
<artifactId>spring-session-core</artifactId>
205+
<optional>true</optional>
206+
</dependency>
202207
<!-- Annotation processing -->
203208
<dependency>
204209
<groupId>org.springframework.boot</groupId>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2012-2017 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.boot.actuate.session;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Set;
22+
import java.util.stream.Collectors;
23+
24+
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
25+
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
26+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
27+
import org.springframework.boot.actuate.endpoint.annotation.Selector;
28+
import org.springframework.session.FindByIndexNameSessionRepository;
29+
import org.springframework.session.Session;
30+
31+
/**
32+
* {@link Endpoint} to expose a user's {@link Session}s.
33+
*
34+
* @author Vedran Pavic
35+
* @since 2.0.0
36+
*/
37+
@Endpoint(id = "sessions")
38+
public class SessionsEndpoint {
39+
40+
private final FindByIndexNameSessionRepository<? extends Session> sessionRepository;
41+
42+
/**
43+
* Create a new {@link SessionsEndpoint} instance.
44+
* @param sessionRepository the session repository
45+
*/
46+
public SessionsEndpoint(
47+
FindByIndexNameSessionRepository<? extends Session> sessionRepository) {
48+
this.sessionRepository = sessionRepository;
49+
}
50+
51+
@ReadOperation
52+
public SessionsReport sessionsForUsername(String username) {
53+
Map<String, ? extends Session> sessions = this.sessionRepository
54+
.findByIndexNameAndIndexValue(
55+
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME,
56+
username);
57+
return new SessionsReport(sessions);
58+
}
59+
60+
@ReadOperation
61+
public SessionDescriptor getSession(@Selector String sessionId) {
62+
Session session = this.sessionRepository.findById(sessionId);
63+
return new SessionDescriptor(session);
64+
}
65+
66+
@DeleteOperation
67+
public void deleteSession(@Selector String sessionId) {
68+
this.sessionRepository.deleteById(sessionId);
69+
}
70+
71+
/**
72+
* A report of user's {@link Session sessions}. Primarily intended for serialization
73+
* to JSON.
74+
*/
75+
public static final class SessionsReport {
76+
77+
private final List<SessionDescriptor> sessions;
78+
79+
public SessionsReport(Map<String, ? extends Session> sessions) {
80+
this.sessions = sessions.entrySet().stream()
81+
.map(s -> new SessionDescriptor(s.getValue()))
82+
.collect(Collectors.toList());
83+
}
84+
85+
public List<SessionDescriptor> getSessions() {
86+
return this.sessions;
87+
}
88+
89+
}
90+
91+
/**
92+
* A description of user's {@link Session session}. Primarily intended for
93+
* serialization to JSON.
94+
*/
95+
public static final class SessionDescriptor {
96+
97+
private final String id;
98+
99+
private final Set<String> attributeNames;
100+
101+
private final long creationTime;
102+
103+
private final long lastAccessedTime;
104+
105+
private final long maxInactiveInterval;
106+
107+
private final boolean expired;
108+
109+
public SessionDescriptor(Session session) {
110+
this.id = session.getId();
111+
this.attributeNames = session.getAttributeNames();
112+
this.creationTime = session.getCreationTime().toEpochMilli();
113+
this.lastAccessedTime = session.getLastAccessedTime().toEpochMilli();
114+
this.maxInactiveInterval = session.getMaxInactiveInterval().getSeconds();
115+
this.expired = session.isExpired();
116+
}
117+
118+
public String getId() {
119+
return this.id;
120+
}
121+
122+
public Set<String> getAttributeNames() {
123+
return this.attributeNames;
124+
}
125+
126+
public long getCreationTime() {
127+
return this.creationTime;
128+
}
129+
130+
public long getLastAccessedTime() {
131+
return this.lastAccessedTime;
132+
}
133+
134+
public long getMaxInactiveInterval() {
135+
return this.maxInactiveInterval;
136+
}
137+
138+
public boolean isExpired() {
139+
return this.expired;
140+
}
141+
142+
}
143+
144+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2017 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.boot.actuate.session;
18+
19+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
20+
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
21+
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension;
22+
import org.springframework.boot.actuate.session.SessionsEndpoint.SessionsReport;
23+
24+
/**
25+
* {@link WebEndpointExtension} for the {@link SessionsEndpoint}.
26+
*
27+
* @author Vedran Pavic
28+
* @since 2.0.0
29+
*/
30+
@WebEndpointExtension(endpoint = SessionsEndpoint.class)
31+
public class SessionsWebEndpointExtension {
32+
33+
private final SessionsEndpoint delegate;
34+
35+
public SessionsWebEndpointExtension(SessionsEndpoint delegate) {
36+
this.delegate = delegate;
37+
}
38+
39+
@ReadOperation
40+
public WebEndpointResponse<SessionsReport> sessionsForUsername(String username) {
41+
if (username == null) {
42+
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
43+
}
44+
SessionsReport sessions = this.delegate.sessionsForUsername(username);
45+
return new WebEndpointResponse<>(sessions);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)