|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.web.servlet |
| 18 | + |
| 19 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity |
| 20 | +import org.springframework.security.config.annotation.web.configurers.RememberMeConfigurer |
| 21 | +import org.springframework.security.core.userdetails.UserDetailsService |
| 22 | +import org.springframework.security.web.authentication.AuthenticationSuccessHandler |
| 23 | +import org.springframework.security.web.authentication.RememberMeServices |
| 24 | +import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository |
| 25 | + |
| 26 | +/** |
| 27 | + * A Kotlin DSL to configure [HttpSecurity] Remember me using idiomatic Kotlin code. |
| 28 | + * |
| 29 | + * @author Ivan Pavlov |
| 30 | + * @property authenticationSuccessHandler the [AuthenticationSuccessHandler] used after |
| 31 | + * authentication success |
| 32 | + * @property key the key to identify tokens |
| 33 | + * @property rememberMeServices the [RememberMeServices] to use |
| 34 | + * @property rememberMeParameter the HTTP parameter used to indicate to remember |
| 35 | + * the user at time of login. Defaults to 'remember-me' |
| 36 | + * @property rememberMeCookieName the name of cookie which store the token for |
| 37 | + * remember me authentication. Defaults to 'remember-me' |
| 38 | + * @property rememberMeCookieDomain the domain name within which the remember me cookie |
| 39 | + * is visible |
| 40 | + * @property tokenRepository the [PersistentTokenRepository] to use. Defaults to |
| 41 | + * [org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices] instead |
| 42 | + * @property userDetailsService the [UserDetailsService] used to look up the UserDetails |
| 43 | + * when a remember me token is valid |
| 44 | + * @property tokenValiditySeconds how long (in seconds) a token is valid for. |
| 45 | + * Defaults to 2 weeks |
| 46 | + * @property useSecureCookie whether the cookie should be flagged as secure or not |
| 47 | + * @property alwaysRemember whether the cookie should always be created even if |
| 48 | + * the remember-me parameter is not set. Defaults to `false` |
| 49 | + */ |
| 50 | +@SecurityMarker |
| 51 | +class RememberMeDsl { |
| 52 | + var authenticationSuccessHandler: AuthenticationSuccessHandler? = null |
| 53 | + var key: String? = null |
| 54 | + var rememberMeServices: RememberMeServices? = null |
| 55 | + var rememberMeParameter: String? = null |
| 56 | + var rememberMeCookieName: String? = null |
| 57 | + var rememberMeCookieDomain: String? = null |
| 58 | + var tokenRepository: PersistentTokenRepository? = null |
| 59 | + var userDetailsService: UserDetailsService? = null |
| 60 | + var tokenValiditySeconds: Int? = null |
| 61 | + var useSecureCookie: Boolean? = null |
| 62 | + var alwaysRemember: Boolean? = null |
| 63 | + |
| 64 | + internal fun get(): (RememberMeConfigurer<HttpSecurity>) -> Unit { |
| 65 | + return { rememberMe -> |
| 66 | + authenticationSuccessHandler?.also { rememberMe.authenticationSuccessHandler(authenticationSuccessHandler) } |
| 67 | + key?.also { rememberMe.key(key) } |
| 68 | + rememberMeServices?.also { rememberMe.rememberMeServices(rememberMeServices) } |
| 69 | + rememberMeParameter?.also { rememberMe.rememberMeParameter(rememberMeParameter) } |
| 70 | + rememberMeCookieName?.also { rememberMe.rememberMeCookieName(rememberMeCookieName) } |
| 71 | + rememberMeCookieDomain?.also { rememberMe.rememberMeCookieDomain(rememberMeCookieDomain) } |
| 72 | + tokenRepository?.also { rememberMe.tokenRepository(tokenRepository) } |
| 73 | + userDetailsService?.also { rememberMe.userDetailsService(userDetailsService) } |
| 74 | + tokenValiditySeconds?.also { rememberMe.tokenValiditySeconds(tokenValiditySeconds!!) } |
| 75 | + useSecureCookie?.also { rememberMe.useSecureCookie(useSecureCookie!!) } |
| 76 | + alwaysRemember?.also { rememberMe.alwaysRemember(alwaysRemember!!) } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments