发布于 2018-03-18 12:24:01 | 193 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Boot

Spring Boot 项目旨在简化创建产品级的 Spring 应用和服务。你可通过它来选择不同的 Spring 平台。可创建独立的 Java 应用和 Web 应用,同时提供了命令行工具来允许 'spring scripts'.


本篇文章主要介绍了详解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  

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务