图说明
第一步首先去阿里云弄一个免费的SSL证书
在这里插入图片描述
下载然后 放到项目里面的resource路径下
这里一定要注意 是 key-store 和 key-store-password 我在配置时写出了 key-password 弄了很久没找到原因 换成了
nginx 去配置,最近还是嫌弃服务启动太多 改了回来
在这里插入图片描述
现在如果直接方法服务器上那么现在 就可以https 访问我的项目了 但是如果你用http 就不行 因为http是默认80端口 而https 是默认的 443 端口 所以我在我的启动类里面写一段代码 。防护http 是自动跳转到https
EmbeddedServletContainerFactory 可以因为你的spring boot 版本 过高没有这个class 那么你需要去你对应spring boot版本的class
public class HuahaiApplication {
public static void main(String[] args)
{
SpringApplication.run(HuahaiApplication.class, args);
}
/**
* 配置一个 TomcatServletWebServerFactory bean
* 将http 重定向到 https
* @return
*/
/**
* it's for set http url auto change to https
*/
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint=new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");//confidential
SecurityCollection collection=new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
//配置http转https
@Bean
public Connector httpConnector(){
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(443);
return connector;
}
}
最近我可以把我本地的服务映射到外网去访问借助一个 sunny-ngrok 免费的
在这里插入图片描述
去域名管理中心解析你的域名
在这里插入图片描述本地启动ngrok 的服务
在这里插入图片描述
效果
输入你的域名 不大https 也会自动跳转到https 这里是因为我刚刚在启动类里面配置了 重定向
在这里插入图片描述
在这里插入图片描述
网友评论