Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | About

5.35. application.properties

配置資源檔案位置,預設application.properties是放在jar包中的,通過spring.config.location可以制定外部配置檔案,這樣更便于運維。

	
java -jar demo.jar --spring.config.location=/opt/config/application.properties
	
	

5.35.1. 加載排除

			
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration			
			
		

5.35.2. PID FILE

		
spring.pid.fail-on-write-error= # Fail if ApplicationPidFileWriter is used but it cannot write the PID file.
spring.pid.file= # Location of the PID file to write (if ApplicationPidFileWriter is used).		
		
		

5.35.3. 內嵌 tomcat server

5.35.3.1. server.tomcat.basedir

設置 Tomcat 工作目錄,預設 /tmp/tomcat-docbase.7057591687859485145.7000 通過下面配置修改

			
server.tomcat.basedir=/tmp/your_project
			
			
5.35.3.2. server
			
server.port=8080 # 監聽連接埠
server.address= # 綁定的地址
server.context-path= #預設為/					
			
			

連接數配置

				
server.tomcat.max-threads=2048 # 最大綫程數				
				
			
5.35.3.3. access.log

如果前端有 nginx 代理這個配置可以禁用

				
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=/tmp/logs
server.tomcat.accesslog.pattern=common
server.tomcat.accesslog.prefix=www.netkiller.cn.access
server.tomcat.accesslog.suffix=.log				
				
			
5.35.3.4. logging
				logging.path=/tmp # 日誌目錄預設為 /tmp
				logging.file=your.log # 日誌檔案名稱,預設為spring.log
			
				java -jar spring-boot-app.jar --logging.file=/tmp/spring-boot-app.log
			
5.35.3.5. charset

Spring boot 預設並非UTF-8 所以下面配置必設,否則將會出現

				spring.messages.encoding=UTF-8
				server.tomcat.uri-encoding=UTF-8
				spring.http.encoding.charset=UTF-8
				spring.http.encoding.enabled=true
				spring.http.encoding.force=true
			
5.35.3.6. Session 配置
				server.session.persistent 重啟時是否持久化session,預設false
				server.session.timeout session的超時時間
				server.session.tracking-modes 設定Session的追蹤模式(cookie, url, ssl).
				server.session.timeout=1800 #session有效時長
			
5.35.3.7. cookie
				server.session.cookie.comment 指定session cookie的comment
				server.session.cookie.domain 指定session cookie的domain
				server.session.cookie.http-only 否開啟HttpOnly.
				server.session.cookie.max-age 設定session cookie的最大age.
				server.session.cookie.name 設定Session cookie 的名稱.
				server.session.cookie.path 設定session cookie的路徑.
				server.session.cookie.secure 設定session cookie的“Secure” flag.
			

案例

				server.session.cookie.name=PHPSESSID
				server.session.cookie.domain=.example.com
				server.session.cookie.http-only=true
				server.session.cookie.path=/
			
5.35.3.8. error 路徑
				server.error.path=/error
			
5.35.3.9. 壓縮傳輸
				server.compression.enabled=true #是否開啟壓縮,預設為false.
				server.compression.excluded-user-agents #指定不壓縮的user-agent,多個以逗號分隔,預設值為:text/html,text/xml,text/plain,text/css
				server.compression.mime-types #指定要壓縮的MIME type,多個以逗號分隔.
				server.compression.min-response-size #執行壓縮的閾值,預設為2048

				server.compression.enabled=true
				server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,text/css,application/javascript
				server.compression.min-response-size=1024
			
5.35.3.10. server.servlet.context-path
				server.servlet.context-path=/your
			
5.35.3.11. ssl
				server.ssl.ciphers 是否支持SSL ciphers.
				server.ssl.client-auth 設定client authentication是wanted 還是 needed.
				server.ssl.enabled 是否開啟ssl,預設: true
				server.ssl.key-alias 設定key store中key的別名.
				server.ssl.key-password 訪問key store中key的密碼.
				server.ssl.key-store 設定持有SSL certificate的key store的路徑,通常是一個.jks檔案.
				server.ssl.key-store-password設定訪問key store的密碼.
				server.ssl.key-store-provider設定key store的提供者.
				server.ssl.key-store-type 設定key store的類型.
				server.ssl.protocol 使用的SSL協議,預設: TLS
				server.ssl.trust-store 持有SSL certificates的Trust store.
				server.ssl.trust-store-password訪問trust store的密碼.
				server.ssl.trust-store-provider設定trust store的提供者.
				server.ssl.trust-store-type 指定trust store的類型.
			

生成證書

server.ssl.* #ssl相關配置

				keytool -genkey -alias springboot -keyalg RSA -keystore /www/ssl/spring
				設置密碼 123456
			

配置 application.properties

				server.ssl.enabled 啟動tomcat ssl配置
				server.ssl.keyAlias 別名
				server.ssl.keyPassword 密碼
				server.ssl.keyStore 位置
			
				server.port=8443
				server.ssl.enabled=true
				server.ssl.keyAlias=springboot
				server.ssl.keyPassword=123456
				server.ssl.keyStore=/www/ssl/spring
			

5.35.4. JSON 輸出與日期格式化

			# Pretty-print JSON responses
			spring.jackson.serialization.indent_output=true
		
			
#序列化時間格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
#mvc序列化時候時區選擇
spring.jackson.time-zone=GMT+8			
			
		

