Skip to content

Add session endpoints #5730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-boot-actuator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@
<artifactId>spring-security-config</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
import org.springframework.boot.actuate.endpoint.SessionEndpoint;
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
import org.springframework.boot.actuate.health.HealthAggregator;
Expand All @@ -61,6 +62,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;

/**
Expand Down Expand Up @@ -216,4 +218,17 @@ public RequestMappingEndpoint requestMappingEndpoint() {

}

@Configuration
@ConditionalOnBean(FindByIndexNameSessionRepository.class)
@ConditionalOnClass(FindByIndexNameSessionRepository.class)
static class SessionEndpointConfiguration {

@Bean
@ConditionalOnMissingBean
public SessionEndpoint sessionEndpoint() {
return new SessionEndpoint();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.SessionEndpoint;
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMappingCustomizer;
Expand All @@ -35,6 +36,7 @@
import org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints;
import org.springframework.boot.actuate.endpoint.mvc.SessionMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -55,6 +57,7 @@
* Configuration to expose {@link Endpoint} instances over Spring MVC.
*
* @author Dave Syer
* @author Eddú Meléndez
* @since 1.3.0
*/
@ManagementContextConfiguration
Expand Down Expand Up @@ -167,6 +170,13 @@ public ShutdownMvcEndpoint shutdownMvcEndpoint(ShutdownEndpoint delegate) {
return new ShutdownMvcEndpoint(delegate);
}

@Bean
@ConditionalOnBean(SessionEndpoint.class)
@ConditionalOnEnabledEndpoint(value = "session", enabledByDefault = false)
public SessionMvcEndpoint sessionMvcEndpoint(SessionEndpoint delegate) {
return new SessionMvcEndpoint(delegate);
}

private static class LogFileCondition extends SpringBootCondition {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.endpoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository;

/**
* {@link Endpoint} to manage web sessions.
*
* @author Eddú Meléndez
* @since 1.4.0
*/
@ConfigurationProperties("endpoints.session")
public class SessionEndpoint implements Endpoint<Object> {

@Autowired
private FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository;

/**
* Enable the endpoint.
*/
private boolean enabled = false;

@Override
public String getId() {
return "session";
}

@Override
public boolean isEnabled() {
return this.enabled;
}

@Override
public boolean isSensitive() {
return true;
}

@Override
public Object invoke() {
return null;
}

public Map<Object, Object> result(String username) {
List<Object> sessions = new ArrayList<Object>();
for (ExpiringSession session : this.sessionRepository.findByIndexNameAndIndexValue(
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, username)
.values()) {
sessions.add(new Session(session));
}
Map<Object, Object> sessionEntries = new HashMap<Object, Object>();
sessionEntries.put("sessions", sessions);
return Collections.unmodifiableMap(sessionEntries);
}

public boolean delete(String sessionId) {
ExpiringSession session = this.sessionRepository.getSession(sessionId);
if (session != null) {
this.sessionRepository.delete(sessionId);
return true;
}
return false;
}

/**
* Session properties.
*/
public static class Session {

private String id;

private long creationTime;

private long lastAccessedTime;

public Session(ExpiringSession session) {
this.id = session.getId();
this.creationTime = session.getCreationTime();
this.lastAccessedTime = session.getLastAccessedTime();
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public long getCreationTime() {
return this.creationTime;
}

public void setCreationTime(long creationTime) {
this.creationTime = creationTime;
}

public long getLastAccessedTime() {
return this.lastAccessedTime;
}

public void setLastAccessedTime(long lastAccessedTime) {
this.lastAccessedTime = lastAccessedTime;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
*
* @author Christian Dupuis
* @author Andy Wilkinson
* @author Eddú Meléndez
*/
public class EndpointMBeanExporter extends MBeanExporter
implements SmartLifecycle, ApplicationContextAware {
Expand Down Expand Up @@ -188,6 +189,9 @@ protected void registerEndpoint(String beanName, Endpoint<?> endpoint) {
}

protected EndpointMBean getEndpointMBean(String beanName, Endpoint<?> endpoint) {
if (endpoint instanceof SessionEndpointMBean) {
return new SessionEndpointMBean(beanName, endpoint, this.objectMapper);
}
if (endpoint instanceof ShutdownEndpoint) {
return new ShutdownEndpointMBean(beanName, endpoint, this.objectMapper);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.endpoint.jmx;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.SessionEndpoint;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedResource;

/**
* Special endpoint wrapper for {@link SessionEndpoint}.
*
* @author Eddú Meléndez
* @since 1.4.0
*/
@ManagedResource
public class SessionEndpointMBean extends EndpointMBean {

private SessionEndpoint sessionEndpoint;

/**
* Create a new {@link SessionEndpointMBean} instance.
*
* @param beanName the bean name
* @param endpoint the endpoint to wrap
* @param objectMapper the {@link ObjectMapper} used to convert the payload
*/
public SessionEndpointMBean(String beanName, Endpoint<?> endpoint, ObjectMapper objectMapper) {
super(beanName, endpoint, objectMapper);
this.sessionEndpoint = (SessionEndpoint) getEndpoint();
}

@ManagedOperation(description = "Find current user's sessions")
@ManagedOperationParameter(name = "username", description = "Application's username")
public Object findSessionsByUsername(String username) {
return convert(this.sessionEndpoint.result(username));
}

@ManagedOperation(description = "Delete session by id")
@ManagedOperationParameter(name = "sessionId", description = "Web session id")
public boolean deleteSessionBySessionId(String sessionId) {
return this.sessionEndpoint.delete(sessionId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.endpoint.mvc;

import java.util.Map;

import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.SessionEndpoint;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Adapter to expose {@link SessionEndpoint} as an {@link MvcEndpoint}.
*
* @author Eddú Meléndez
* @since 1.4.0
*/
@ConfigurationProperties(prefix = "endpoints.session")
public class SessionMvcEndpoint extends AbstractEndpointMvcAdapter<SessionEndpoint> {

/**
* Create a new {@link EndpointMvcAdapter}.
*
* @param delegate the underlying {@link Endpoint} to adapt.
*/
public SessionMvcEndpoint(SessionEndpoint delegate) {
super(delegate);
}

@RequestMapping(method = RequestMethod.GET)
public Map<Object, Object> result(@RequestParam String username) {
return getDelegate().result(username);
}

@RequestMapping(path = "/{sessionId}", method = RequestMethod.DELETE)
public ResponseEntity delete(@PathVariable String sessionId) {
boolean deleted = getDelegate().delete(sessionId);
if (deleted) {
return ResponseEntity.ok().build();
}
return ResponseEntity.notFound().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.util.ApplicationContextTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
Expand Down Expand Up @@ -65,7 +66,8 @@ public void testChild() {
ElasticsearchRepositoriesAutoConfiguration.class,
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
Neo4jAutoConfiguration.class, RedisAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class }, excludeName = {
RedisRepositoriesAutoConfiguration.class,
SessionAutoConfiguration.class }, excludeName = {
"org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration" })
public static class Child {

Expand Down
Loading