无法解析多部分servlet请求; .IllegalStateException:无法处理部件,因为未提供多部件配置

时间:2016-04-22 15:09:35

标签: java eclipse spring-mvc servlets spring-security

我无法将图像上传到服务器。我已经在这里尝试了许多相关问题的解决方案,但没有为我工作。这是我的文件:

SecurityApplicationInitializer:

        package com.App.config;

        import javax.servlet.ServletContext;

        import org.springframework.context.annotation.Configuration;
        import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
        import org.springframework.web.multipart.support.MultipartFilter;

        @Configuration
        public class AppSecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

            @Override
            protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
                insertFilters(servletContext, new MultipartFilter());
            }

        }

WebMVCConfigurerAdapter:

    package com.App.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.web.multipart.commons.CommonsMultipartResolver;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;



     //mvc:annotation-driven
    @Configuration

    @ComponentScan(basePackages ={ "com.App.controller", "com.App.mapper",
            "com.App.JDBC","com.App.model"      
    })//, excludeFilters = { 
    //@Filter(type = FilterType.ANNOTATION, value = Configuration.class) })

    public class AppConfig extends WebMvcConfigurerAdapter {
        int IMAGE_MAX_UPLOAD_SIZE = 8000;

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        }
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

        }

      @Bean
          public CommonsMultipartResolver multipartResolver() {
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
            multipartResolver.setMaxUploadSize(IMAGE_MAX_UPLOAD_SIZE);
            multipartResolver.setDefaultEncoding("utf-8");
            return multipartResolver;
          }


        @Bean
        public InternalResourceViewResolver viewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setViewClass(JstlView.class);
            viewResolver.setPrefix("/WEB-INF/views/jsp/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }

        /* @Bean 
         public UserRepositoryUserDetailsService getUserRepositoryUserDetailsService(UserRepository userRepository){

            UserRepositoryUserDetailsService bean = new UserRepositoryUserDetailsService(userRepository);
             return bean;
         }*/
        @Bean
        public DriverManagerDataSource getDatasource(){
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setPassword("....");
            dataSource.setUrl("....");
            dataSource.setUsername("root");
            return dataSource;
        }
        ...
    }

ServletInit:

    package com.App.config;

    import javax.servlet.Filter;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.filter.HiddenHttpMethodFilter;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    @EnableWebMvc
    @Configuration
    public class AppWebInit extends
            AbstractAnnotationConfigDispatcherServletInitializer {


        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class[] { AppConfig.class };
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/","/logout","/403", "/login" };
        }

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { RootConfiguration.class };
        }
       @Override
        protected Filter[] getServletFilters() {
            return new Filter[] { new HiddenHttpMethodFilter() };
        }
    }

RootConfig:

    package com.App.config;

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;

    /**

     *
     * @author E
     *
     */
    @Configuration
    @ComponentScan
    public class RootConfiguration {

    }

SecurityConfig:

    package com.App.config;

    import javax.sql.DataSource;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.web.csrf.CsrfTokenRepository;
    import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled=true)
    //@ComponentScan(basePackageClasses = UserRepositoryUserDetailsService.class)
    public class SecurityConfigure extends WebSecurityConfigurerAdapter{

        //@Autowired
        //private UserDetailsService userDetailsService;


         @Autowired
        private CsrfTokenRepository csrfTokenRepository() 
        { 
            HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
            repository.setSessionAttributeName("_csrf");
            return repository; 
        }
         @Override
        protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf()
                .csrfTokenRepository(csrfTokenRepository())
                .and()
            .authorizeRequests()
                .antMatchers("/resources/**", "/signuPerson", "/login", "/", "/logout","viewBuyOrders").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
              //.loginProcessingUrl("/processLogin")
                //.failureUrl("login?error")
                //
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/logout")
               // .addLogoutHandler(new SecurityContextLogoutHandler())
               // .logoutSuccessUrl("/403")
                 //
                .permitAll();

        }

        @Autowired
        public void configureGlobal(DataSource dataSource , AuthenticationManagerBuilder auth) throws Exception {
        auth
            .jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery("select login_username,  login_password, true from login where login_username = ?")
            .authoritiesByUsernameQuery("select login_username, 'ROLE_USER' from login where login_username = ? ");

        }

    }

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>App</groupId>
      <artifactId>App</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
          <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
       <dependencies>
       <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
       <version>1.3.1</version> 
    </dependency>
           <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
       <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
                <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>3.0.5.RELEASE</version>
         </dependency>
        <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.2.RELEASE</version>
       </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency> 
       <groupId>org.springframework.security</groupId>
       <artifactId>spring-security-web</artifactId>
       <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- 
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
                <version>1.3.0.RELEASE</version>
     </dependency>-->
             <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.3.RELEASE</version>
      </dependency>

          </dependencies>

    </project>

的pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>App</groupId>
      <artifactId>App</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
          <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
       <dependencies>
       <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
       <version>1.3.1</version> 
    </dependency>
           <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
       <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
                <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>3.0.5.RELEASE</version>
         </dependency>
        <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.2.RELEASE</version>
       </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency> 
       <groupId>org.springframework.security</groupId>
       <artifactId>spring-security-web</artifactId>
       <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- 
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
                <version>1.3.0.RELEASE</version>
     </dependency>-->
             <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.3.RELEASE</version>
      </dependency>

          </dependencies>

    </project>

帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

我认为MultipartResolver bean名称错误。它应该命名为filterMultipartResolver。所以在你的Java配置中:

@Bean public CommonsMultipartResolver filterMultipartResolver() { ... }

选项是跳过commons-fileupload并使用Servlet 3 API中的多部分支持。您可以通过添加WebAppInit类来启用它:

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
    registration.setMultipartConfig(new MultipartConfigElement(LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD));
}

您选择的常数。