Spring Framework 5
目前Spring Framework的GA版本是5.1.9,Spring 5带来了两个新特性,一个是Functional Web Framework,有个链接说的比较详细:关于Functional Web Framework,一个是Reactive Programming响应式编程。
JDK
安装JDK 1.8.201,1.8的应该都行。
Tomcat 9配置HTTP/2
Spring 5支持HTTP/2协议,这里采用的容器是Tomcat 9.0.22,除了配置HTTP/2之外,还要配置HTTPS,配置如下:
- tomcat-users.xml中添加:
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="spring5" password="spring5" roles="manager-gui,manager-script"/>
后面需要这两种角色manager-gui、manager-script。
-
需要ssl证书,安装Win64OpenSSL-1_1_1c.exe。
-
命令行执行:
openssl req -newkey rsa:2048 -nodes -keyout spring5.key -x509 -days 3650 -out spring5.crt
生成自签名ssl证书spring5.crt 并 生成2048位rsa(非对称加密算法)私钥spring5.key,
自签名证书是由我们自己签署的证书,而不是由受信任的证书机构签发的证书。
- 回车后会要求填写CSR(证书签名请求文件):
Generating a RSA private key
writing new private key to 'spring5.key'
You are about to be asked to enter information that will be incorporated into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank.
For some fields there will be a default value.
If you enter '.', the field will be left blank.
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:xx
Locality Name (eg, city) []:yy
Organization Name (eg, company) [Internet Widgits Pty Ltd]:zz
Organizational Unit Name (eg, section) []:aa
Common Name (e.g. server FQDN or YOUR name) []:spring5test
Email Address []:cc
命令会生成spring5.key和spring5.crt,spring5.key是server用的私钥,其他人是禁止访问的,spring5.crt是ssl证书,后面会把这个证书注册到Tomcat和JRE中。
- 用JDK提供的工具keytool:
keytool -import -alias spring5keystore -file spring5.crt -keystore spring5.keystore
将ssl证书文件spring5.crt导入到名为spring5.keystore的证书库中,取个别名为spring5keystore。
会要求输入不少于六位的密码,123456。
是否信任证书,填y。
- Java JRE要识别这个证书,要将创建的证书注册到JRE cacerts中,运行以下命令:
keytool -import -alias spring5keystore -file spring5.crt -keystore "C:\Program Files\Java\jre1.8.0_201\lib\security\cacerts"
默认密码是changeit,填y,即添加进了JRE cacerts中。
-
将spring5.crt, spring5.key,以及spring5.keystore复制到Tomcat的conf文件夹和JRE的C:\Program Files\Java\jre1.8.0_201\lib\security。
-
Tomcat的server.xml,修改端口8443的配置:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" maxThreads="150" SSLEnabled="true">
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol"/>
<SSLHostConfig honorCipherOrder="false">
<Certificate certificateKeyFile="conf/spring5.key" certificateFile="conf/spring5.crt" type="RSA" />
</SSLHostConfig>
</Connector>
- 可以使用https://localhost:8443/manager验证,账号密码spring5。
Spring Tools 4 for Eclipse
现在IDEA很火,但是IDEA是收费的,盗版使用毕竟不好,所以这里先采用Spring官网tools中的Spring Tools 4 for Eclipse。
- SpringToolSuite4.ini中做下配置:
-Xms512m
-Xmx1024m
-
在STS中Window|Preferences,JRE选择本地JRE路径:
Snipaste_2019-08-16_17-08-57.png -
使用maven构建spring 5应用,STS中有内置的maven,但是我们用自己安装的外部maven,版本3.5,maven的settings.xml记得新加一个阿里云的镜像地址并设置本地Repository:
<localRepository>c:/maven</localRepository>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
STS中添加自己的maven:
Snipaste_2019-08-19_09-15-35.png
maven的配置也要相应修改: Snipaste_2019-08-19_09-24-22.png
-
新建一个maven项目:
Snipaste_2019-08-19_09-26-42.png
Snipaste_2019-08-19_09-28-14.png
Snipaste_2019-08-19_09-33-04.png
pom.xml中加下面配置,避免出现Errors,使maven的编译采用JDK 1.8。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
pom.xml中关于Tomcat配置:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>https://localhost:8443/manager/text</url>
<path>/test01</path>
<keystoreFile>C:\apache-tomcat-9.0.22-windows-x64\apache-tomcat-9.0.22\conf\spring5.keystore</keystoreFile>
<keystorePass>123456</keystorePass>
<update>true</update>
<username>spring5</username>
<password>spring5</password>
</configuration>
</plugin>
- 先运行Tomcat,右键单击项目,然后单击Run As | Maven Build…并执行以下目标:clean install tomcat7:deploy。
这个时候会报错:
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:deploy (default-cli) on project test01: Cannot invoke Tomcat manager: hostname in certificate didn't match: <localhost> != <spring5test> -> [Help 1]
<localhost> != <spring5test> 其实指的是不能用localhost了,得使用前面填写CSR信息时的‘spring5test’,先在hosts文件添加:
127.0.0.1 spring5test
pom.xml中关于Tomcat配置部分修改:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>https://spring5test:8443/manager/text</url>
<path>/test01</path>
<keystoreFile>C:\apache-tomcat-9.0.22-windows-x64\apache-tomcat-9.0.22\conf\spring5.keystore</keystoreFile>
<keystorePass>123456</keystorePass>
<update>true</update>
<username>spring5</username>
<password>spring5</password>
</configuration>
</plugin>
- 重启Tomcat就可以了。注意https://127.0.0.1:8443/manager一定要能够访问。
Snipaste_2019-08-19_10-52-46.png
网友评论