1. keytool 生成证书
windows下的生成:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
linux下的命令:
keytool -genkey -alias tomcat -keyalg RSA -validity 20000 -keystore keystore.p12
执行完后会生成一个文件:keystore.p12。
关于keytool的说明:
keytool -genkey -alias 你的证书别名 -keyalg 密钥算法 -keystore 证书库文件保存的位置和文件名 -keysize 密钥长度 -validity 证书有效期天数
springboot编码
配置application.properties:
# https://127.0.0.1:8080/index.html https端口
server.port=8080
server.servlet.path=/
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias= tomcat
将上面生成的keystore.p12文件移动到resources下。
此时你的项目就可以使用https协议访问。
与http同时访问
如果你希望你的项目既可以http访问,也可以https访问,而且两个互不干扰:
在启动类下,加入以下代码:
springboot版本2.x:
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createHTTPConnector());
return tomcat;
}
private Connector createHTTPConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setSecure(false);
// http 端口
connector.setPort(8081);
//https 端口
connector.setRedirectPort(8080);
return connector;
}
ps:注意:http的端口不要https相同,即上文的port设置,否则会出现端口占用错误。
http强制转换https访问
在用户用http访问的时候,强制转换为https。
基于上述模块与http同时访问,添加过滤器,强制重定向。
packagecom.ps.learn.socketio.filter;/**
* Created by Administrator on 2018/8/5 0005.
*/importorg.springframework.context.annotation.Configuration;importorg.springframework.web.filter.OncePerRequestFilter;importjavax.servlet.FilterChain;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebFilter;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/** * * * o *@Description*@Modify*/@Configuration@WebFilterpublicclassKindoFilterextendsOncePerRequestFilter{@OverrideprotectedvoiddoFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throwsServletException, IOException{ String requestURL = request.getRequestURL().toString(); String protocol = requestURL.split("://")[0]; System.out.println(1111);if("http".equals(protocol)) { requestURL = requestURL.replace("http","https").replace("8081","8080"); response.sendRedirect(requestURL); } filterChain.doFilter(request, response); }}
以上是http和https共存时,http转https
若不需要共存 ,而且需要http转https,则只需要去掉过滤器,然后把之前加在启动类的代码改成一下即可
@BeanpublicServletWebServerFactoryservletContainer(){TomcatServletWebServerFactory tomcat =newTomcatServletWebServerFactory() {@OverrideprotectedvoidpostProcessContext(Context context){SecurityConstraint constraint =newSecurityConstraint();constraint.setUserConstraint("CONFIDENTIAL");SecurityCollection collection =newSecurityCollection();collection.addPattern("/*");constraint.addCollection(collection);context.addConstraint(constraint);}};tomcat.addAdditionalTomcatConnectors(createHTTPConnector());returntomcat;}privateConnectorcreateHTTPConnector(){Connector connector =newConnector("org.apache.coyote.http11.Http11NioProtocol");connector.setScheme("http");connector.setSecure(false);// http 端口connector.setPort(8081);//https端口connector.setRedirectPort(8080);returnconnector;}