十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
ssl如何实现在spring boot中配置 ?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
紫阳网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。成都创新互联于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
ssl协议位于tcp/ip协议与各种应用协议之间,为数据通信提供安全支持。
ssl协议分为两层:
基于B/S的web应用,是通过https来实现ssl的。https是http的安全版,即在http下加入ssl层,https的安全基础是ssl;
我们开始在spring boot中使用ssl设置;
1.生成证书
每一个jdk或者jre中都有一个工具叫keytool,它是一个证书管理工具,可以用来生成自签名的证书;打开cmd,进入jdk/bin路径,敲入命令
keytool -genkey -alias tomcat

在用户路径下生成 .keystore文件 ,这就是我们要使用的证书文件。

2.spring boot配置ssl
将.keystore文件复制到项目根目录,然后配置application.properties中做ssl配置
server.ssl.key-store=.keystore server.ssl.key-store-password=密码 server.ssl.keyStoreType = JKS server.ssl.keyAlias=tomcat
启动项目

访问地址 https://localhost:8080


3、http转https
要实现这个功能,我们需要配置TomcatEmbeddedServletContainerFactory,并且添加tomcat的connector来实现。
package com.example;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by xingzhuipingye on 2017/5/7.
*/
@Controller
@SpringBootApplication
public class ApplicationMy {
@RequestMapping("/")
public String index(Model model){
Person single = new Person("aa",11);
List list = new ArrayList<>();
Person p1 = new Person("xx",11);
Person p2 = new Person("yy",22);
Person p3 = new Person("zz",33);
list.add(p1);
list.add(p2);
list.add(p3);
model.addAttribute("singlePerson",single);
model.addAttribute("people",list);
return "index";
}
public static void main(String[] args){
SpringApplication.run(ApplicationMy.class);
}
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector(){
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8088);
return connector;
}
}
注:我在application.properties 中修改了端口为8088
此时我们访问http://localhost:8080 就会跳转到 https://localhost:8088
关于ssl如何实现在spring boot中配置 问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。