|
| 1 | +package org.apereo.cas.config; |
| 2 | + |
| 3 | +import org.apereo.cas.configuration.CasConfigurationProperties; |
| 4 | + |
| 5 | +import lombok.val; |
| 6 | + |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | +import org.springframework.beans.factory.ObjectProvider; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 11 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 12 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 13 | +import org.springframework.cloud.context.config.annotation.RefreshScope; |
| 14 | +import org.springframework.context.annotation.Bean; |
| 15 | +import org.springframework.context.annotation.Configuration; |
| 16 | +import org.springframework.web.servlet.LocaleResolver; |
| 17 | +import org.springframework.web.servlet.ModelAndView; |
| 18 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| 19 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 20 | +import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; |
| 21 | +import org.springframework.web.servlet.i18n.CookieLocaleResolver; |
| 22 | +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; |
| 23 | +import org.springframework.web.servlet.mvc.Controller; |
| 24 | +import org.springframework.web.servlet.mvc.ParameterizableViewController; |
| 25 | +import org.springframework.web.servlet.mvc.UrlFilenameViewController; |
| 26 | +import org.springframework.web.servlet.theme.ThemeChangeInterceptor; |
| 27 | +import org.springframework.web.servlet.view.RedirectView; |
| 28 | + |
| 29 | +import javax.servlet.http.HttpServletRequest; |
| 30 | +import javax.servlet.http.HttpServletResponse; |
| 31 | + |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Locale; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +/** |
| 37 | + * This is {@link CasWebAppConfiguration}. |
| 38 | + * |
| 39 | + * @author Misagh Moayyed |
| 40 | + * @since 5.0.0 |
| 41 | + */ |
| 42 | +@Configuration(value = "casWebAppConfiguration", proxyBeanMethods = false) |
| 43 | +@EnableConfigurationProperties(CasConfigurationProperties.class) |
| 44 | +public class CasWebAppConfiguration implements WebMvcConfigurer { |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private CasConfigurationProperties casProperties; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + @Qualifier("localeChangeInterceptor") |
| 51 | + private ObjectProvider<LocaleChangeInterceptor> localeChangeInterceptor; |
| 52 | + |
| 53 | + @RefreshScope |
| 54 | + @Bean |
| 55 | + public ThemeChangeInterceptor themeChangeInterceptor() { |
| 56 | + val bean = new ThemeChangeInterceptor(); |
| 57 | + bean.setParamName(casProperties.getTheme().getParamName()); |
| 58 | + return bean; |
| 59 | + } |
| 60 | + |
| 61 | + @ConditionalOnMissingBean(name = "localeResolver") |
| 62 | + @Bean |
| 63 | + public LocaleResolver localeResolver() { |
| 64 | + val localeProps = casProperties.getLocale(); |
| 65 | + val localeCookie = localeProps.getCookie(); |
| 66 | + |
| 67 | + val resolver = new CookieLocaleResolver() { |
| 68 | + @Override |
| 69 | + protected Locale determineDefaultLocale(final HttpServletRequest request) { |
| 70 | + val locale = request.getLocale(); |
| 71 | + if (StringUtils.isBlank(localeProps.getDefaultValue()) |
| 72 | + || !locale.getLanguage().equals(localeProps.getDefaultValue())) { |
| 73 | + return locale; |
| 74 | + } |
| 75 | + return new Locale(localeProps.getDefaultValue()); |
| 76 | + } |
| 77 | + }; |
| 78 | + resolver.setCookieDomain(localeCookie.getDomain()); |
| 79 | + resolver.setCookiePath(StringUtils.defaultIfBlank(localeCookie.getPath(), CookieLocaleResolver.DEFAULT_COOKIE_PATH)); |
| 80 | + resolver.setCookieHttpOnly(localeCookie.isHttpOnly()); |
| 81 | + resolver.setCookieSecure(localeCookie.isSecure()); |
| 82 | + resolver.setCookieName(StringUtils.defaultIfBlank(localeCookie.getName(), CookieLocaleResolver.DEFAULT_COOKIE_NAME)); |
| 83 | + resolver.setCookieMaxAge(localeCookie.getMaxAge()); |
| 84 | + resolver.setLanguageTagCompliant(true); |
| 85 | + resolver.setRejectInvalidCookies(true); |
| 86 | + return resolver; |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + protected UrlFilenameViewController passThroughController() { |
| 91 | + return new UrlFilenameViewController(); |
| 92 | + } |
| 93 | + |
| 94 | + @Bean |
| 95 | + protected Controller rootController() { |
| 96 | + return new ParameterizableViewController() { |
| 97 | + @Override |
| 98 | + protected ModelAndView handleRequestInternal(final HttpServletRequest request, |
| 99 | + final HttpServletResponse response) { |
| 100 | + val queryString = request.getQueryString(); |
| 101 | + val url = request.getContextPath() + "/login" |
| 102 | + + Optional.ofNullable(queryString).map(string -> '?' + string).orElse(StringUtils.EMPTY); |
| 103 | + return new ModelAndView(new RedirectView(response.encodeURL(url))); |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + @Bean |
| 109 | + public SimpleUrlHandlerMapping handlerMapping() { |
| 110 | + val mapping = new SimpleUrlHandlerMapping(); |
| 111 | + |
| 112 | + val root = rootController(); |
| 113 | + mapping.setOrder(1); |
| 114 | + mapping.setAlwaysUseFullPath(true); |
| 115 | + mapping.setRootHandler(root); |
| 116 | + val urls = new HashMap<String, Object>(); |
| 117 | + urls.put("/", root); |
| 118 | + |
| 119 | + mapping.setUrlMap(urls); |
| 120 | + return mapping; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void addInterceptors(final InterceptorRegistry registry) { |
| 125 | + registry.addInterceptor(localeChangeInterceptor.getObject()) |
| 126 | + .addPathPatterns("/**"); |
| 127 | + } |
| 128 | +} |
0 commit comments