首页 > 编程 > Java > 正文

详解spring boot配置 ssl

2019-11-26 12:21:32
字体:
来源:转载
供稿:网友

ssl协议位于tcp/ip协议与各种应用协议之间,为数据通信提供安全支持。

ssl协议分为两层:

  1. ssl记录协议,它建立在可靠传输协议之上,为高层协议提供数据封装、压缩、加密等基本功能支持。
  2. ssl握手协议,它建立在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=.keystoreserver.ssl.key-store-password=密码server.ssl.keyStoreType = JKSserver.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<Person> 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  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表