spring boot配置 ssl 详解spring boot配置 ssl

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring boot配置 ssl 详解spring boot配置 ssl

烙印者乌都尔   2021-03-24 我要评论
想了解详解spring boot配置 ssl的相关内容吗,烙印者乌都尔在本文为您仔细讲解spring boot配置 ssl的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,boot配置,ssl,spring,boot,ssl,下面大家一起来学习吧。

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=.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<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  

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们