Package eu.franzoni.config

Source Code of eu.franzoni.config.WebMvcConfig$UserDetailsHandlerMethodArgumentResolver

package eu.franzoni.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.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesViewResolver;

@Configuration
@ComponentScan(basePackages = { "eu.franzoni.web" })
@ImportResource("classpath:spring-global-method-security.xml")
public class WebMvcConfig extends WebMvcConfigurationSupport {
 
  private static final String MESSAGE_SOURCE = "/WEB-INF/classes/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;
    }
  }
}
TOP

Related Classes of eu.franzoni.config.WebMvcConfig$UserDetailsHandlerMethodArgumentResolver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.