首页 > 解决方案 > Spring Boot MVC Jsoup 无法初始化类 sun.security.ssl.SSLContextImpl$DefaultSSLContext

问题描述

我开发了一个 Spring Boot 应用程序,它使用 jsoup 从网站读取信息。获得信息后,我将重新格式化为适当的 Google Amp 页面。该应用程序在导入到 Mac 时在 Windows 上运行,当执行 Jsoup.connect(htmlPageUrl).get() 时我得到错误。在代码行发生以下错误

错误

        java.lang.SecurityException: The jurisdiction policy files are not signed by a trusted signer!
            at javax.crypto.JarVerifier.verifyPolicySigned(JarVerifier.java:328) ~[na:1.8.0_121]
            at javax.crypto.JceSecurity.loadPolicies(JceSecurity.java:317) ~[na:1.8.0_121]
            at javax.crypto.JceSecurity.setupJurisdictionPolicies(JceSecurity.java:262) ~[na:1.8.0_121]
            at javax.crypto.JceSecurity.access$000(JceSecurity.java:48) ~[na:1.8.0_121]
            at javax.crypto.JceSecurity$1.run(JceSecurity.java:80) ~[na:1.8.0_121]
            at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_131]
            at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:77) ~[na:1.8.0_121]
            at javax.crypto.JceSecurityManager.<clinit>(JceSecurityManager.java:65) ~[na:1.8.0_121]
            at javax.crypto.Cipher.getConfiguredPermission(Cipher.java:2587) ~[na:1.8.0_121]
            at javax.crypto.Cipher.getMaxAllowedKeyLength(Cipher.java:2611) ~[na:1.8.0_121]
            at sun.security.ssl.CipherSuite$BulkCipher.isUnlimited(CipherSuite.java:535) ~[na:1.8.0_131]
            at sun.security.ssl.CipherSuite$BulkCipher.<init>(CipherSuite.java:507) ~[na:1.8.0_131]
            at sun.security.ssl.CipherSuite.<clinit>(CipherSuite.java:614) ~[na:1.8.0_131]
            at sun.security.ssl.SSLContextImpl.getApplicableCipherSuiteList(SSLContextImpl.java:293) ~[na:1.8.0_131]
            at sun.security.ssl.SSLContextImpl.access$100(SSLContextImpl.java:41) ~[na:1.8.0_131]
            at sun.security.ssl.SSLContextImpl$AbstractTLSContext.<clinit>(SSLContextImpl.java:424) ~[na:1.8.0_131]
            at java.lang.Class.forName0(Native Method) ~[na:1.8.0_131]
            at java.lang.Class.forName(Class.java:264) ~[na:1.8.0_131]
            at java.security.Provider$Service.getImplClass(Provider.java:1634) ~[na:1.8.0_131]
            at java.security.Provider$Service.newInstance(Provider.java:1592) ~[na:1.8.0_131]
            at sun.security.jca.GetInstance.getInstance(GetInstance.java:236) ~[na:1.8.0_131]
            at sun.security.jca.GetInstance.getInstance(GetInstance.java:164) ~[na:1.8.0_131]
            at javax.net.ssl.SSLContext.getInstance(SSLContext.java:156) ~[na:1.8.0_131]
            at javax.net.ssl.SSLContext.getDefault(SSLContext.java:96) ~[na:1.8.0_131]
            at javax.net.ssl.SSLSocketFactory.getDefault(SSLSocketFactory.java:122) ~[na:1.8.0_131]
            at javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory(HttpsURLConnection.java:332) ~[na:1.8.0_131]
            at javax.net.ssl.HttpsURLConnection.<init>(HttpsURLConnection.java:289) ~[na:1.8.0_131]
            at sun.net.www.protocol.https.HttpsURLConnectionImpl.<init>(HttpsURLConnectionImpl.java:85) ~[na:1.8.0_131]
            at sun.net.www.protocol.https.Handler.openConnection(Handler.java:62) ~[na:1.8.0_131]
            at sun.net.www.protocol.https.Handler.openConnection(Handler.java:57) ~[na:1.8.0_131]
            at java.net.URL.openConnection(URL.java:979) ~[na:1.8.0_131]
            at org.jsoup.helper.HttpConnection$Response.createConnection(HttpConnection.java:774) ~[jsoup-1.10.2.jar:na]
            at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:648) ~[jsoup-1.10.2.jar:na]
            at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:628) ~[jsoup-1.10.2.jar:na]
            at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:260) ~[jsoup-1.10.2.jar:na]
            at org.jsoup.helper.HttpConnection.get(HttpConnection.java:249) ~[jsoup-1.10.2.jar:na]
            at com.example.googleamppageconverter.HtmlToGoogleAmpController.ampBlog(HtmlToGoogleAmpController.java:51) ~[classes/:na]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
            at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
            at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

