2016-10-05 2 views
1

Ich habe einen REST Upload-Service mit Sppring boot:SpringBoot aktiviert @CrosOrigin nicht. Angular2 + Webpack Client

@CrossOrigin 
@RestController 
@RequestMapping("/upload") 
public class FileUploadHandler { 

    @PostMapping("/doUpload") 
    public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { 

     if (!file.isEmpty()) { 
      try { 
       byte[] bytes = file.getBytes(); 
       BufferedOutputStream stream = new BufferedOutputStream(
         new FileOutputStream(new File(file.getName() + "-uploaded"))); 
       stream.write(bytes); 
       stream.close(); 
       return "You successfully uploaded " + file.getName() + " into " + file.getName() + "-uploaded !"; 
      } catch (Exception e) { 
       return "You failed to upload " + file.getName() + " => " + e.getMessage(); 
      } 
     } else { 

      redirectAttributes.addFlashAttribute("message", 
        "You successfully uploaded " + file.getOriginalFilename() + "!"); 

      return "Some error... "; 
     } 
    } 

} 

Ich verwende eine Angular2 + webpack Client den Dienst zu verbrauchen. Problem ist, dass ich einen CROS-Block bekomme, wenn ich versuche, auf den Dienst zuzugreifen.

XMLHttpRequest cannot load http://localhost:8080/kti-cms-spring/upload/doUpload. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ac1068:8080' is therefore not allowed access. 

Ich benutze Glassfish4 als Anwendungsserver.

Meine Konfiguration:

@Configuration 
@ComponentScan("de.awinta.kti.cms") 
public class MainConfig { 

    @Bean 
    public FilterRegistrationBean corsFilter() { 

     UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
     CorsConfiguration config = new CorsConfiguration(); 
     config.setAllowCredentials(true); 
     // likely you should limit this to specific origins 
     config.addAllowedOrigin("*"); 
     config.addAllowedHeader("*"); 
     config.addAllowedMethod("*"); 
     config.setAllowCredentials(Boolean.TRUE); 
     source.registerCorsConfiguration("/**", config); 
     FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); 
     bean.setOrder(0); 
     return bean; 

    } 
} 

App Einspeisepunkt

@SpringBootApplication 
public class KtiCmsApplication extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
    SpringApplication.run(KtiCmsApplication.class, args); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(applicationClass); 
    } 

    private static Class<KtiCmsApplication> applicationClass = KtiCmsApplication.class; 
} 

Ich bin aus Ideen, wie diese Funktion zu erhalten, und dachte nur alten JAX-RS für Service-Exposition zu versuchen. Vielleicht hat hier jemand eine Idee, wie man mit dem Frühling arbeiten kann.

Antwort

0

Ich sehe keinen bestimmten Fehler in Ihrem Code, aber vielleicht hilft Ihnen das folgende Code-Snippet, CORS zum Laufen zu bringen. So habe ich in einem Springboot-Projekt mit Ember.js als Frontend eingerichtet.

Versuchen Sie, eine Konfigurationsklasse CorsConfig zu einem Projekt mit einem CorsFilter Bean hinzuzufügen:

package your.app.config; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.core.Ordered; 
import org.springframework.core.annotation.Order; 
import org.springframework.web.cors.CorsConfiguration; 
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 
import org.springframework.web.filter.CorsFilter; 

@Configuration 
public class CorsConfig { 

public CorsConfig() { 
} 

@Bean 
public CorsFilter corsFilter() { 

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    CorsConfiguration config = new CorsConfiguration(); 
    config.setAllowCredentials(true); 
    // likely you should limit this to specific origins 
    config.addAllowedOrigin("*"); 
    config.addAllowedHeader("*"); 
    config.addAllowedMethod("OPTIONS"); 
    config.addAllowedMethod("HEAD"); 
    config.addAllowedMethod("GET"); 
    config.addAllowedMethod("PUT"); 
    config.addAllowedMethod("POST"); 
    config.addAllowedMethod("DELETE"); 
    config.addAllowedMethod("PATCH"); 
    config.setAllowCredentials(Boolean.TRUE); 
    source.registerCorsConfiguration("/**", config); 
    return new CorsFilter(source); 
} 
} 

hinzufügen @CrossOrigin zu Ihrem Controller (wie Sie bereits haben)

These doc und SO Frage hat mir geholfen, zu erhalten dies läuft:

+0

Hey. Danke für die Rückmeldung. Nach dem Hinzufügen der Konfigurationsklasse wird immer noch der gleiche Fehler angezeigt. Ich bin mir sicher, aber denkst du, dass es möglich ist, dass der Glassfish die App nicht korrekt lädt? – user2960896

+0

Es tut mir leid, ich weiß nicht viel über Glassfish und konnte es nicht an deinem Beispiel erkennen. Nur um sicher zu sein, müssen Sie automatische Konfiguration aktivieren (über EnableAutoConfiguration oder @SpringBootApplication Annotation) in Spring Boot – Pascal

+0

Hinzufügen von Anmerkungen hilft nicht :(. Ich habe die App-Konfiguration und Einstiegspunkt auf das Problem, vielleicht hilft es. – user2960896