给你的项目配置个https吧

1.申请证书

这里我选择的是阿里云的个人免费的证书

给你的项目配置个https吧

因为使用的是内置的Tomcat,所以下载Tomcat类型的

给你的项目配置个https吧

2.配置项目

将证书XXXX.pfx文件放到项目的resources目录,接着修改application.yml文件

<code>

server

:

port

:

443

ssl

:

key-store

:

classpath

:XXXX.pfx

key-store-password

: 证书密码

keyStoreType

: PKCS12/<code>

接着修改启动类,添加如下内容,接着启动项目

<code>

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.context.annotation.Bean;

import

org.mybatis.spring.annotation.MapperScan;

import

org.springframework.scheduling.annotation.EnableAsync;

import

org.springframework.scheduling.annotation.EnableScheduling;

import

org.springframework.transaction.annotation.EnableTransactionManagement;

public

class

MxemApplication

implements

EmbeddedServletContainerCustomizer

{

public

EmbeddedServletContainerFactory

servletContainer

()

{ TomcatEmbeddedServletContainerFactory tomcat =

new

TomcatEmbeddedServletContainerFactory() {

protected

void

postProcessContext

(Context context)

{ SecurityConstraint constraint =

new

SecurityConstraint(); constraint.setUserConstraint(

"CONFIDENTIAL"

); SecurityCollection collection =

new

SecurityCollection(); collection.addPattern(

"/*"

); constraint.addCollection(collection); context.addConstraint(constraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector());

return

tomcat; }

public

Connector

httpConnector

()

{ Connector connector =

new

Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL); connector.setScheme(

"http"

); connector.setPort(

80

); connector.setSecure(

false

); connector.setRedirectPort(

443

);

return

connector; }

public

void

customize

(ConfigurableEmbeddedServletContainer container)

{ container.setPort(

443

); } }/<code>

3.可能出现的问题

可能会出现下面的错误

<code>Address already in 

use

:

bind

/<code>

解决办法

以windows系统为例,查看当前端口被哪个进程占用了(进入到CMD中)

<code>

netstat

-ano|findstr

"443"

/<code>

然后找到进程ID,使用任务管理器结束此进程即可。

如果对你有帮助,还请点个赞,点个关注

给你的项目配置个https吧


分享到:


相關文章: