p:imageCropper与https servlet路径问题

时间:2019-06-13 17:36:23

标签: jsf primefaces

我正在尝试使用imageCropper实现primefaces文件上传,以在https下更改用户的头像。
我上传图像并将其保存到应用程序服务器的temp文件夹中。
我使用服务器临时路径中的WebServlet将上传的图像投放到primefaces imageCropper中

当我使用http时,一切正常,但是当我转为https时,出现错误{0}: Conversion error occurred.

这是我的代码:

xhtml代码

 <p:imageCropper
    id="avatarImage"
    image="https://#{request.serverName}:#{request.serverPort}#{request.contextPath}/reports/#{UserpreferencesBean.imageFilePath}"
    value="#{UserpreferencesBean.croppedImage}"
    aspectRatio="1.0" initialCoords="225,75,300,125"
    boxWidth="400"
    boxHeight="400"
    minSize="90,90"/>
  <br/>

   <p:commandButton id="cropButton"
     value="Crop"
     action="#{UserpreferencesBean.crop()}"
     update="form:messages image avatarImage avatarForm"
     icon="ui-icon-scissors"/>

Bean代码

public void crop() throws IOException {
    avatarImage = new DefaultStreamedContent(null);
    avatarImage = new DefaultStreamedContent(new ByteArrayInputStream(croppedImage.getBytes()));

    in = new ByteArrayInputStream(croppedImage.getBytes());
    avatarByteArray = croppedImage.getBytes();

    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Cropping finished."));
}

WebServlet代码

@WebServlet("/reports/*")
public class ImageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = request.getPathInfo().substring(1);
    File file = new File(System.getProperty("jboss.server.temp.dir"), filename);
    response.setHeader("Content-Type", getServletContext().getMimeType(filename));
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "inline; filename="+File.separator + filename + File.separator );
    Files.copy(file.toPath(), response.getOutputStream());
}

}

一些笔记
->我的SSL无效。我使用自签名证书
->我的应用服务器是Wildfly 16

1 个答案:

答案 0 :(得分:1)

最后,我找到了解决方案。问题是自签名证书。 解决方案是在Java中信任不受信任的证书。您只需在应用程序启动时在侦听器中使用此代码,即可信任所有证书。

 private static class TrustAllManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}
    public static void initTrustAllClient() {

        try {
            TrustManager[] trustAll = new TrustManager[]{new TrustAllManager()};

            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAll, new SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HostnameVerifier allHostValid = new HostnameVerifier() {
                @Override
                public boolean verify(String string, SSLSession ssls) {
                    return true;
                }
            };
            HttpsURLConnection.setDefaultHostnameVerifier(allHostValid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

一些链接
Trusting all certificates using HttpClient over HTTPS
https://gist.github.com/michalbcz/4170520


其他解决方法(不是很好)是将WebServlet URL排除在SSL安全之外,而我提供的图像不带https。

因此,除了图像servlet之外,所有应用程序都使用https。

我仍然认为问题出在自签名证书上。

 <security-constraint>
    <web-resource-collection>
        <web-resource-name>SecureResource</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Non-SecureResource</web-resource-name>
        <url-pattern>/reports/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>