package com.sheetsj.config; import java.util.List; import org.springframework.context.MessageSource; import org.springframework.context.annotation.*; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.core.MethodParameter; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.*; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.view.tiles3.*; @Configuration @Profile("production") public class WebMvcConfig extends WebMvcConfigurationSupport { private static final String MESSAGE_SOURCE = "/WEB-INF/i18n/messages"; private static final String TILES = "/WEB-INF/tiles/tiles.xml"; private static final String VIEWS = "/WEB-INF/views/**/views.xml"; private static final String RESOURCES_HANDLER = "/resources/"; private static final String RESOURCES_LOCATION = RESOURCES_HANDLER + "**"; @Override public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping(); requestMappingHandlerMapping.setUseSuffixPatternMatch(false); requestMappingHandlerMapping.setUseTrailingSlashMatch(false); return requestMappingHandlerMapping; } @Bean(name = "messageSource") public MessageSource configureMessageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename(MESSAGE_SOURCE); messageSource.setCacheSeconds(5); return messageSource; } @Bean public TilesViewResolver configureTilesViewResolver() { return new TilesViewResolver(); } @Bean public TilesConfigurer configureTilesConfigurer() { TilesConfigurer configurer = new TilesConfigurer(); configurer.setDefinitions(new String[] {TILES, VIEWS}); return configurer; } @Override public Validator getValidator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(configureMessageSource()); return validator; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(RESOURCES_HANDLER).addResourceLocations(RESOURCES_LOCATION); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(new UserDetailsHandlerMethodArgumentResolver()); } // custom argument resolver inner classes private static class UserDetailsHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { public boolean supportsParameter(MethodParameter parameter) { return UserDetails.class.isAssignableFrom(parameter.getParameterType()); } public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Authentication auth = (Authentication) webRequest.getUserPrincipal(); return auth != null && auth.getPrincipal() instanceof UserDetails ? auth.getPrincipal() : null; } } }