3. Spring session-uses Hazelcast httpsession__session

Source: Internet
Author: User
Tags sessions hazelcast

4.4. Use of Hazelcast httpsession

You can enable spring session by adding a servlet filter before using any of the features of HttpSession.

This section describes how to use Hazelcast support httpsession based on Java configuration.

The Hazelcast Spring sample provides an executable sample that provides an example of how to consolidate spring session and HttpSession based on a Java configuration. You can read some of the basic steps below, but when you integrate with your application, it is recommended that you follow the detailed Hazelcast Spring Reference Guide.

4.4.1. Spring configuration

After adding the necessary dependencies, we need to create our own spring configuration, and the spring configuration is responsible for creating a servlet filter that replaces httpsession through an implementation that uses the spring session support. Add the following spring configuration:

 @EnableHazelcastHttpSession @Configuration public class Hazelcasthttpsessionconfig {@Bean
                Public Hazelcastinstance hazelcastinstance () {mapattributeconfig attributeconfig = new Mapattributeconfig () . SetName (Hazelcastsessionrepository.principal_name_attribute). Setextractor (Principalnameext

        Ractor.class.getName ());

        Config config = new config (); Config.getmapconfig ("Spring:session:sessions"). Addmapattributeconfig (Attributeconfig). A 
        Ddmapindexconfig (New Mapindexconfig (Hazelcastsessionrepository.principal_name_attribute, false)); 
    return hazelcast.newhazelcastinstance (config); }
}
@EnableHazelcastHttpSession Note Creates a bean (filter) named Springsessionrepositoryfilter that implements filter, which is responsible for using the spring The session supports an implementation that replaces HttpSession, which is supported by Hazelcast, and requires registration of the appropriate valueextractor in order to support principal name index retrieval sessions. The Spring session provides principalnameextractor for this purpose. Create a hazelcastinstance link spring session and Hazelcast, by default, an embedded Hazelcast default instance is started and is linked by application. For more information on configuring Hazelcast, refer to the documentation

4.4.2. servlet Container Initialization

The spring configuration file creates a bean,springsessionrepositoryfilter named Springsessionrepositoryfilter that implements filter is responsible for using a support spring The personalization implementation of the session replaces HttpSession.

In order for our filter to play its magic, Spring needs to load our Sessionconfig class. Since our application has loaded the spring configuration with the Securityinitializer class, we can simply add our Sessionconfig class.

Src/main/java/sample/securityinitializer.java public
class Securityinitializer extends Abstractsecuritywebapplicationinitializer {public

    Securityinitializer () {
        super (Securityconfig.class, Sessionconfig.class);
    }

Finally, we need to make sure that every request for a servlet container (such as Tomcat) uses Springsessionrepositoryfilter, and it is important that spring The springsessionrepositoryfilter of the session must be invoked before spring security ' s springsecurityfilterchain. This will ensure that the httpsession used by spring security is also supported by the spring session. Fortunately, the Spring session provides a very effective class called Abstracthttpsessionapplicationinitializer, which makes it very easy to implement these features. Take a look at the following example:

Src/main/java/sample/initializer.java public
class initializer extends Abstracthttpsessionapplicationinitializer {

}

We don't care about the naming of our own class (initializer), the most important thing is to inherit Abstracthttpsessionapplicationinitializer.

After inheriting Abstracthttpsessionapplicationinitializer, we can make sure that the spring bean named Springsessionrepositoryfilter is registered in the servlet container. And for each request, he will be in the spring security ' s Springsecurityfilterchain filter chain before all the filters.

Original: https://docs.spring.io/spring-session/docs/2.0.0.M4/reference/html5/#httpsession-hazelcast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.