(window.webpackJsonp=window.webpackJsonp||[]).push([[251],{677:function(e,t,n){"use strict";n.r(t);var s=n(56),i=Object(s.a)({},(function(){var e=this,t=e.$createElement,n=e._self._c||t;return n("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[n("h1",{attrs:{id:"session-management"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#session-management"}},[e._v("#")]),e._v(" Session Management")]),e._v(" "),n("h2",{attrs:{id:"detecting-timeouts"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#detecting-timeouts"}},[e._v("#")]),e._v(" Detecting Timeouts")]),e._v(" "),n("p",[e._v("You can configure Spring Security to detect the submission of an invalid session ID and redirect the user to an appropriate URL.\nThis is achieved through the "),n("code",[e._v("session-management")]),e._v(" element:")]),e._v(" "),n("p",[e._v("Java")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('@Override\nprotected void configure(HttpSecurity http) throws Exception{\n http\n .sessionManagement(session -> session\n .invalidSessionUrl("/invalidSession.htm")\n );\n}\n')])])]),n("p",[e._v("XML")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n...\n\n\n')])])]),n("p",[e._v("Note that if you use this mechanism to detect session timeouts, it may falsely report an error if the user logs out and then logs back in without closing the browser.\nThis is because the session cookie is not cleared when you invalidate the session and will be resubmitted even if the user has logged out.\nYou may be able to explicitly delete the JSESSIONID cookie on logging out, for example by using the following syntax in the logout handler:")]),e._v(" "),n("p",[e._v("Java")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('@Override\nprotected void configure(HttpSecurity http) throws Exception{\n http\n .logout(logout -> logout\n .deleteCookies("JSESSIONID")\n );\n}\n')])])]),n("p",[e._v("XML")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n\n\n')])])]),n("p",[e._v("Unfortunately this can’t be guaranteed to work with every servlet container, so you will need to test it in your environment")]),e._v(" "),n("table",[n("thead",[n("tr",[n("th"),e._v(" "),n("th",[e._v("If you are running your application behind a proxy, you may also be able to remove the session cookie by configuring the proxy server."),n("br"),e._v("For example, using Apache HTTPD’s mod_headers, the following directive would delete the "),n("code",[e._v("JSESSIONID")]),e._v(" cookie by expiring it in the response to a logout request (assuming the application is deployed under the path "),n("code",[e._v("/tutorial")]),e._v("):"),n("br"),n("br"),n("code",[e._v('

Header always set Set-Cookie "JSESSIONID=;Path=/tutorial;Expires=Thu, 01 Jan 1970 00:00:00 GMT"

')])])])]),e._v(" "),n("tbody")]),e._v(" "),n("h2",{attrs:{id:"concurrent-session-control"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#concurrent-session-control"}},[e._v("#")]),e._v(" Concurrent Session Control")]),e._v(" "),n("p",[e._v("If you wish to place constraints on a single user’s ability to log in to your application, Spring Security supports this out of the box with the following simple additions.\nFirst, you need to add the following listener to your configuration to keep Spring Security updated about session lifecycle events:")]),e._v(" "),n("p",[e._v("Java")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v("@Bean\npublic HttpSessionEventPublisher httpSessionEventPublisher() {\n return new HttpSessionEventPublisher();\n}\n")])])]),n("p",[e._v("XML")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v("\n\n\torg.springframework.security.web.session.HttpSessionEventPublisher\n\n\n")])])]),n("p",[e._v("Then add the following lines to your application context:")]),e._v(" "),n("p",[e._v("Java")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v("@Override\nprotected void configure(HttpSecurity http) throws Exception {\n http\n .sessionManagement(session -> session\n .maximumSessions(1)\n );\n}\n")])])]),n("p",[e._v("XML")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n...\n\n\t\n\n\n')])])]),n("p",[e._v("This will prevent a user from logging in multiple times - a second login will cause the first to be invalidated.\nOften you would prefer to prevent a second login, in which case you can use")]),e._v(" "),n("p",[e._v("Java")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v("@Override\nprotected void configure(HttpSecurity http) throws Exception {\n http\n .sessionManagement(session -> session\n .maximumSessions(1)\n .maxSessionsPreventsLogin(true)\n );\n}\n")])])]),n("p",[e._v("XML")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n\n\t\n\n\n')])])]),n("p",[e._v('The second login will then be rejected.\nBy "rejected", we mean that the user will be sent to the '),n("code",[e._v("authentication-failure-url")]),e._v(' if form-based login is being used.\nIf the second authentication takes place through another non-interactive mechanism, such as "remember-me", an "unauthorized" (401) error will be sent to the client.\nIf instead you want to use an error page, you can add the attribute '),n("code",[e._v("session-authentication-error-url")]),e._v(" to the "),n("code",[e._v("session-management")]),e._v(" element.")]),e._v(" "),n("p",[e._v("If you are using a customized authentication filter for form-based login, then you have to configure concurrent session control support explicitly.\nMore details can be found in the "),n("a",{attrs:{href:"#session-mgmt"}},[e._v("Session Management chapter")]),e._v(".")]),e._v(" "),n("h2",{attrs:{id:"session-fixation-attack-protection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#session-fixation-attack-protection"}},[e._v("#")]),e._v(" Session Fixation Attack Protection")]),e._v(" "),n("p",[n("a",{attrs:{href:"https://en.wikipedia.org/wiki/Session_fixation",target:"_blank",rel:"noopener noreferrer"}},[e._v("Session fixation"),n("OutboundLink")],1),e._v(" attacks are a potential risk where it is possible for a malicious attacker to create a session by accessing a site, then persuade another user to log in with the same session (by sending them a link containing the session identifier as a parameter, for example).\nSpring Security protects against this automatically by creating a new session or otherwise changing the session ID when a user logs in.\nIf you don’t require this protection, or it conflicts with some other requirement, you can control the behavior using the "),n("code",[e._v("session-fixation-protection")]),e._v(" attribute on "),n("code",[e._v("")]),e._v(", which has four options")]),e._v(" "),n("ul",[n("li",[n("p",[n("code",[e._v("none")]),e._v(" - Don’t do anything.\nThe original session will be retained.")])]),e._v(" "),n("li",[n("p",[n("code",[e._v("newSession")]),e._v(' - Create a new "clean" session, without copying the existing session data (Spring Security-related attributes will still be copied).')])]),e._v(" "),n("li",[n("p",[n("code",[e._v("migrateSession")]),e._v(" - Create a new session and copy all existing session attributes to the new session.\nThis is the default in Servlet 3.0 or older containers.")])]),e._v(" "),n("li",[n("p",[n("code",[e._v("changeSessionId")]),e._v(" - Do not create a new session.\nInstead, use the session fixation protection provided by the Servlet container ("),n("code",[e._v("HttpServletRequest#changeSessionId()")]),e._v(").\nThis option is only available in Servlet 3.1 (Java EE 7) and newer containers.\nSpecifying it in older containers will result in an exception.\nThis is the default in Servlet 3.1 and newer containers.")])])]),e._v(" "),n("p",[e._v("When session fixation protection occurs, it results in a "),n("code",[e._v("SessionFixationProtectionEvent")]),e._v(" being published in the application context.\nIf you use "),n("code",[e._v("changeSessionId")]),e._v(", this protection will "),n("em",[e._v("also")]),e._v(" result in any "),n("code",[e._v("javax.servlet.http.HttpSessionIdListener")]),e._v("s being notified, so use caution if your code listens for both events.\nSee the "),n("a",{attrs:{href:"#session-mgmt"}},[e._v("Session Management")]),e._v(" chapter for additional information.")]),e._v(" "),n("h2",{attrs:{id:"sessionmanagementfilter"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#sessionmanagementfilter"}},[e._v("#")]),e._v(" SessionManagementFilter")]),e._v(" "),n("p",[e._v("The "),n("code",[e._v("SessionManagementFilter")]),e._v(" checks the contents of the "),n("code",[e._v("SecurityContextRepository")]),e._v(" against the current contents of the "),n("code",[e._v("SecurityContextHolder")]),e._v(" to determine whether a user has been authenticated during the current request, typically by a non-interactive authentication mechanism, such as pre-authentication or remember-me "),n("sup",{staticClass:"footnote"},[e._v("["),n("a",{staticClass:"footnote",attrs:{id:"_footnoteref_1",href:"#_footnotedef_1",title:"View footnote."}},[e._v("1")]),e._v("]")]),e._v(".\nIf the repository contains a security context, the filter does nothing.\nIf it doesn’t, and the thread-local "),n("code",[e._v("SecurityContext")]),e._v(" contains a (non-anonymous) "),n("code",[e._v("Authentication")]),e._v(" object, the filter assumes they have been authenticated by a previous filter in the stack.\nIt will then invoke the configured "),n("code",[e._v("SessionAuthenticationStrategy")]),e._v(".")]),e._v(" "),n("p",[e._v("If the user is not currently authenticated, the filter will check whether an invalid session ID has been requested (because of a timeout, for example) and will invoke the configured "),n("code",[e._v("InvalidSessionStrategy")]),e._v(", if one is set.\nThe most common behaviour is just to redirect to a fixed URL and this is encapsulated in the standard implementation "),n("code",[e._v("SimpleRedirectInvalidSessionStrategy")]),e._v(".\nThe latter is also used when configuring an invalid session URL through the namespace, "),n("a",{attrs:{href:"#session-mgmt"}},[e._v("as described earlier")]),e._v(".")]),e._v(" "),n("h2",{attrs:{id:"sessionauthenticationstrategy"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#sessionauthenticationstrategy"}},[e._v("#")]),e._v(" SessionAuthenticationStrategy")]),e._v(" "),n("p",[n("code",[e._v("SessionAuthenticationStrategy")]),e._v(" is used by both "),n("code",[e._v("SessionManagementFilter")]),e._v(" and "),n("code",[e._v("AbstractAuthenticationProcessingFilter")]),e._v(", so if you are using a customized form-login class, for example, you will need to inject it into both of these.\nIn this case, a typical configuration, combining the namespace and custom beans might look like this:")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n\n\n\n\n\n\t\n\t...\n\n\n\n')])])]),n("p",[e._v("Note that the use of the default, "),n("code",[e._v("SessionFixationProtectionStrategy")]),e._v(" may cause issues if you are storing beans in the session which implement "),n("code",[e._v("HttpSessionBindingListener")]),e._v(", including Spring session-scoped beans.\nSee the Javadoc for this class for more information.")]),e._v(" "),n("h2",{attrs:{id:"concurrency-control"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#concurrency-control"}},[e._v("#")]),e._v(" Concurrency Control")]),e._v(" "),n("p",[e._v('Spring Security is able to prevent a principal from concurrently authenticating to the same application more than a specified number of times.\nMany ISVs take advantage of this to enforce licensing, whilst network administrators like this feature because it helps prevent people from sharing login names.\nYou can, for example, stop user "Batman" from logging onto the web application from two different sessions.\nYou can either expire their previous login or you can report an error when they try to log in again, preventing the second login.\nNote that if you are using the second approach, a user who has not explicitly logged out (but who has just closed their browser, for example) will not be able to log in again until their original session expires.')]),e._v(" "),n("p",[e._v("Concurrency control is supported by the namespace, so please check the earlier namespace chapter for the simplest configuration.\nSometimes you need to customize things though.")]),e._v(" "),n("p",[e._v("The implementation uses a specialized version of "),n("code",[e._v("SessionAuthenticationStrategy")]),e._v(", called "),n("code",[e._v("ConcurrentSessionControlAuthenticationStrategy")]),e._v(".")]),e._v(" "),n("table",[n("thead",[n("tr",[n("th"),e._v(" "),n("th",[e._v("Previously the concurrent authentication check was made by the "),n("code",[e._v("ProviderManager")]),e._v(", which could be injected with a "),n("code",[e._v("ConcurrentSessionController")]),e._v("."),n("br"),e._v("The latter would check if the user was attempting to exceed the number of permitted sessions."),n("br"),e._v("However, this approach required that an HTTP session be created in advance, which is undesirable."),n("br"),e._v("In Spring Security 3, the user is first authenticated by the "),n("code",[e._v("AuthenticationManager")]),e._v(" and once they are successfully authenticated, a session is created and the check is made whether they are allowed to have another session open.")])])]),e._v(" "),n("tbody")]),e._v(" "),n("p",[e._v("To use concurrent session support, you’ll need to add the following to "),n("code",[e._v("web.xml")]),e._v(":")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v("\n\t\n\torg.springframework.security.web.session.HttpSessionEventPublisher\n\t\n\n")])])]),n("p",[e._v("In addition, you will need to add the "),n("code",[e._v("ConcurrentSessionFilter")]),e._v(" to your "),n("code",[e._v("FilterChainProxy")]),e._v(".\nThe "),n("code",[e._v("ConcurrentSessionFilter")]),e._v(" requires two constructor arguments, "),n("code",[e._v("sessionRegistry")]),e._v(", which generally points to an instance of "),n("code",[e._v("SessionRegistryImpl")]),e._v(", and "),n("code",[e._v("sessionInformationExpiredStrategy")]),e._v(", which defines the strategy to apply when a session has expired.\nA configuration using the namespace to create the "),n("code",[e._v("FilterChainProxy")]),e._v(" and other default beans might look like this:")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\t\n\t\t\n\t\t\n\t\n\t\n\t\n\t\n\t\t\n\t\n\t\n\n\n\n\n')])])]),n("p",[e._v("Adding the listener to "),n("code",[e._v("web.xml")]),e._v(" causes an "),n("code",[e._v("ApplicationEvent")]),e._v(" to be published to the Spring "),n("code",[e._v("ApplicationContext")]),e._v(" every time a "),n("code",[e._v("HttpSession")]),e._v(" commences or ends.\nThis is critical, as it allows the "),n("code",[e._v("SessionRegistryImpl")]),e._v(" to be notified when a session ends.\nWithout it, a user will never be able to log back in again once they have exceeded their session allowance, even if they log out of another session or it times out.")]),e._v(" "),n("h3",{attrs:{id:"querying-the-sessionregistry-for-currently-authenticated-users-and-their-sessions"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#querying-the-sessionregistry-for-currently-authenticated-users-and-their-sessions"}},[e._v("#")]),e._v(" Querying the SessionRegistry for currently authenticated users and their sessions")]),e._v(" "),n("p",[e._v("Setting up concurrency-control, either through the namespace or using plain beans has the useful side effect of providing you with a reference to the "),n("code",[e._v("SessionRegistry")]),e._v(" which you can use directly within your application, so even if you don’t want to restrict the number of sessions a user may have, it may be worth setting up the infrastructure anyway.\nYou can set the "),n("code",[e._v("maximumSession")]),e._v(" property to -1 to allow unlimited sessions.\nIf you’re using the namespace, you can set an alias for the internally-created "),n("code",[e._v("SessionRegistry")]),e._v(" using the "),n("code",[e._v("session-registry-alias")]),e._v(" attribute, providing a reference which you can inject into your own beans.")]),e._v(" "),n("p",[e._v("The "),n("code",[e._v("getAllPrincipals()")]),e._v(" method supplies you with a list of the currently authenticated users.\nYou can list a user’s sessions by calling the "),n("code",[e._v("getAllSessions(Object principal, boolean includeExpiredSessions)")]),e._v(" method, which returns a list of "),n("code",[e._v("SessionInformation")]),e._v(" objects.\nYou can also expire a user’s session by calling "),n("code",[e._v("expireNow()")]),e._v(" on a "),n("code",[e._v("SessionInformation")]),e._v(" instance.\nWhen the user returns to the application, they will be prevented from proceeding.\nYou may find these methods useful in an administration application, for example.\nHave a look at the Javadoc for more information.")]),e._v(" "),n("hr"),e._v(" "),n("p",[n("a",{attrs:{href:"#_footnoteref_1"}},[e._v("1")]),e._v(". Authentication by mechanisms which perform a redirect after authenticating (such as form-login) will not be detected by "),n("code",[e._v("SessionManagementFilter")]),e._v(", as the filter will not be invoked during the authenticating request. Session-management functionality has to be handled separately in these cases.")]),e._v(" "),n("p",[n("RouterLink",{attrs:{to:"/en/spring-security/passwords/ldap.html"}},[e._v("LDAP")]),n("RouterLink",{attrs:{to:"/en/spring-security/rememberme.html"}},[e._v("Remember Me")])],1)])}),[],!1,null,null,null);t.default=i.exports}}]);