diff --git a/docs/src/docs/asciidoc/index.adoc b/docs/src/docs/asciidoc/index.adoc index bd5b872b..02452ac6 100644 --- a/docs/src/docs/asciidoc/index.adoc +++ b/docs/src/docs/asciidoc/index.adoc @@ -614,9 +614,10 @@ Spring Session's most basic API for using a `Session` is the `SessionRepository` This API is intentionally very simple, so that it is easy to provide additional implementations with basic functionality. Some `SessionRepository` implementations may choose to implement `FindByIndexNameSessionRepository` also. -For example, Spring's Redis support implements `FindByIndexNameSessionRepository`. +For example, Spring's Redis, JDBC and Hazelcast support all implement `FindByIndexNameSessionRepository`. -The `FindByIndexNameSessionRepository` adds a single method to look up all the sessions for a particular user. +The `FindByIndexNameSessionRepository` provides a method to look up all the sessions with a given index name and index value. +As a common use case that is supported by all provided `FindByIndexNameSessionRepository` implementations, there's a convenient method to look up all the sessions for a particular user. This is done by ensuring that the session attribute with the name `FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME` is populated with the username. It is the responsibility of the developer to ensure the attribute is populated since Spring Session is not aware of the authentication mechanism being used. An example of how this might be used can be seen below: diff --git a/docs/src/test/java/docs/FindByIndexNameSessionRepositoryTests.java b/docs/src/test/java/docs/FindByIndexNameSessionRepositoryTests.java index c574b113..ccc87673 100644 --- a/docs/src/test/java/docs/FindByIndexNameSessionRepositoryTests.java +++ b/docs/src/test/java/docs/FindByIndexNameSessionRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -52,9 +52,7 @@ public class FindByIndexNameSessionRepositoryTests { // tag::findby-username[] String username = "username"; Map sessionIdToSession = this.sessionRepository - .findByIndexNameAndIndexValue( - FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, - username); + .findByPrincipalName(username); // end::findby-username[] } } diff --git a/samples/boot/findbyusername/src/main/java/sample/mvc/IndexController.java b/samples/boot/findbyusername/src/main/java/sample/mvc/IndexController.java index 944590d8..086b626a 100644 --- a/samples/boot/findbyusername/src/main/java/sample/mvc/IndexController.java +++ b/samples/boot/findbyusername/src/main/java/sample/mvc/IndexController.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -44,10 +44,7 @@ public class IndexController { @RequestMapping("/") public String index(Principal principal, Model model) { Collection usersSessions = this.sessions - .findByIndexNameAndIndexValue( - FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, - principal.getName()) - .values(); + .findByPrincipalName(principal.getName()).values(); model.addAttribute("sessions", usersSessions); return "index"; } @@ -56,9 +53,8 @@ public class IndexController { @RequestMapping(value = "/sessions/{sessionIdToDelete}", method = RequestMethod.DELETE) public String removeSession(Principal principal, @PathVariable String sessionIdToDelete) { - Set usersSessionIds = this.sessions.findByIndexNameAndIndexValue( - FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, - principal.getName()).keySet(); + Set usersSessionIds = this.sessions + .findByPrincipalName(principal.getName()).keySet(); if (usersSessionIds.contains(sessionIdToDelete)) { this.sessions.deleteById(sessionIdToDelete); } diff --git a/spring-session-core/src/main/java/org/springframework/session/FindByIndexNameSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/FindByIndexNameSessionRepository.java index c0832199..2adada7e 100644 --- a/spring-session-core/src/main/java/org/springframework/session/FindByIndexNameSessionRepository.java +++ b/spring-session-core/src/main/java/org/springframework/session/FindByIndexNameSessionRepository.java @@ -19,27 +19,22 @@ package org.springframework.session; import java.util.Map; /** - * Extends a basic {@link SessionRepository} to allow finding a session id by the - * principal name. The principal name is defined by the {@link Session} attribute with the - * name {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME}. + * Extends a basic {@link SessionRepository} to allow finding sessions by the specified + * index name and index value. * * @param the type of Session being managed by this * {@link FindByIndexNameSessionRepository} * @author Rob Winch + * @author Vedran Pavic */ public interface FindByIndexNameSessionRepository extends SessionRepository { /** + * A session index that contains the current principal name (i.e. username). *

- * A common session attribute that contains the current principal name (i.e. - * username). - *

- * - *

- * It is the responsibility of the developer to ensure the attribute is populated - * since Spring Session is not aware of the authentication mechanism being used. - *

+ * It is the responsibility of the developer to ensure the index is populated since + * Spring Session is not aware of the authentication mechanism being used. * * @since 1.1 */ @@ -47,17 +42,34 @@ public interface FindByIndexNameSessionRepository .concat(".PRINCIPAL_NAME_INDEX_NAME"); /** - * Find a Map of the session id to the {@link Session} of all sessions that contain - * the session attribute with the name - * {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME} and the value of - * the specified principal name. + * Find a {@link Map} of the session id to the {@link Session} of all sessions that + * contain the specified index name index value. * * @param indexName the name of the index (i.e. * {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME}) * @param indexValue the value of the index to search for. - * @return a Map (never null) of the session id to the {@link Session} of all sessions - * that contain the session specified index name and the value of the specified index - * name. If no results are found, an empty Map is returned. + * @return a {@code Map} (never {@code null}) of the session id to the {@code Session} + * of all sessions that contain the specified index name and index value. If no + * results are found, an empty {@code Map} is returned. */ Map findByIndexNameAndIndexValue(String indexName, String indexValue); + + /** + * Find a {@link Map} of the session id to the {@link Session} of all sessions that + * contain the index with the name + * {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME} and the + * specified principal name. + * + * @param principalName the principal name + * @return a {@code Map} (never {@code null}) of the session id to the {@code Session} + * of all sessions that contain the specified principal name. If no results are found, + * an empty {@code Map} is returned. + * @since 2.1.0 + */ + default Map findByPrincipalName(String principalName) { + + return findByIndexNameAndIndexValue(PRINCIPAL_NAME_INDEX_NAME, principalName); + + } + } diff --git a/spring-session-core/src/main/java/org/springframework/session/security/SpringSessionBackedSessionRegistry.java b/spring-session-core/src/main/java/org/springframework/session/security/SpringSessionBackedSessionRegistry.java index 05c7eb53..c02490ae 100644 --- a/spring-session-core/src/main/java/org/springframework/session/security/SpringSessionBackedSessionRegistry.java +++ b/spring-session-core/src/main/java/org/springframework/session/security/SpringSessionBackedSessionRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -65,9 +65,8 @@ public class SpringSessionBackedSessionRegistry @Override public List getAllSessions(Object principal, boolean includeExpiredSessions) { - Collection sessions = this.sessionRepository.findByIndexNameAndIndexValue( - FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, - name(principal)).values(); + Collection sessions = this.sessionRepository + .findByPrincipalName(name(principal)).values(); List infos = new ArrayList<>(); for (S session : sessions) { if (includeExpiredSessions || !Boolean.TRUE.equals(session diff --git a/spring-session-core/src/test/java/org/springframework/session/security/SpringSessionBackedSessionRegistryTest.java b/spring-session-core/src/test/java/org/springframework/session/security/SpringSessionBackedSessionRegistryTest.java index 2a6080eb..a9403763 100644 --- a/spring-session-core/src/test/java/org/springframework/session/security/SpringSessionBackedSessionRegistryTest.java +++ b/spring-session-core/src/test/java/org/springframework/session/security/SpringSessionBackedSessionRegistryTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -162,9 +162,8 @@ public class SpringSessionBackedSessionRegistryTest { Map sessions = new LinkedHashMap<>(); sessions.put(session1.getId(), session1); sessions.put(session2.getId(), session2); - when(this.sessionRepository.findByIndexNameAndIndexValue( - FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, USER_NAME)) - .thenReturn(sessions); + when(this.sessionRepository.findByPrincipalName(USER_NAME)) + .thenReturn(sessions); } }