/
MvcConfiguration.java
169 lines (146 loc) · 7.41 KB
/
MvcConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
import org.springframework.session.security.SpringSessionBackedSessionRegistry;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
import org.springframework.session.web.http.HttpSessionIdResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import static alfio.config.Initializer.API_V2_PUBLIC_PATH;
@Configuration(proxyBeanMethods = false)
@ComponentScan(basePackages = {"alfio.controller", "alfio.config"})
@EnableWebMvc
@EnableJdbcHttpSession(maxInactiveIntervalInSeconds = 4 * 60 * 60, tableName = "ALFIO_SPRING_SESSION") //4h
public class MvcConfiguration implements WebMvcConfigurer {
private final Environment environment;
private final String alfioVersion;
private final ObjectMapper objectMapper;
public MvcConfiguration(Environment environment,
@Value("${alfio.version}") String alfioVersion,
ObjectMapper objectMapper) {
this.environment = environment;
this.alfioVersion = alfioVersion;
this.objectMapper = objectMapper;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
boolean isLive = environment.acceptsProfiles(Profiles.of(Initializer.PROFILE_LIVE));
int cacheMinutes = isLive ? 15 : 0;
var defaultCacheControl = CacheControl.maxAge(Duration.ofDays(isLive ? 10 : 0)).mustRevalidate();
registry.addResourceHandler(alfioVersion + "/resources/**")
.addResourceLocations("/resources/")
.setCachePeriod(cacheMinutes * 60)
.setCacheControl(defaultCacheControl);
registry.addResourceHandler("/resources/font/*")
.addResourceLocations("/resources/font/")
.setCachePeriod(cacheMinutes * 60)
.setCacheControl(defaultCacheControl);
registry.addResourceHandler("/resources/images/**")
.addResourceLocations("/resources/images/")
.setCachePeriod(cacheMinutes * 60)
.setCacheControl(defaultCacheControl);
registry.addResourceHandler("/frontend-public/**")
.addResourceLocations("/resources/alfio-public-frontend/")
.setCachePeriod(cacheMinutes * 60)
.setCacheControl(CacheControl.maxAge(Duration.ofDays(60)));
registry.addResourceHandler("/frontend-admin/**")
.addResourceLocations("/resources/alfio-admin-frontend/")
.setCachePeriod(cacheMinutes * 60)
.setCacheControl(CacheControl.maxAge(Duration.ofDays(60)));
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter(objectMapper));
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
converters.add(converter);
}
private MappingJackson2HttpMessageConverter jacksonMessageConverter(ObjectMapper objectMapper) {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper);
return converter;
}
@Bean
public ViewResolver viewResolver() {
var resolver = new UrlBasedViewResolver();
resolver.setViewClass(AbstractUrlBasedView.class);
return resolver;
}
@Bean
public SpringSessionBackedSessionRegistry<?> sessionRegistry(FindByIndexNameSessionRepository<?> sessionRepository) {
return new SpringSessionBackedSessionRegistry<>(sessionRepository);
}
@Bean
public HttpSessionIdResolver httpSessionIdResolver(CookieSerializer cookieSerializer) {
var publicSessionIdResolver = HeaderHttpSessionIdResolver.xAuthToken();
var adminSessionIdResolver = new CookieHttpSessionIdResolver();
adminSessionIdResolver.setCookieSerializer(cookieSerializer);
return new HttpSessionIdResolver() {
@Override
public List<String> resolveSessionIds(HttpServletRequest request) {
return isPublic(request) ? publicSessionIdResolver.resolveSessionIds(request)
: adminSessionIdResolver.resolveSessionIds(request);
}
@Override
public void setSessionId(HttpServletRequest request, HttpServletResponse response, String sessionId) {
if (isPublic(request)) {
publicSessionIdResolver.setSessionId(request, response, sessionId);
} else {
adminSessionIdResolver.setSessionId(request, response, sessionId);
}
}
@Override
public void expireSession(HttpServletRequest request, HttpServletResponse response) {
if (isPublic(request)) {
publicSessionIdResolver.expireSession(request, response);
} else {
adminSessionIdResolver.expireSession(request, response);
}
}
private static boolean isPublic(HttpServletRequest request) {
var requestURI = request.getRequestURI();
return requestURI.startsWith(API_V2_PUBLIC_PATH);
}
};
}
}