5.35.5. SMTP 相關配置

			spring.mail.host=smtp.netkiller.cn
			#spring.mail.username=
			#spring.mail.password=
			#spring.mail.properties.mail.smtp.auth=true
			#spring.mail.properties.mail.smtp.starttls.enable=true
			#spring.mail.properties.mail.smtp.starttls.required=true
			mail.smtp.debug=true
		

5.35.6. Redis

			# REDIS (RedisProperties)
			# Redis資料庫索引(預設為0)
			spring.redis.database=0
			# Redis伺服器地址
			spring.redis.host=localhost
			# Redis伺服器連接連接埠
			spring.redis.port=6379
			# Redis伺服器連接密碼(預設為空)
			spring.redis.password=
			# 連接池最大連接數(使用負值表示沒有限制)
			spring.redis.pool.max-active=8
			# 連接池最大阻塞等待時間(使用負值表示沒有限制)
			spring.redis.pool.max-wait=-1
			# 連接池中的最大空閒連接
			spring.redis.pool.max-idle=8
			# 連接池中的最小空閒連接
			spring.redis.pool.min-idle=0
			# 連接超時時間(毫秒)
			spring.redis.timeout=0
		

5.35.7. MongoDB

格式:mongodb://用戶名:密碼@主機地址/資料庫

			spring.data.mongodb.uri=mongodb://user:passw0rd@mdb.netkiller.cn/test
			spring.data.mongodb.repositories.enabled=true
		

5.35.8. MySQL

			spring.datasource.driver-class-name=com.mysql.jdbc.Driver
			spring.datasource.url=jdbc:mysql://主機地址:連接埠號/資料庫
			spring.datasource.username=用戶名
			spring.datasource.password=密碼
			spring.jpa.database=MYSQL # 啟用JPA支持
		

5.35.9. Oracle

			spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
			spring.datasource.url=jdbc:oracle:thin:@//odb.netkiller.cn:1521/orcl
			spring.datasource.username=orcl
			spring.datasource.password=passw0rd
			spring.datasource.connection-test-query="SELECT 1 FROM DUAL"
			spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

			spring.jpa.show-sql=true
			#spring.jpa.hibernate.ddl-auto=none
			#spring.jpa.hibernate.ddl-auto=create-drop
			spring.datasource.max-active=50
			spring.datasource.initial-size=5
			spring.datasource.max-idle=10
			spring.datasource.min-idle=5
			spring.datasource.test-while-idle=true
			spring.datasource.test-on-borrow=false
			spring.datasource.validation-query=SELECT 1 FROM DUAL
			spring.datasource.time-between-eviction-runs-millis=5000
			spring.datasource.min-evictable-idle-time-millis=60000
		

5.35.10. default_schema

			spring.jpa.properties.hibernate.default_schema=schema
		

5.35.11. datasource

啟用/禁用 導入 schema.sql 和 data.sql / data-${platform}.sql 其中 platform 是 spring.datasource.platform 所定義的平台

			spring.datasource.initialize=false
			spring.datasource.platform=MYSQL
		

5.35.12. velocity

			spring.velocity.resourceLoaderPath=classpath:/templates/
			spring.velocity.prefix=
			spring.velocity.suffix=.vm
			spring.velocity.cache=false
			spring.velocity.check-template-location=true
			spring.velocity.content-type=text/html
			spring.velocity.charset=UTF-8
			spring.velocity.properties.input.encoding=UTF-8
			spring.velocity.properties.output.encoding=UTF-8
		

禁用 velocity 模板引擎

			spring.velocity.enabled=false
			spring.velocity.check-template-location=false
		

5.35.13. Security 相關配置

			security.user.name=user
			security.user.password=password
			security.user.role=USER
		

Web 安全

			# X-Frame-Options: DENY
			security.headers.frame=false

			security.headers.cache
			security.headers.content-type
			security.headers.hsts
			security.headers.xss
		

參考 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java#L171

5.35.14. MVC 配置

是否支持favicon.ico,預設為: true

		
spring.mvc.favicon.enabled=false
		
		

5.35.15. Kafka 相關配置

		
spring.kafka.bootstrap-servers
spring.kafka.client-id
spring.kafka.ssl.key-password
spring.kafka.ssl.keystore-location
spring.kafka.ssl.keystore-password
spring.kafka.ssl.truststore-location
spring.kafka.ssl.truststore-password
spring.kafka.template.default-topic
spring.kafka.consumer.auto-commit-interval
spring.kafka.consumer.auto-offset-reset
spring.kafka.consumer.bootstrap-servers
spring.kafka.consumer.client-id
spring.kafka.consumer.enable-auto-commit
spring.kafka.consumer.fetch-max-wait
spring.kafka.consumer.fetch-min-size
spring.kafka.consumer.group-id
spring.kafka.consumer.heartbeat-interval
spring.kafka.consumer.key-deserializer
spring.kafka.consumer.max-poll-records
spring.kafka.consumer.value-deserializer
spring.kafka.listener.ack-count
spring.kafka.listener.ack-mode
spring.kafka.listener.ack-time
spring.kafka.listener.concurrency
spring.kafka.listener.poll-timeout
spring.kafka.producer.acks
spring.kafka.producer.batch-size
spring.kafka.producer.bootstrap-servers
spring.kafka.producer.buffer-memory
spring.kafka.producer.client-id
spring.kafka.producer.compression-type
spring.kafka.producer.key-serializer
spring.kafka.producer.retries
spring.kafka.producer.value-serializer
spring.kafka.properties