001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.session;
018
019import java.time.Duration;
020import java.time.temporal.ChronoUnit;
021import java.util.Arrays;
022import java.util.HashSet;
023import java.util.Set;
024
025import javax.annotation.PostConstruct;
026
027import org.springframework.beans.factory.ObjectProvider;
028import org.springframework.boot.autoconfigure.web.ServerProperties;
029import org.springframework.boot.context.properties.ConfigurationProperties;
030import org.springframework.boot.convert.DurationUnit;
031import org.springframework.boot.web.servlet.DispatcherType;
032import org.springframework.boot.web.servlet.server.Session;
033import org.springframework.session.web.http.SessionRepositoryFilter;
034
035/**
036 * Configuration properties for Spring Session.
037 *
038 * @author Tommy Ludwig
039 * @author Stephane Nicoll
040 * @author Vedran Pavic
041 * @since 1.4.0
042 */
043@ConfigurationProperties(prefix = "spring.session")
044public class SessionProperties {
045
046        /**
047         * Session store type.
048         */
049        private StoreType storeType;
050
051        /**
052         * Session timeout. If a duration suffix is not specified, seconds will be used.
053         */
054        @DurationUnit(ChronoUnit.SECONDS)
055        private Duration timeout;
056
057        private Servlet servlet = new Servlet();
058
059        private final ServerProperties serverProperties;
060
061        public SessionProperties(ObjectProvider<ServerProperties> serverProperties) {
062                this.serverProperties = serverProperties.getIfUnique();
063        }
064
065        @PostConstruct
066        public void checkSessionTimeout() {
067                if (this.timeout == null && this.serverProperties != null) {
068                        this.timeout = this.serverProperties.getServlet().getSession().getTimeout();
069                }
070        }
071
072        public StoreType getStoreType() {
073                return this.storeType;
074        }
075
076        public void setStoreType(StoreType storeType) {
077                this.storeType = storeType;
078        }
079
080        /**
081         * Return the session timeout.
082         * @return the session timeout
083         * @see Session#getTimeout()
084         */
085        public Duration getTimeout() {
086                return this.timeout;
087        }
088
089        public void setTimeout(Duration timeout) {
090                this.timeout = timeout;
091        }
092
093        public Servlet getServlet() {
094                return this.servlet;
095        }
096
097        public void setServlet(Servlet servlet) {
098                this.servlet = servlet;
099        }
100
101        /**
102         * Servlet-related properties.
103         */
104        public static class Servlet {
105
106                /**
107                 * Session repository filter order.
108                 */
109                private int filterOrder = SessionRepositoryFilter.DEFAULT_ORDER;
110
111                /**
112                 * Session repository filter dispatcher types.
113                 */
114                private Set<DispatcherType> filterDispatcherTypes = new HashSet<>(Arrays.asList(
115                                DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST));
116
117                public int getFilterOrder() {
118                        return this.filterOrder;
119                }
120
121                public void setFilterOrder(int filterOrder) {
122                        this.filterOrder = filterOrder;
123                }
124
125                public Set<DispatcherType> getFilterDispatcherTypes() {
126                        return this.filterDispatcherTypes;
127                }
128
129                public void setFilterDispatcherTypes(Set<DispatcherType> filterDispatcherTypes) {
130                        this.filterDispatcherTypes = filterDispatcherTypes;
131                }
132
133        }
134
135}