2016-07-01 12 views
0

私は既にこの質問をVaadin Forumに掲載しましたが、残念ながら私は何の応答も得ていませんでした。おそらく質問の答えはspring-bootとVaadinの間にあります。Vaadin spring-bootアプリケーションをHTMLに埋め込む

現在、私はVaadinアプリケーションをHTMLページに埋め込むのに苦労しています。

私は何を使っています:

Vaadin 7.6.6 
vaadin-spring 
spring-boot 1.3.5.RELEASE 

春-ブーツとの組み合わせでCORSを有効にするには、私はSami's Blog entryを適応し、次のカスタムCORSサーブレットを作成しました:

import java.io.IOException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.stereotype.Component; 

import com.vaadin.spring.server.SpringVaadinServlet; 

/** 
* This custom {@link SpringVaadinServlet} enables CORS in combination with 
* Spring. 
* 
* @author Christoph Guse 
* 
*/ 
public class CORSServlet extends SpringVaadinServlet { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -2482991123719720492L; 

    /** 
    * Override to handle the CORS requests. 
    */ 
    @Override 
    protected void service(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     // Origin is needed for all CORS requests 
     String origin = request.getHeader("Origin"); 
     if (origin != null && isAllowedRequestOrigin(origin)) { 

      // Handle a preflight "option" requests 
      if ("options".equalsIgnoreCase(request.getMethod())) { 
       response.addHeader("Access-Control-Allow-Origin", origin); 
       response.setHeader("Allow", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS"); 

       // allow the requested method 
       String method = request.getHeader("Access-Control-Request-Method"); 
       response.addHeader("Access-Control-Allow-Methods", method); 

       // allow the requested headers 
       String headers = request.getHeader("Access-Control-Request-Headers"); 
       response.addHeader("Access-Control-Allow-Headers", headers); 

       response.addHeader("Access-Control-Allow-Credentials", "true"); 
       response.setContentType("text/plain"); 
       response.setCharacterEncoding("utf-8"); 
       response.getWriter().flush(); 
       return; 
      } // Handle UIDL post requests 
      else if ("post".equalsIgnoreCase(request.getMethod())) { 
       response.addHeader("Access-Control-Allow-Origin", origin); 
       response.addHeader("Access-Control-Allow-Credentials", "true"); 
       super.service(request, response); 
       return; 
      } 
     } 

     // All the other requests nothing to do with CORS 
     super.service(request, response); 

    } 

    /** 
    * Check that the page Origin header is allowed. 
    */ 
    private boolean isAllowedRequestOrigin(String origin) { 
     // TODO: Remember to limit the origins. 
     return origin.matches(".*"); 
    } 

} 

はさらに私は約いくつかのドキュメントを見つけましたspring-bootとCORSのため、このSpringの設定を追加しました:

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.CorsRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

import CORSServlet; 

/** 
* @author Christoph Guse 
* 
*/ 
@Configuration 
public class AuthAppVaadinApplicationConfiguration { 

    @Bean 
    public WebMvcConfigurer corsConfigurer(){ 
     return new WebMvcConfigurerAdapter() { 

      @Override 
      public void addCorsMappings(CorsRegistry registry) { 
       registry.addMapping("/**").allowedOrigins(".*"); 
      } 

     }; 
    } 

