最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解spring boot配置 ssl

 更新時間:2017年05月08日 09:24:31   作者:烙印者烏都爾  
本篇文章主要介紹了詳解spring boot配置 ssl,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

ssl協(xié)議位于tcp/ip協(xié)議與各種應(yīng)用協(xié)議之間,為數(shù)據(jù)通信提供安全支持。

ssl協(xié)議分為兩層:

  1. ssl記錄協(xié)議,它建立在可靠傳輸協(xié)議之上,為高層協(xié)議提供數(shù)據(jù)封裝、壓縮、加密等基本功能支持。
  2. ssl握手協(xié)議,它建立在ssl記錄協(xié)議之上,用于實(shí)際數(shù)據(jù)傳輸開始前,通信雙方進(jìn)行身份認(rèn)證、協(xié)商加密算法、交換加密密鑰等

基于B/S的web應(yīng)用,是通過https來實(shí)現(xiàn)ssl的。https是http的安全版,即在http下加入ssl層,https的安全基礎(chǔ)是ssl;

我們開始在spring boot中使用ssl設(shè)置;

1.生成證書

每一個jdk或者jre中都有一個工具叫keytool,它是一個證書管理工具,可以用來生成自簽名的證書;打開cmd,進(jìn)入jdk/bin路徑,敲入命令

keytool -genkey -alias tomcat
  

  

在用戶路徑下生成 .keystore文件 ,這就是我們要使用的證書文件。

2.spring boot配置ssl

將.keystore文件復(fù)制到項(xiàng)目根目錄,然后配置application.properties中做ssl配置

server.ssl.key-store=.keystore

server.ssl.key-store-password=密碼

server.ssl.keyStoreType = JKS

server.ssl.keyAlias=tomcat 

啟動項(xiàng)目

  

訪問地址 https://localhost:8080

    

3、http轉(zhuǎn)https

要實(shí)現(xiàn)這個功能,我們需要配置TomcatEmbeddedServletContainerFactory,并且添加tomcat的connector來實(shí)現(xiàn)。

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 就會跳轉(zhuǎn)到 https://localhost:8088  

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

湛江市| 广汉市| 乌什县| 伊通| 道孚县| 乌拉特前旗| 游戏| 内江市| 博白县| 鄂州市| 黄大仙区| 汾阳市| 临高县| 甘孜| 大名县| 平邑县| 鄂托克前旗| 江源县| 库伦旗| 麻江县| 黄龙县| 稷山县| 铁岭县| 中牟县| 江城| 渝北区| 祥云县| 汶上县| 万载县| 濮阳市| 苏尼特右旗| 元朗区| 栾川县| 普洱| 桐柏县| 介休市| 宁南县| 彝良县| 唐山市| 英山县| 辉南县|