-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCasWebAppConfiguration.java
More file actions
186 lines (165 loc) · 7.32 KB
/
CasWebAppConfiguration.java
File metadata and controls
186 lines (165 loc) · 7.32 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package org.apereo.cas.config;
import org.apache.http.HttpStatus;
import org.apereo.cas.configuration.CasConfigurationProperties;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.mvc.UrlFilenameViewController;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
/**
* This is {@link CasWebAppConfiguration}.
*
* @author Misagh Moayyed
* @author Longze Chen
* @since 5.0.0
*/
@Configuration(value = "casWebAppConfiguration", proxyBeanMethods = false)
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class CasWebAppConfiguration implements WebMvcConfigurer {
private static final List<Integer> HTTP_ERROR_WITH_TEMPLATES = List.of(
HttpStatus.SC_UNAUTHORIZED,
HttpStatus.SC_FORBIDDEN,
HttpStatus.SC_NOT_FOUND,
HttpStatus.SC_METHOD_NOT_ALLOWED,
HttpStatus.SC_LOCKED,
HttpStatus.SC_INTERNAL_SERVER_ERROR
);
@Autowired
private CasConfigurationProperties casProperties;
@Autowired
@Qualifier("localeChangeInterceptor")
private ObjectProvider<LocaleChangeInterceptor> localeChangeInterceptor;
@RefreshScope
@Bean
public ThemeChangeInterceptor themeChangeInterceptor() {
val bean = new ThemeChangeInterceptor();
bean.setParamName(casProperties.getTheme().getParamName());
return bean;
}
@ConditionalOnMissingBean(name = "localeResolver")
@Bean
public LocaleResolver localeResolver() {
val localeProps = casProperties.getLocale();
val localeCookie = localeProps.getCookie();
val resolver = new CookieLocaleResolver() {
@Override
protected Locale determineDefaultLocale(final HttpServletRequest request) {
val locale = request.getLocale();
if (StringUtils.isBlank(localeProps.getDefaultValue())
|| !locale.getLanguage().equals(localeProps.getDefaultValue())) {
return locale;
}
return new Locale(localeProps.getDefaultValue());
}
};
resolver.setCookieDomain(localeCookie.getDomain());
resolver.setCookiePath(StringUtils.defaultIfBlank(localeCookie.getPath(), CookieLocaleResolver.DEFAULT_COOKIE_PATH));
resolver.setCookieHttpOnly(localeCookie.isHttpOnly());
resolver.setCookieSecure(localeCookie.isSecure());
resolver.setCookieName(StringUtils.defaultIfBlank(localeCookie.getName(), CookieLocaleResolver.DEFAULT_COOKIE_NAME));
resolver.setCookieMaxAge(localeCookie.getMaxAge());
resolver.setLanguageTagCompliant(true);
resolver.setRejectInvalidCookies(true);
return resolver;
}
@Bean
protected UrlFilenameViewController passThroughController() {
return new UrlFilenameViewController();
}
@Bean
protected Controller rootController() {
return new ParameterizableViewController() {
@Override
protected ModelAndView handleRequestInternal(
final HttpServletRequest request,
final HttpServletResponse response
) {
val queryString = request.getQueryString();
val url = request.getContextPath() + "/login"
+ Optional.ofNullable(queryString).map(string -> '?' + string).orElse(StringUtils.EMPTY);
// OSF CAS Customization: use absolute URL to temporarily solve the ingress issue on staging1 and prod.
val fullUrl = casProperties.getServer().getName() + url;
return new ModelAndView(new RedirectView(response.encodeURL(fullUrl)));
}
};
}
/**
* OSF CAS Customization: implement a new controller to support testing error pages with templates in
* "resources/templates/error/" (401, 403, 404, 405 and 423) and with the CAS unavailable template of
* "templates/error.html" (500).
*
* @return {@code null}
*/
@Bean
protected Controller forceHttpErrorController() {
return new ParameterizableViewController() {
@Override
protected ModelAndView handleRequestInternal(
final HttpServletRequest request,
final HttpServletResponse response
) throws IOException {
// TODO: disable this for production environment
var errorCodeString = request.getParameter("code");
if (StringUtils.isNotBlank(errorCodeString)) {
try {
var errorCode = Integer.parseInt(errorCodeString);
if (HTTP_ERROR_WITH_TEMPLATES.contains(errorCode)) {
response.sendError(errorCode);
return null;
}
} catch (NumberFormatException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
};
}
@Bean
public SimpleUrlHandlerMapping handlerMapping() {
val mapping = new SimpleUrlHandlerMapping();
val root = rootController();
mapping.setOrder(1);
mapping.setAlwaysUseFullPath(true);
mapping.setRootHandler(root);
val urls = new HashMap<String, Object>();
urls.put("/", root);
if (casProperties.getServer().getDevMode().isAllowForceHttpError()) {
val forceHttpError = forceHttpErrorController();
urls.put("/forceHttpError", forceHttpError);
}
mapping.setUrlMap(urls);
return mapping;
}
@Override
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor.getObject())
.addPathPatterns("/**");
}
}