Spring Boot的web开发(2)

Tomcat配置
Spring Boot默认内嵌的tomcat是Servlet容器
1.配置Tomcat
关于Tomcat所有属性都在org.springframework.bott.autoconfigure.web.ServerProperties配置类中作了定义,我们只需要在application.properties配置属性做配置即可。
通用的Servlet容器配置都以“server”为前缀
Tomcat特有配置以“server.tomcat”为前缀。
配置Servlet容器:
server.port=#配置程序端口,默认为8080
server.session-timeout=#用户会话session过期时间,以秒为单位
server.context-path=#配置访问路径,默认为/
配置Tomcat:
server.tomcat.uri-encoding=#配置Tomcat编码,默认为UTF-8
server.tomcat.compression=#Tomcat是否开启压缩,默认为关闭off

2.代码配置Tomcat
通过代码的方式配置servlet容器,可以注册一个实现 EmbeddedServletContainerCustomizer接口的Bean,
若想直接配置Tomcat、Jetty、Undertow,则直接定义TomcatEmbeddedServletContainerFactory、 JettyEmbeddedServletContainerFactory、 UndertowEmbeddedServletContainerFactory
2.1 通用配置
(1)新建类的配置
package com.hand;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.http.HttpStatus;

import java.util.concurrent.TimeUnit;

/**
* Created by lqy on 2017-11-27.
*/
public class CustomServletContainer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container){
container.setPort( 8888 );
container.addErrorPages( new ErrorPage(HttpStatus. NOT_FOUND , "/404.html" ));
container.setSessionTimeout( 10 , TimeUnit. MINUTES );
}
}
(2)当前配置文件内配置
在当前已有的配置文件内添加类的Bean,在Spring配置中, 当前类要声明为static
@SpringBootApplication
public class SpringThymeleafApplication {
@Component
public static class CustomServletContainer implements EmbeddedServletContainerCustomizer{
@Override
public void customize(ConfigurableEmbeddedServletContainer container){
container.setPort( 8888 );
container.addErrorPages( new ErrorPage(HttpStatus. NOT_FOUND , "/404.html" ));
container.setSessionTimeout( 10 , TimeUnit. MINUTES );
}
}
public static void main(String[] args) {
SpringApplication. run (SpringThymeleafApplication. class , args);
}
}
2.2 特定配置
以Tomcat为例( Jetty使用 JettyEmbeddedServletContainerFactory、Undertow使用 UndertowEmbeddedServletContainerFactory
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory factory= new TomcatEmbeddedServletContainerFactory();
factory.setPort( 8888 ); //配置端口号
//配置错误页面,根据HttpStatus中的错误状态信息,直接转向错误页面,其中404.html放置在src/main/resources/static下即可
factory.addErrorPages( new ErrorPage(HttpStatus. NOT_FOUND , "/404.html" ));
//配置Servlet容器用户会话(session)过期时间
factory.setSessionTimeout( 10 , TimeUnit. MINUTES );
return factory;
}

3.替换Tomcat
Spring Boot默认使用Tomcat作为内嵌Servlet容器,查看spring-boot-starter-web依赖。
(1)替换成Jetty
在pom.xml中,将 spring-boot-starter-web依赖由 spring-boot-starter-tomcat替换为spring-boot-starter-jetty.
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-web </ artifactId >
< exclusions >
< exclusion >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-tomcat </ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-jetty </ artifactId >
</ dependency >
此时启动Spring Boot

(2)替换成Undertow
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-web </ artifactId >
< exclusions >
< exclusion >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-tomcat </ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-undertow </ artifactId >
</ dependency >
4.SSL配置
SSL(Secure Sockets Layer,安全套接层)为网络通信提供安全及数据完整性的一种安全协议。
SSL在网络传输层对网络连接加密。
SSL协议位于TCP/IP协议与各种应用层协议之间,为数据通信提供安全支持。
SSL协议分为两层:
SSL记录协议(SSL Record Protocol)建立在可靠的传输协议(TCP等)之上,为高层协议提供数据封装、压缩、加密等支持
SSL握手协议(SSL Handshake Protocol)建立在SSL记录协议之上,用于在实际数据传输开始前,通信双方进行身份认证、协商加密算法、交换加密密钥等。

基于B/S的Web应用,通过HTTPS实现SSL,HHTPS是HTTP的安全版,即在HTTP下加入SSL层,HTTPS安全基础是SSL。

Spring Boot用的是内嵌的Tomcat,因此做SSL配置时需要做如下操作:
1.生成证书
使用SSL首先需要一个证书,这个证书既可以自签名也可以从SLL证书授权中心得到。
每一个JDK或JRE都有keytool,这是证书管理工具,用来生成自签名证书。
配置好JAVA_HOME,并将JAVA_HOME的bin目录加入Path后,可以在控制台当前目录生成自签名证书.keystore:keytool -genkey -alias tomcat

2.Spring Boot配置SSL
添加Index.html到src/main/resources/static下,作为测试。
将.keystore文件复制到项目根目录,然后再application.properties中做SSL配置
server.port = 8443
server.ssl.key-store = .keystore
server.ssl.key-store-password = 123456
server.ssl.keyStoreType = JKS
server.ssl.keyAlias : tomcat

启动Spring Boot


3.http转向https
要实现这个功能,我们需配置 TomcatEmbeddedServletContainerFactory,并且添加Tomcat的connector实现。我们需要在配置文件新增如下。(下面为了省事改造了入口类,可以另作一个配置类)
package com.hand;

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.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation. Bean ;

@SpringBootApplication
public class SpringBootSslApplication {

public static void main(String[] args) {
SpringApplication. run (SpringBootSslApplication. class , args);
}
@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( 8443 );
return connector;
}
}
此时访问http://localhost:8080会自动跳转到 https://localhost:8443
Favicon配置
1 默认的Favicon
Spring Boot提供了一个默认的 Favicon,每次访问应用时候就能看到。

2 关闭Favicon
可以在application.properties设置关闭Favicon,默认为开启。
spring.mvc.favicon.enabled = false
3 设置自己的Favicon
若需要设置自己的Favicon,则只需将自己的favicon.ico(文件名不能变动)文件放置在类路径根目录、类路径META-INF/resources/下、类路径resources/下、类路径static/下或类路径public/下。这里放置在src/main/resources/static下


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值