Spring在创建messageSource后停止提供静态资源

问题描述 投票:2回答:1

在Spring webMvcConfigurer中创建了messageSource bean后,我的应用程序停止提供http状态为404的静态资源(images / css / js),但是所有文件都有正确的路径(我完全确定messageSource中的某个地方有错误,因为没有这个bean服务可以正常工作)。也许你知道如何解决这个问题?

我的WebMvcConfigurer Impl:

@Configuration
@Import({dbConfig.class})
@EnableWebMvc
@EnableSpringDataWebSupport
@ComponentScan("my.shop.common.fliPusto")
@PropertySource("classpath:properties/resources.properties")
public class mvcConfig implements WebMvcConfigurer {

    @Value("${uploadBook.path}")
    private String bookPath;

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:/" + bookPath + "/images/");
        registry.addResourceHandler("/scripts/**")
                .addResourceLocations("file:/" + bookPath + "/scripts/");
        registry.addResourceHandler("/styles/**")
                .addResourceLocations("file:/" + bookPath + "/styles/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("classpath:freemarkerTemplates/");
        configurer.setDefaultEncoding("UTF-8");
        return configurer;
    }

    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
        freeMarkerViewResolver.setCache(false);
        freeMarkerViewResolver.setPrefix("");
        freeMarkerViewResolver.setSuffix(".ftl");
        freeMarkerViewResolver.setContentType("text/html; charset=UTF-8");

        return freeMarkerViewResolver;
    }

    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("/messages/locales");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}

我的securityConfig:

@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan(value = "my.shop.common.fliPusto.services")
public class securityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(4);
    }

    @Bean
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceEncoding(true);
        encodingFilter.setForceRequestEncoding(true);
        encodingFilter.setForceResponseEncoding(true);

        return encodingFilter;
    }

    @Autowired
    void setAuthManagerBuilder(AuthenticationManagerBuilder authenticationManagerBuilder, PasswordEncoder passwordEncoder, UserDetailsService userDetailsService) {
        try {
            authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/", "/registration", "/test/*", "/book", "/tag/**", "/user/profile/{profile}", "/images/**", "/scripts/**", "/styles/**").permitAll()
                .antMatchers("/admin/**").hasAuthority("MODERATOR")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .logoutSuccessUrl("/")
                .permitAll()
                .and()
                .requiresChannel()
                .anyRequest()
                .requiresSecure();

        http.addFilterBefore(characterEncodingFilter(), CsrfFilter.
    }
}
spring spring-mvc
1个回答
0
投票

我通过将messagesource添加到另一个Configuration类并将此类作为ServletConfigClass添加到dispatherServlet来解决这个问题

另一个配置:

@Configuration
public class OtherConfig {
    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new 
ResourceBundleMessageSource();
        messageSource.setBasename("/messages/locales");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}

Servlet类:

public class servlet extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {mvcConfig.class, securityConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {OtherConfig.class};      <-------------There
    }

    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[]{new charsetFilter()};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(new MultipartConfigElement(""));
    }
}