欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解SpringBoot應(yīng)用服務(wù)啟動與安全終止

 更新時間:2019年04月29日 09:54:20   作者:wangshuang1631  
這篇文章主要介紹了SpringBoot應(yīng)用服務(wù)啟動與安全終止,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

SpringBoot應(yīng)用服務(wù)啟動

參照官方示例工程可以快速搭建簡單SpringBoot應(yīng)用,官方連接如下:http://projects.spring.io/spring-boot/#quick-start
閑話少敘,上代碼:

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

  @RequestMapping("/")
  @ResponseBody
  String home() {
    return "Hello World!";
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(SampleController.class, args);
  }
}

通過該main()函數(shù)作為入口,可以啟動SpringBoot服務(wù)。雖然這里只有幾行代碼,當(dāng)時已經(jīng)是一個完整的web程序。

有幾個注解需要特殊說明一下,我在開發(fā)的時候就在這幾個注解上吃了不少虧。

@EnableAutoConfiguration:這個注解告訴Spring Boot根據(jù)添加的jar依賴猜測你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發(fā)一個web應(yīng)用并相應(yīng)地對Spring進(jìn)行設(shè)置。

@ComponentScan:注解搜索beans,并結(jié)合@Autowired構(gòu)造器注入。

@SpringBootApplication:等價于以默認(rèn)屬性使用@Configuration,@EnableAutoConfiguration和@ComponentScan,不過該注解只搜索該包同級的包和下級的包!?。。。?!

SpringBoot應(yīng)用安全終止

由于SpringBoot集成了tomcat,所以當(dāng)SpringBoot應(yīng)用啟動之后,不能像對普通的tomcat操作一下來操作SpringBoot,不過SpringBoot封裝了啟動,停止的方法。

SpringBoot,作為Spring框架對“約定優(yōu)先于配置(Convention Over Configuration)”理念的最佳實(shí)踐的產(chǎn)物,它能幫助我們很快捷的創(chuàng)建出獨(dú)立運(yùn)行、產(chǎn)品級別的基于Spring框架的應(yīng)用,大部分Spring Boot應(yīng)用只需要非常少的配置就可以快速運(yùn)行起來,是一個與微服務(wù)(MicroServices)相當(dāng)契合的微框架。

主要有兩種方式:通過HTTP發(fā)送shutdown信號,或者通過service stop的方式,本文只介紹HTTP方式。

1.在pom.xml中引入actuator依賴

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.在application.properties文件開啟shutdown endpoint,SpringBoot的endpoints.shutdown.enabled默認(rèn)是關(guān)閉的。

#啟用shutdown
endpoints.shutdown.enabled=true
#禁用密碼驗(yàn)證
endpoints.shutdown.sensitive=false

3.發(fā)送停止信號,使用curl向服務(wù)器發(fā)送post請求:

curl -X POST host:port/shutdown

 將會得到返回消息如下:

{"message":"Shutting down, bye..."}

4.可以看出此方法非常方便,但是也很不安全,正式使用時,必須對該請求進(jìn)行必要的安全設(shè)置,可以借助spring-boot-starter-security進(jìn)行身份認(rèn)證:
pom.xml添加security依賴

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.properties中變更配置

#開啟shutdown的安全驗(yàn)證
endpoints.shutdown.sensitive=true
#驗(yàn)證用戶名
security.user.name=admin
#驗(yàn)證密碼
security.user.password=secret
#角色
management.security.role=SUPERUSER

 指定路徑、IP、端口

#指定shutdown endpoint的路徑
endpoints.shutdown.path=/custompath
#也可以統(tǒng)一指定所有endpoints的路徑`management.context-path=/manage`
#指定管理端口和IP
management.port=8081
management.address=127.0.0.1

 以上所述是小編給大家介紹的SpringBoot應(yīng)用服務(wù)啟動與安全終止詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論