我在 MacBook 上使用 intellij IDE。

这是我的 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>com.example</groupId>
        <artifactId>googleamppageconverter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>

        <name>googleamppageconverter</name>
        <description>Google Amp Page Converter Microservice</description>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>

            <dependency>
                <groupId>org.jsoup</groupId>
                <artifactId>jsoup</artifactId>
                <version>1.10.2</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <executable>true</executable>
                    </configuration>
                </plugin>
            </plugins>
        </build>


    </project>

这是我的控制器

    package com.example.googleamppageconverter;

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.servlet.ModelAndView;

    import javax.servlet.http.HttpServletRequest;


    @Controller
    public class HtmlToGoogleAmpController {

        final private String imgPattern = "<img\\s[^>]*?height\\s*=\\s*['\\\"]([0-9]*?)['\\\"][^>]*?src\\s*=\\s*['\\\"]([^'\\\"]*?)['\\\"][^>]*?width\\s*=\\s*['\\\"]([0-9]*?)['\\\"][^>]*?>";
        final private String ampImgTag = "<amp-img height=\"$1\" width=\"$3\" src=\"https://example.com$2\" layout=\"responsive\" alt=\"AMP\"></amp-img>";
        final private String relativeAnchor = "<a\\s*href\\s*=['\\\"](?!http)";
        final private String absoluteAnchor = "<a href=\"https://example.com/";

        // need to get entire url including path
        //this is a multi-level path
        @GetMapping("/ampBlog/{section}/{article}")
        public String ampBlog(@PathVariable("section") String section, @PathVariable("article") String article, Model model) {

            String ampBlogTitle = "Page not Found";
            String cleanContent = "Content Not Found";

            if(section.isEmpty() && !section.matches("^[a-zA-Z0-9-_]+$")) {
                model.addAttribute("ampBlogTitle",ampBlogTitle);
                model.addAttribute("blogContent", cleanContent);

                return "ampBlog";
            }

            if (article.isEmpty() && !article.matches("^[a-zA-Z0-9-_]+$")){
                model.addAttribute("ampBlogTitle",ampBlogTitle);
                model.addAttribute("blogContent", cleanContent);

                return "ampBlog";
            }

            String htmlPageUrl = "https://example.com/blog/"+section+"/"+article;


            try {
                Document blog = Jsoup.connect(htmlPageUrl).get();
                Elements blogContent = blog.select(".blog-article");
                blogContent.select("a").first().remove();
                Element blogTitle = blogContent.select("h1").first();
                ampBlogTitle = blogTitle.text();
                String blogContentHtml = blogContent.html();
                cleanContent = blogContentHtml.replaceAll(imgPattern,ampImgTag);
                cleanContent = cleanContent.replaceAll(relativeAnchor,absoluteAnchor);

            }catch (java.io.IOException ex) {


            }

            model.addAttribute("htmlPageUrl", htmlPageUrl);
            model.addAttribute("ampBlogTitle",ampBlogTitle);
            model.addAttribute("blogContent", cleanContent);

            return "ampBlog";
        }

    }

我最初在 windows 中创建了应用程序,它在 windows 环境下工作。我是 Java 和 Spring Boot 框架的新手。

标签: spring-mvcspring-bootjsoup

解决方案


已修复:我的 Java 加密扩展 (JCE) 无限强度管辖策略文件已过时。我更新了它们,扩展现在正在工作。遵循此处的步骤:java.lang.SecurityException:管辖权策略文件未由受信任的签名者签名


推荐阅读