Spring Session and Spring Security with Hazelcast
本指南描述了使用 Hazelcast 作为数据存储时,如何将 Spring 会话与 Spring 安全性配合使用。假设您已向应用程序应用了 Spring 安全性。
This guide describes how to use Spring Session along with Spring Security when you use Hazelcast as your data store. It assumes that you have already applied Spring Security to your application.
你可以在Hazelcast Spring Security sample application中找到已完成的指南。 |
You cand find the completed guide in the hazelcast-spring-security-sample. |
Updating Dependencies
在使用 Spring 会话之前,您必须更新依赖。如果您使用 Maven,您必须添加以下依赖:
Before you use Spring Session, you must update your dependencies. If you use Maven, you must add the following dependencies:
<dependencies>
<!-- ... -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>{hazelcast-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>{spring-core-version}</version>
</dependency>
</dependencies>
Spring Configuration
在添加所需的依赖项后,我们可以创建我们的 Spring 配置。Spring 配置负责创建一个 Servlet 过滤器,该过滤器将 HttpSession
实现替换为由 Spring Session 支持的实现。为此,请添加以下 Spring 配置:
After adding the required dependencies, we can create our Spring configuration.
The Spring configuration is responsible for creating a servlet filter that replaces the HttpSession
implementation with an implementation backed by Spring Session.
To do so, add the following Spring Configuration:
/*
* Copyright 2014-2023 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
*
* https://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 docs.http;
import com.hazelcast.config.AttributeConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.IndexConfig;
import com.hazelcast.config.IndexType;
import com.hazelcast.config.SerializerConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.MapSession;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
import org.springframework.session.hazelcast.HazelcastSessionSerializer;
import org.springframework.session.hazelcast.PrincipalNameExtractor;
import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
// tag::config[]
@EnableHazelcastHttpSession (1)
@Configuration
public class HazelcastHttpSessionConfig {
@Bean
public HazelcastInstance hazelcastInstance() {
Config config = new Config();
AttributeConfig attributeConfig = new AttributeConfig()
.setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
.setExtractorClassName(PrincipalNameExtractor.class.getName());
config.getMapConfig(HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME) (2)
.addAttributeConfig(attributeConfig)
.addIndexConfig(
new IndexConfig(IndexType.HASH, HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE));
SerializerConfig serializerConfig = new SerializerConfig();
serializerConfig.setImplementation(new HazelcastSessionSerializer()).setTypeClass(MapSession.class);
config.getSerializationConfig().addSerializerConfig(serializerConfig); (3)
return Hazelcast.newHazelcastInstance(config); (4)
}
}
// end::config[]
1 | The @EnableHazelcastHttpSession annotation creates a Spring bean named springSessionRepositoryFilter that implements Filter .
The filter is in charge of replacing the HttpSession implementation to be backed by Spring Session.
In this instance, Spring Session is backed by Hazelcast. |
2 | In order to support retrieval of sessions by principal name index, an appropriate ValueExtractor needs to be registered.
Spring Session provides PrincipalNameExtractor for this purpose. |
3 | In order to serialize MapSession objects efficiently, HazelcastSessionSerializer needs to be registered. If this
is not set, Hazelcast will serialize sessions using native Java serialization. |
4 | We create a HazelcastInstance that connects Spring Session to Hazelcast.
By default, the application starts and connects to an embedded instance of Hazelcast.
For more information on configuring Hazelcast, see the reference documentation. |
如果选择`HazelcastSessionSerializer`,则必须在所有 Hazelcast 集群成员开始之前对所有 Hazelcast 集群成员进行配置。在 Hazelcast 集群中,所有成员都应使用用于会话的相同序列化方法。此外,如果使用 Hazelcast 客户端/服务器拓扑结构,则成员和客户端都必须使用相同序列化方法。可以使用`ClientConfig`注册序列化程序并使用与成员相同的`SerializerConfiguration`。 |
If |
Servlet Container Initialization
我们的 Spring Configuration 创建了一个名为 springSessionRepositoryFilter
的 Spring bean,它实现 Filter
。 springSessionRepositoryFilter
bean 负责用由 Spring Session 支持的自定义实现替换 HttpSession
。
Our Spring Configuration created a Spring bean named springSessionRepositoryFilter
that implements Filter
.
The springSessionRepositoryFilter
bean is responsible for replacing the HttpSession
with a custom implementation that is backed by Spring Session.
为了让我们的 Filter`发挥作用,Spring 需要加载我们的 `SessionConfig
类。由于我们的应用程序已经通过使用我们的 SecurityInitializer
类加载了 Spring 配置,我们可以将我们的 SessionConfig
添加到该类。以下清单显示了如何执行此操作:
In order for our Filter
to do its magic, Spring needs to load our SessionConfig
class.
Since our application is already loading Spring configuration by using our SecurityInitializer
class, we can add our SessionConfig
class to it.
The following listing shows how to do so:
Unresolved include directive in modules/ROOT/pages/guides/java-hazelcast.adoc - include::example$spring-session-samples/spring-session-sample-javaconfig-hazelcast/src/main/java/sample/SecurityInitializer.java[]
最后,我们需要确保我们的 Servlet 容器(即 Tomcat)对每个请求使用我们的 springSessionRepositoryFilter
。Spring 会话的 springSessionRepositoryFilter
在 Spring Security 的 springSecurityFilterChain
之前调用非常重要。这样做可以确保 Spring Security 使用的 HttpSession
由 Spring 会话支持。幸运的是,Spring 会话提供了一个名为 AbstractHttpSessionApplicationInitializer
的实用程序类,使此操作变得简单。以下示例显示了如何执行此操作:
Last, we need to ensure that our Servlet Container (that is, Tomcat) uses our springSessionRepositoryFilter
for every request.
It is extremely important that Spring Session’s springSessionRepositoryFilter
is invoked before Spring Security’s springSecurityFilterChain
.
Doing so ensures that the HttpSession
that Spring Security uses is backed by Spring Session.
Fortunately, Spring Session provides a utility class named AbstractHttpSessionApplicationInitializer
that makes this doing so easy.
The following example shows how to do so:
Unresolved include directive in modules/ROOT/pages/guides/java-hazelcast.adoc - include::example$spring-session-samples/spring-session-sample-javaconfig-hazelcast/src/main/java/sample/Initializer.java[]
我们的类 ( |
The name of our class ( |
通过扩展 AbstractHttpSessionApplicationInitializer
,我们确保为 Spring 安全的 springSecurityFilterChain
注册名为 springSessionRepositoryFilter
的 Spring Bean 以便在对每个请求进行之前。
By extending AbstractHttpSessionApplicationInitializer
, we ensure that the Spring Bean named springSessionRepositoryFilter
is registered with our servlet container for every request before Spring Security’s springSecurityFilterChain
.
Hazelcast Spring Security Sample Application
本节描述如何使用 Hazelcast Spring 安全性示例应用程序。
This section describes how to work with the Hazelcast Spring Security sample application.
Running the Sample Application
您可以获取 源代码 并调用以下命令运行示例:
You can run the sample by obtaining the source code and invoking the following command:
$ ./gradlew :spring-session-sample-javaconfig-hazelcast:tomcatRun
默认情况下,Hazelcast 以嵌入模式在你的应用程序中运行。但是,如果你希望连接到独立实例,则可以通过按照 reference documentation 中的说明进行配置。 |
By default, Hazelcast runs in embedded mode with your application. However, if you want to connect to a standalone instance instead, you can configure it by following the instructions in the reference documentation. |
您现在应该能够访问 [role="bare"][role="bare"]http://localhost:8080/ 中的应用程序。
You should now be able to access the application at [role="bare"]http://localhost:8080/
Exploring the Security Sample Application
您现在可以尝试使用该应用程序。为此,请输入以下内容进行登录:
You can now try using the application. To do so, enter the following to log in:
-
Username user
-
Password password
单击 Login 按钮。您现在应该看到一条消息,指示您已使用之前输入的用户登录。用户的信息存储在 Hazelcast 中,而不是 Tomcat HttpSession
实现中。
Now click the Login button.
You should now see a message indicating that your are logged in with the user entered previously.
The user’s information is stored in Hazelcast rather than Tomcat’s HttpSession
implementation.
How Does It Work?
我们不会使用 Tomcat 的 HttpSession
,而是将这些值持久保存到 Hazelcast 中。Spring 会话将 HttpSession
替换为由 Hazelcast 中的 Map
支持的实现。当 Spring 安全性的 SecurityContextPersistenceFilter
将 SecurityContext
保存到 HttpSession
时,它就被持久保存到 Hazelcast 中。
Instead of using Tomcat’s HttpSession
, we persist the values in Hazelcast.
Spring Session replaces the HttpSession
with an implementation that is backed by a Map
in Hazelcast.
When Spring Security’s SecurityContextPersistenceFilter
saves the SecurityContext
to the HttpSession
, it is then persisted into Hazelcast.
Interacting with the Data Store
您可以使用 a Java client one of the other clients 或 management center 来移除会话。
You can remove the session by using a Java client, one of the other clients, or the management center.
Using the Console
例如,要连接到 Hazelcast 节点后使用管理中心控制台删除会话,请运行以下命令:
For example, to remove the session by using the management center console after connecting to your Hazelcast node, run the following commands:
default> ns spring:session:sessions spring:session:sessions> m.clear
Hazelcast 文档包含有关 the console 的说明。 |
The Hazelcast documentation has instructions for the console. |
或者,您也可以删除显式密钥。在控制台输入以下内容,务必将 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
替换为 SESSION
cookie 的值:
Alternatively, you can also delete the explicit key. Enter the following into the console, being sure to replace 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
with the value of your SESSION
cookie:
spring:session:sessions> m.remove 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
现在,再次访问 [role="bare"][role="bare"]http://localhost:8080/ 中的应用程序,并观察不再对我们进行身份验证的情况。
Now visit the application at [role="bare"]http://localhost:8080/ and observe that we are no longer authenticated.
Using the REST API
如文档中介绍其他客户端的部分所述,Hazelcast 节点提供一个 REST API。
As described in the section of the documentation that cover other clients, there is a REST API provided by the Hazelcast node(s).
例如,您可以删除单独的密钥,如下所示(务必将 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
替换为 SESSION cookie 的值):
For example, you could delete an individual key as follows (being sure to replace 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
with the value of your SESSION cookie):
$ curl -v -X DELETE http://localhost:xxxxx/hazelcast/rest/maps/spring:session:sessions/7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
Hazelcast 节点的端口号会在启动时打印到控制台中。使用端口号替换`xxxxx`。 |
The port number of the Hazelcast node is printed to the console on startup. Replace |
现在,您可以看到,您不再通过该会话进行验证。
Now you can see that you are no longer authenticated with this session.