    @Bean(name="vaadinServlet") 
    public CORSServlet corsServlet(){ 

     return new CORSServlet(); 

    } 

} 

私のHTMLは次のようになります。

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" 
content="text/html; charset=UTF-8" /> 
<meta http-equiv="X-UA-Compatible" 
content="IE=9;chrome=1" /> 

<title>Embedding a Vaadin Application in HTML Page</title> 

<!-- Set up the favicon from the Vaadin theme --> 
<link rel="shortcut icon" type="image/vnd.microsoft.icon" 
href="/VAADIN/themes/reindeer/favicon.ico" /> 
<link rel="icon" type="image/vnd.microsoft.icon" 
href="/VAADIN/themes/reindeer/favicon.ico" /> 
</head> 

<body> 
<!-- Loads the Vaadin widget set, etc. --> 
<script type="text/javascript" 
src="http://vaadin.poc:8090/VAADIN/vaadinBootstrap.js?v=7.6.6"></script> 

<h1>Embedding a Vaadin UI</h1> 

<p>This is a static web page that contains an embedded Vaadin 
application. It's here:</p> 

<!-- So here comes the div element in which the Vaadin --> 
<!-- application is embedded. --> 
<div style="width: 100%; height: 75vh; border: 2px solid green;" 
id="helloworld" class="v-app"> 

<!-- Optional placeholder for the loading indicator --> 
<div class=" v-app-loading"></div> 

<!-- Alternative fallback text --> 
<noscript>You have to enable javascript in your browser to 
use an application built with Vaadin.</noscript> 
</div> 

<script type="text/javascript">//<![CDATA[ 
if (!window.vaadin) 
alert("Failed to load the bootstrap JavaScript: "+ 
"VAADIN/vaadinBootstrap.js"); 

/* The UI Configuration */ 
vaadin.initApplication("helloworld", { 
"browserDetailsUrl": "http://vaadin.poc:8090/", 
"serviceUrl": "http://vaadin.poc:8090/", 
"theme": "valo", 
"versionInfo": {"vaadinVersion": "7.6.6"}, 
"widgetset": "com.vaadin.DefaultWidgetSet", 
"vaadinDir": "http://vaadin.poc:8090/VAADIN/", 
"heartbeatInterval": 300, 
"debug": true, 
"standalone": false, 
"authErrMsg": { 
"message": "Take note of any unsaved data, "+ 
"and <u>click here<\/u> to continue.", 
"caption": "Authentication problem" 
}, 
"comErrMsg": { 
"message": "Take note of any unsaved data, "+ 
"and <u>click here<\/u> to continue.", 
"caption": "Communication problem" 
}, 
"sessExpMsg": { 
"message": "Take note of any unsaved data, "+ 
"and <u>click here<\/u> to continue.", 
"caption": "Session Expired" 
} 
});//]] > 
</script> 

<p>Please view the page source to see how embedding works.</p> 
</body> 
</html> 

私の問題がinitallyにロードされるアプリケーションですが、いくつかのアイコンが不足していると私はappliction、すなわちドロップボックスを開くでアクションをトリガーする場合、アプリケーションはできませんspring-bootアプリケーションに接続します。 エラーメッセージは次のようになります。誰がそこに別のHTMLアプリケーションにVaadin春ブートアプリケーションを埋め込むために、管理者

XMLHttpRequest cannot load http://vaadin.poc:8090/UIDL/?v-uiId=0. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403. 

ありますか?

どのようなヒントも高く評価されています。 Christoph

+0

。残念ながら私はこれまでのところ進歩はしていませんでした。 – flexguse

答えて

0

幸いにも、Vaadinフォーラムの誰かが私に不足しているリンクを教えてくれました。私は、スタンドアロンのHTMLに、いくつかのJavaScriptを追加するのを忘れ:

<script> 
     XMLHttpRequest.prototype._originalSend = XMLHttpRequest.prototype.send; 
     var sendWithCredentials = function(data) { 
      this.withCredentials = true; 
      this._originalSend(data); 
     }; 
     XMLHttpRequest.prototype.send = sendWithCredentials; 
    </script> 
助け

が、フォントが正しくCORSの問題によってロードされたので、私は、カスタムVaadin CORSServletを取り除き、ベースのCORSをspring-によって提供されるサポートのフィルタを追加していませんでしたブート(in this blogの記事を参照)。

私のサンプルが正しく動作し、デモアプリケーションが完全に機能し、フォントが正しく読み込まれて使用されます。

完全に動作する例を得るには、https://github.com/flexguse/vaadin-html-embeddingをご覧ください。私はGitHubのhttps://github.com/flexguse/vaadin-html-embeddingで見つけることができるデモアプリケーションを作成し、より良い議論のベース用

乾杯、 クリストフ

関連する問題