谈到Jetty 9 ...只要您的SSL连接器已经可以使用,就可以按照以下方法进行操作:
第1步:通过将所有内容添加到您的web.xml中,确保所有内容都通过SSL。如果您尝试通过HTTP访问资源,则将返回403!SECURE错误
<security-constraint>
<web-resource-collection>
<web-resource-name>Everything</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
步骤2:将 Jetty看到403!SECURE错误时将其重定向到HTTPS,方法是将此错误添加到您的jetty.xml中
<New id="tlsHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Arg>
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<!-- This says... Redirect to https://host:8443 if server returns "NOT SECURE" error -->
<Set name="secureScheme">https</Set>
<Set name="securePort">8443</Set>
</New>
</Arg>
<Call name="addCustomizer">
<Arg>
<New class="org.eclipse.jetty.server.SecureRequestCustomizer" />
</Arg>
</Call>
</New>
<!-- This is your HTTP connector, you should have another one for HTTPS -->
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server">
<Ref refid="MyServer" />
</Arg>
<Arg name="factories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config">
<!-- defined above -->
<Ref refid="tlsHttpConfig" />
</Arg>
</New>
</Item>
</Array>
</Arg>
<Set name="host">localhost</Set>
<Set name="port">8080</Set>
</New>