2017-07-25 190 views
0

我正在使用Java脚本API来与Web项目进行Tableau集成。我已经使用commnad:tabadmin set wgserver.trusted_hosts“”和相应的命令在Tableau Server中配置了我的IP。但是我无法获得票证,最终以-1结尾。我遵循了所有配置步骤。Tableau与Web项目的集成

public class TableauServlet extends javax.servlet.http.HttpServlet { 
private static final long serialVersionUID = 1L; 

public TableauServlet() { 
    super(); 
}  

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    final String user = "raghu"; 
    final String wgserver = "103.xxx.xxx.xx"; 
    final String dst = "views/Regional/College?:iid=1"; 
    final String params = ":embed=yes&:toolbar=yes"; 

    String ticket = getTrustedTicket(wgserver, user, request.getRemoteAddr()); 

    if (!ticket.equals("-1")) { 
     response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); 
     response.setHeader("Location", "http://" + wgserver + "/trusted/" + ticket + "/" + dst + "?" + params); 
    } 
    else 
     // handle error 
     throw new ServletException("Invalid ticket " + ticket); 
} 

// the client_ip parameter isn't necessary to send in the POST unless you have 
// wgserver.extended_trusted_ip_checking enabled (it's disabled by default) 
private String getTrustedTicket(String wgserver, String user, String remoteAddr) 
    throws ServletException 
{ 
    OutputStreamWriter out = null; 
    BufferedReader in = null; 
    try { 
     // Encode the parameters 
     StringBuffer data = new StringBuffer(); 
     data.append(URLEncoder.encode("username", "UTF-8")); 
     data.append("="); 
     data.append(URLEncoder.encode(user, "UTF-8")); 
     data.append("&"); 
     data.append(URLEncoder.encode("client_ip", "UTF-8")); 
     data.append("="); 
     data.append(URLEncoder.encode(remoteAddr, "UTF-8")); 

     // Send the request 
     URL url = new URL("http://" + wgserver + "/trusted"); 
     URLConnection conn = url.openConnection(); 
     conn.setDoOutput(true); 
     out = new OutputStreamWriter(conn.getOutputStream()); 
     out.write(data.toString()); 
     out.flush(); 

     // Read the response 
     StringBuffer rsp = new StringBuffer(); 
     in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     String line; 
     while ((line = in.readLine()) != null) { 
      rsp.append(line); 
     } 

     return rsp.toString(); 

    } catch (Exception e) { 
     throw new ServletException(e); 
    } 
    finally { 
     try { 
      if (in != null) in.close(); 
      if (out != null) out.close(); 
     } 
     catch (IOException e) {} 
    } 
} 

}

回答

0

我想在您的网址你缺少“target_site”参数,你去信任票,其需要的,如果你没有在您的默认网站的意见/区域/学院“ 。

我也经历了很多'-1'票的挫折! 您可能会尝试的一件事是在将Web服务器IP添加到tableau的trusted_hosts后重新启动tableau服务器。

我们最终做的另一件事是将Web服务器的内部IP和外部IP添加到tableau上的trusted_hosts。由于您使用103.xxx.xxx.xx作为您的tableau服务器,我假定两台服务器都位于同一内部网络上。如果一切都失败了,你可以尝试。

我的代码与您的代码几乎完全相同,并且工作正常。所以如果你的问题依然存在,那一定是与配置有关的东西。 这里是我的代码:

private String getAuthenticationTicket(String tableauServerUserName,String tableauServerUrl, String targetSite) { 

     OutputStreamWriter out = null; 
     BufferedReader in = null; 
     try { 
      StringBuffer data = new StringBuffer(); 
      data.append(URLEncoder.encode("username", Constant.UTF_8)); 
      data.append("="); 
      data.append(URLEncoder.encode(tableauServerUserName, Constant.UTF_8)); 
      data.append("&"); 
      data.append(URLEncoder.encode("target_site", Constant.UTF_8)); 
      data.append("="); 
      data.append(URLEncoder.encode(targetSite, Constant.UTF_8)); 
      URL url = new URL(tableauServerUrl + "/" + "trusted"); 

      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 
      out = new OutputStreamWriter(conn.getOutputStream()); 
      out.write(data.toString()); 
      out.flush(); 
      StringBuffer rsp = new StringBuffer(); 
      in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String line; 
      while ((line = in.readLine()) != null) { 
       rsp.append(line); 
      } 
      return rsp.toString(); 
     } catch (Exception ex) { 
      //log stuff, handle error 
      return null; 
     } finally { 
      try { 
       if (in != null) 
        in.close(); 
       if (out != null) 
        out.close(); 
      } catch (IOException ex) { 
       //log stuff, handle error 
      } 
     } 
    } 
+0

感谢您的回复@merawalaid。我已经设置了目标参数,即时获取HTML页面作为像{<!DOCTYPE html>票证的票证Tableau Server登录}现在的问题是org.apache.coyote.http11.HeadersTooLargeException:试图将更多的数据写入响应头中,而不是缓冲区中有可用空间。增加连接器上的maxHttpHeaderSize或将更少的数据写入响应头。 – veera

+0

Humm ...所以它看起来像Tableau服务器没有给您一个可信的令牌,并且正试图将您重定向到它的登录页面。如果我在你的地方,我现在只是摆脱'client_ip',直到我有基本的令牌机制工作。另外,我会检查用户'raghu'是否具有在tableau服务器上配置到您指定的'target_site'的访问权限。从代码的角度来看,我无法找到你在做什么错,我有非常类似的代码工作。所以这个问题一定是因为一些配置问题。 – merawalaid

+0

我忘了提到的一件事是tableau服务器是在其他一些网络上,而web服务器是在我的local.Im试图从我的本地访问tableau服务器。是否有任何重定向设施? – veera