姒傝堪
Spring Boot宸叉垚涓虹幇浠d紒涓氱骇搴旂敤寮€鍙戠殑棣栭€夋鏋躲€傚畠绠€鍖栦簡浼犵粺Java寮€鍙戠殑澶嶆潅鎬э紝骞朵负寮€鍙戣€呮彁渚涗簡鑷姩鍖栭厤缃笌渚濊禆绠$悊鐨勪究鍒┿€傛湰鏂囧皢寮曞璇昏€呭揩閫熶笂鎵婼pring Boot锛屾繁鍏ユ帰绱㈠叾鏍稿績瀹炶返锛屽苟涓轰紒涓氱骇寮€鍙戜箣鏃呮墦涓嬪潥瀹炵殑鍩虹銆?/p>
寮曡█
鍦ㄨ蒋浠跺紑鍙戦鍩燂紝Spring Boot绠€鍖栦簡浼犵粺Java寮€鍙戠殑璁稿澶嶆潅鎬э紝骞舵彁渚涗簡涓€绯诲垪寮€绠卞嵆鐢ㄧ殑鍔熻兘锛屽府鍔╁紑鍙戣€呬互蹇€熴€佺伒娲诲拰楂樻晥鐨勬柟寮忓疄鐜伴」鐩€傛湰鏂囧皢甯﹂璇昏€呴€氳繃Spring Boot蹇€熶笂鎵嬶紝骞舵繁鍏ユ帰绱㈠叾鏍稿績鐗规€э紝涓烘瀯寤轰紒涓氱骇搴旂敤鎵撲笅鍧氬疄鐨勫熀纭€銆?/p>
Spring Boot鍩虹閰嶇疆
涓€銆佸揩閫熸惌寤篠pring Boot椤圭洰鐜
鍦ㄥ紑濮嬪紑鍙戜箣鍓嶏紝纭繚鎮ㄧ殑寮€鍙戠幆澧冨凡姝g‘閰嶇疆銆係pring Boot椤圭洰閫氬父浣跨敤IntelliJ IDEA銆丒clipse鎴朣pring Initializr锛堝湪绾跨敓鎴愬櫒锛夎繘琛屽垱寤恒€備笅闈互IntelliJ IDEA涓轰緥杩涜婕旂ず锛?/p>
鍒涘缓椤圭洰锛氭墦寮€IntelliJ IDEA 鈫?鍒涘缓鏂扮殑Maven椤圭洰 鈫?閫夋嫨Spring Initializr妯℃澘 鈫?纭繚閫夋嫨Java 11鎴栨洿楂樼増鏈?鈫?濉啓椤圭洰鍚嶇О銆佺粍ID銆佸寘鍚嶇瓑鍩烘湰淇℃伅 鈫?鐐瑰嚮鈥淔inish鈥濆垱寤洪」鐩€?/p>
浜屻€佽嚜鍔ㄩ厤缃笌渚濊禆绠$悊
Spring Boot閫氳繃鑷姩閰嶇疆鍔熻兘绠€鍖栦簡閰嶇疆杩囩▼锛岃嚜鍔ㄦ娴嬪苟閰嶇疆鎵€闇€鏈嶅姟銆傞€氳繃閰嶇疆鏂囦欢application.properties鎴朼pplication.yml锛屾偍鍙互杞绘澗瀹氬埗搴旂敤琛屼负銆備緥濡傦細
application.properties绀轰緥锛?/p>
```makefile
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=example
```
鎮ㄤ篃鍙互浣跨敤YAML鏂囦欢杩涜閰嶇疆锛?/p>
application.yml绀轰緥锛?/p>
```yaml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: example
```
渚濊禆绠$悊涓庤嚜鍔ㄩ厤缃?/p>
Spring Boot閬靛惊绾﹀畾浼樹簬閰嶇疆鐨勫師鍒欙紝浣垮紑鍙戣€呰兘澶熶笓娉ㄤ簬涓氬姟閫昏緫锛岃€屾棤闇€杩囧鍏虫敞閰嶇疆缁嗚妭銆備緥濡傦紝娣诲姞Spring Web渚濊禆锛?/p>
鍦╬om.xml涓坊鍔狅細
```xml
org.springframework.boot
spring-boot-starter-web
```
杩欏皢鑷姩鎵撳寘骞堕厤缃甒eb鐩稿叧鐨勪緷璧栵紝濡係pring MVC銆丼ervlet銆丣SP绛夈€係pring Boot鏍规嵁渚濊禆鎺ㄨ崘閰嶇疆锛屽苟鎻愪緵涓板瘜鐨勫惎鍔ㄥ弬鏁版敮鎸侊紝渚夸簬璋冭瘯鍜岀敓浜ч儴缃层€?/p>
Web寮€鍙戝疄璺?/p>
涓€銆佸熀浜嶴pringfox瀹炵幇API鏂囨。鑷姩鐢熸垚
Springfox鏄竴涓嚭鑹茬殑搴擄紝鐢ㄤ簬鑷姩鐢熸垚鍩轰簬Swagger鐨凙PI鏂囨。銆傞€氳繃浣跨敤@Api銆丂ApiOperation绛夋敞瑙o紝鍙互杞绘澗瀹氫箟API鎺ュ彛鍙婂叾鎻忚堪锛屼负寮€鍙戣€呮彁渚涗究鎹风殑鏂瑰紡鐢熸垚API鏂囨。锛屾彁楂樺紑鍙戞晥鐜囧拰浠g爜鍙鎬с€傝繖涓€鐗规€у浜庡洟闃熷崗浣滃拰API绠$悊灏や负閲嶈銆係pringfox API 鏂囨。鐢熸垚涓庡墠绔〉闈㈡覆鏌揟hymeleaf妯℃澘寮曟搸鐨勪娇鐢?/p>
===============================
鍦ㄦ瀯寤虹幇浠eb搴旂敤鏃讹紝API鏂囨。鐢熸垚鍜屽墠绔〉闈㈡覆鏌撴槸涓や釜閲嶈鐨勭幆鑺傘€備笅闈紝鎴戜滑灏嗘繁鍏ユ帰璁ㄥ浣曚娇鐢⊿pringfox鍜孴hymeleaf鏉ヤ紭鍖栬繖涓や釜杩囩▼銆?/p>
涓€銆丼pringfox API 鏂囨。鐢熸垚
--------------
鍦⊿pring椤圭洰涓紝閫氳繃鏁村悎Springfox锛屾垜浠彲浠ヨ交鏉剧敓鎴怉PI鏂囨。銆傚綋浣犵殑API鎺ュ彛涓婃爣娉ㄤ簡鐗瑰畾鐨勬敞瑙e悗锛孲pringfox浼氭崟鎹夊埌杩欎簺娉ㄨВ骞剁敓鎴愬搴旂殑API鏂囨。銆傚叿浣撴搷浣滃涓嬶細
寮曞叆Springfox鐨勭浉鍏充緷璧栥€傜劧鍚庯紝鍦ㄤ綘鐨凜ontroller绫讳笂娣诲姞鐗瑰畾鐨勬敞瑙c€備緥濡傦細
```java
import springfox.documentation.annotations.;
import org.springframework.web.bind.annotation.;
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
@ApiOperation(value = "鑾峰彇鎵€鏈夌敤鎴?, notes = "杩斿洖鎵€鏈夌敤鎴峰垪琛?)
public List getAllUsers() {
// 瀹炵幇浠g爜鑾峰彇鐢ㄦ埛鍒楄〃骞惰繑鍥?/p>
}
}
```
褰撲綘杩愯docs鍛戒护鏃讹紝Springfox浼氳嚜鍔ㄦ崟鎹変綘鐨凙PI鎺ュ彛骞剁敓鎴愬搴旂殑Swagger鏂囨。銆傝繖瀵逛簬寮€鍙戣€呯鐞嗗拰鐞嗚ВAPI鎺ュ彛闈炲父鏈夊府鍔┿€傜敓鎴愮殑鏂囨。杩樺彲浠ヤ綔涓哄墠绔紑鍙戜汉鍛樺拰鍚庣寮€鍙戜汉鍛樹箣闂寸殑妗ユ锛岀‘淇濆墠鍚庣瀵规帴鏃犺銆?/p>
浜屻€佷娇鐢═hymeleaf妯℃澘寮曟搸瀹炵幇鍓嶇椤甸潰娓叉煋
---------------------
Thymeleaf鏄竴涓姛鑳藉己澶х殑妯℃澘寮曟搸锛岀壒鍒€傚悎鍦ㄧ幇浠eb搴旂敤涓疄鐜板鏉傚姩鎬侀〉闈€傞€氳繃鏁村悎Thymeleaf锛屼綘鍙互鐏垫椿鍦版帶鍒堕〉闈㈠竷灞€鍜屾暟鎹睍绀恒€備互涓嬫槸浣跨敤Thymeleaf鐨勬楠わ細
寮曞叆Thymeleaf鐨勪緷璧栥€傜劧鍚庯紝鍒涘缓涓€涓狫ava閰嶇疆绫绘潵閰嶇疆Thymeleaf銆備緥濡傦細
```java
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable(); //鍚敤榛樿Servlet澶勭悊鏈哄埗浠ユ敮鎸侀潤鎬佽祫婧愯闂瓑鐗规€с€?/p>
}
}
``` 鎺ヤ笅鏉ュ垱寤轰竴涓祫婧愭帶鍒跺櫒鏉ュ鐞嗗墠绔〉闈㈢殑璇锋眰鍜屽搷搴旓細 鎺ヤ笅鏉ュ垱寤轰竴涓祫婧愭帶鍒跺櫒鏉ュ鐞嗗墠绔〉闈㈢殑璇锋眰鍜屽搷搴旓細 ```java public class ResourceController { @GetMapping("/resources/") public String handleResourceRequests() { // 杩斿洖瀵瑰簲鐨凥TML椤甸潰鎴栧叾浠栭潤鎬佽祫婧?} } ``` 杩欐牱閰嶇疆瀹屾垚鍚庯紝浣犲氨鍙互鍦ㄥ墠绔〉闈腑浣跨敤Thymeleaf鐨勭壒鎬т簡銆備緥濡傛潯浠舵覆鏌撱€佸惊鐜€佸睘鎬х粦瀹氱瓑锛屼互鍒涘缓鍔ㄦ€佸拰浜や簰寮忛〉闈€傝繖鏍蜂綘灏卞彲浠ュ埄鐢═hymeleaf寮哄ぇ鐨勬ā鏉胯娉曟潵鍔ㄦ€佹覆鏌撳墠绔〉闈簡銆傝繖瀵逛簬鏋勫缓澶嶆潅鐨刉eb搴旂敤闈炲父鏈夊府鍔┿€俆hymeleaf杩樻敮鎸佸搷搴斿紡璁捐鍜屽浗闄呭寲绛夌壒鎬э紝浣垮緱鍓嶇寮€鍙戞洿鍔犵伒娲诲拰楂樻晥銆?涓夈€佹暣鍚圡yBatis涓嶴pring Boot绠€鍖栨暟鎹簱鎿嶄綔 鍦ㄦ瀯寤虹幇浠eb搴旂敤鏃讹紝鏁版嵁搴撴搷浣滄槸涓嶅彲鎴栫己鐨勪竴閮ㄥ垎銆侻yBatis鏄竴涓紭绉€鐨勬寔涔呭眰妗嗘灦锛屽畠绠€鍖栦簡Java寮€鍙戜腑鐨凷QL缂栧啓宸ヤ綔銆傞€氳繃鏁村悎Spring Boot鍜孧yBatis锛屾垜浠彲浠ヨ交鏉惧疄鐜板璞′笌鏁版嵁搴撹〃鐨勬棤缂濇槧灏勩€?棣栧厛寮曞叆MyBatis鐨勪緷璧栫劧鍚庡湪閰嶇疆绫讳腑閰嶇疆MyBatis 杩欐牱浣犲氨鍙互浣跨敤MyBatis鏉ョ畝鍖栦綘鐨勬暟鎹簱鎿嶄綔浜嗐€傚悓鏃朵綘杩樺彲浠ョ粨鍚圫pring Boot鐨勪簨鍔$鐞嗗姛鑳藉拰鏁版嵁搴撹繛鎺ユ睜鏉ユ彁楂樹綘鐨勫簲鐢ㄧ殑鎬ц兘鍜岀ǔ瀹氭€с€?鎬讳箣閫氳繃鏁村悎Springfox鍜孴hymeleaf浠ュ強MyBatis浣犲彲浠ヨ交鏉惧湴鐢熸垚API鏂囨。瀹炵幇鍓嶇椤甸潰娓叉煋浠ュ強绠€鍖栨暟鎹簱鎿嶄綔浠庤€屾瀯寤轰竴涓珮鏁堢ǔ瀹氱殑鐜颁唬Web搴旂敤銆?閰嶇疆浜嬪姟绠$悊
@Configuration
@EnableTransactionManagement
public class TransactionConfig {
鏁版嵁婧愰厤缃?/p>
@Bean
public DataSource dataSource() {
// 閰嶇疆 HikariCP 杩炴帴姹犳垨鍏朵粬杩炴帴姹犲疄鐜版柟寮?/p>
HikariDataSource ds = new HikariDataSourceBuilder().build(); // 瀹炵幇楂樻晥銆佺ǔ瀹氱殑鏁版嵁搴撹繛鎺ユ睜閰嶇疆
return ds;
}
浜嬪姟绠$悊鍣ㄩ厤缃?/p>
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new TransactionManager(dataSource); // 鎻愪緵浜嬪姟绠$悊鏀寔锛岀‘淇濇暟鎹竴鑷存€?/p>
}
}
--- 缁撳熬鍒嗗壊绾?--- 浠ヤ笅鏄叧浜庡畨鍏ㄤ笌璁よ瘉鐨勫唴瀹癸細
--- 鍒嗗壊绾?--- 馃専瀹夊叏涓庤璇侌煂?馃専瀹炵幇鍩轰簬JWT鐨勭敤鎴疯璇佷笌鎺堟潈馃専 馃摐 JSON Web Tokens锛圝WT锛夎璇佹満鍒朵粙缁?馃敀 JWT鏄竴绉嶈交閲忕骇鐨勮璇佹満鍒讹紝灏ゅ叾鍦ㄥ井鏈嶅姟鏋舵瀯涓樉寰楀挨涓哄疄鐢ㄣ€傚湪Spring Boot搴旂敤涓紝杞绘澗閰嶇疆JWT锛屽彲浠ョǔ鍥哄疄鐜扮敤鎴疯璇佷笌鎺堟潈銆傪煋?寮曞叆JWT渚濊禆: 椤圭洰涓渶瑕佸紩鍏ョ浉鍏崇殑JWT渚濊禆搴擄紝濡?`io.jsonwebtoken:jjwt-api:0.11.2` 鍜?`com.auth0:java-jwt:3.11.1`銆傪煍?鍒涘缓JWT浠ょ墝鐢熸垚鍣? 閫氳繃鍒涘缓涓€涓悕涓篔wtTokenGenerator鐨勭粍浠讹紝鍒╃敤棰勮鐨勫瘑閽ワ紙濡俙secret_key`锛夛紝鐢熸垚甯︽湁鐢ㄦ埛淇℃伅鐨凧WT浠ょ墝銆傚綋闇€瑕侀獙璇佺敤鎴疯韩浠芥椂锛屽彧闇€楠岃瘉浠ょ墝鏄惁鏈夋晥鍜屾槸鍚﹁绡℃敼鍗冲彲銆備緥濡傦細鍒╃敤`Jwts.builder().setSubject(username).signWith(...)`绛夋柟娉曟瀯寤轰护鐗屻€傛暣鍚圫pring Security鍔犲己搴旂敤瀹夊叏鎬?鍦ㄥ疄闄呭簲鐢ㄤ腑锛屾垜浠€氬父浼氱粨鍚圫pring Security鏉ヨ繘涓€姝ュ寮哄簲鐢ㄧ殑瀹夊叏鎬с€係pring Security鏄竴涓己澶х殑瀹夊叏妗嗘灦锛屾彁渚涗簡鍏ㄩ潰鐨勮韩浠介獙璇佸拰鎺堟潈鍔熻兘銆傞€氳繃鏁村悎Spring Security锛屽彲浠ヨ交鏉句繚鎶ゅ簲鐢ㄧ殑鍚勪釜灞傞潰锛屼粠鍓嶇鍒板悗绔兘鏃犻渶鎷呭績瀹夊叏鎬ч棶棰樸€傛棤璁烘槸API鎺ュ彛杩樻槸Web椤甸潰锛岄兘鍙互寰楀埌鍏ㄩ潰鐨勫畨鍏ㄩ槻鎶ゃ€傞€氳繃鏁村悎Spring Security涓嶫WT璁よ瘉鏈哄埗锛屽彲浠ュ疄鐜颁竴涓畨鍏ㄣ€侀珮鏁堢殑璁よ瘉涓庢巿鏉冩祦绋嬶紝涓烘偍鐨凷pring Boot搴旂敤淇濋┚鎶よ埅銆係pring Security涓嶴pring Cloud鐨勫井鏈嶅姟瀹夊叏閰嶇疆鍙婇儴缃叉墿灞?/p>
鍦ㄦ瀯寤轰紒涓氱骇寰湇鍔″簲鐢ㄦ椂锛屽畨鍏ㄦ€у拰閮ㄧ讲鎵╁睍鎬ф槸涓や釜鑷冲叧閲嶈鐨勬柟闈€傛湰鏂囧皢缁撳悎Spring Security鍜孲pring Cloud鏉ユ帰璁ㄥ浣曚负寰湇鍔℃灦鏋勬彁渚涘己澶х殑瀹夊叏淇濋殰锛屽苟瀹炵幇楂樻晥鐨勯儴缃层€?/p>
涓€銆丼pring Security鐨勫畨鍏ㄩ厤缃?/p>
璁╂垜浠叧娉⊿pring Security鐨勯厤缃€備互涓嬫槸涓€涓吀鍨嬬殑Spring Security閰嶇疆绫荤ず渚嬶紝鐢ㄤ簬璁剧疆JWT锛圝son Web Token锛夎璇佸拰OAuth2鐢ㄦ埛鏈嶅姟闆嗘垚銆?/p>
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtTokenGenerator jwtTokenGenerator;
@Autowired
private OAuth2UserService oAuth2UserService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/v1/auth/").permitAll() // 鍏佽鎵€鏈夎闂?api/v1/auth/鐨勮姹傞€氳繃
.anyRequest().authenticated() // 鍏朵粬鎵€鏈夎姹傞渶瑕佽璇?/p>
.and()
.csrf().disable() // 鍏抽棴璺ㄧ珯璇锋眰浼€犱繚鎶?/p>
.cors().disable() // 鍏抽棴璺ㄥ煙淇濇姢
.formLogin().disable() // 鍏抽棴琛ㄥ崟鐧诲綍鏀寔
娣卞叆鎺㈢储涓庡疄璺礢pring Boot鎶€鏈鍩熺殑鏃犲敖濂ョ
鍦ㄤ笉鏂彉鍖栫殑绉戞妧娴疆涓紝Spring Boot濡傚悓涓€棰楃拃鐠ㄧ殑鏄庣彔锛屽惛寮曠潃鏃犳暟寮€鍙戣€呯殑鐩厜銆傚€熷姪鎱曡缃戠瓑鍦ㄧ嚎璧勬簮锛屾垜浠彲浠ヨ幏鍙栨洿澶氬叧浜嶴pring Boot鎶€鏈殑娣卞叆瀛︿範涓庡疄璺垫渚嬨€傚湪杩欓噷锛屾垜浠皢涓€鍚屾彮寮€Spring Boot鐨勭绉橀潰绾憋紝鎺㈠鍏惰儗鍚庡己澶х殑鎶€鏈疄鍔涖€?/p>
Spring Boot浠ュ叾绠€娲併€佺伒娲荤殑鐗规€у拰涓板瘜鐨勫姛鑳斤紝璧㈠緱浜嗗紑鍙戣€呯殑骞挎硾璧炶獕銆傛棤璁烘槸鍒濆鑰呰繕鏄祫娣卞紑鍙戣€咃紝閮借兘鍦ㄨ繖閲屾壘鍒板睘浜庤嚜宸辩殑瀛︿範璺緞銆傚湪Spring Boot鐨勪笘鐣岄噷锛屾垜浠彲浠ユ劅鍙楀埌鎶€鏈殑鏃犻檺榄呭姏銆?/p>
閫氳繃涓嶆柇璁块棶鍦ㄧ嚎璧勬簮锛屾垜浠彲浠ヤ簡瑙e埌Spring Boot鐨勬渶鏂板姩鎬佸拰鎶€鏈秼鍔裤€傛棤璁烘槸鍏充簬Spring Boot鐨勫熀纭€鍏ラ棬锛岃繕鏄叧浜庨珮绾х壒鎬х殑娣卞害瑙f瀽锛岃繖浜涜祫婧愰兘鑳戒负鎴戜滑鎻愪緵涓板瘜鐨勭煡璇嗗拰瀹炶返妗堜緥銆傛垜浠彲浠ュ湪杩欓噷瀛︿範鍒板浣曡繍鐢⊿pring Boot鏋勫缓楂樻晥鐨勫簲鐢ㄧ▼搴忥紝濡備綍浼樺寲鎬ц兘锛屽浣曡В鍐冲父瑙佺殑闂绛夈€?/p>
鎴戜滑杩樺彲浠ュ湪杩欎簺璧勬簮涓壘鍒颁笌Spring Boot鐩稿叧鐨勬妧鏈爤瀛︿範涓庡疄璺垫渚嬨€傛瘮濡傦紝鎴戜滑鍙互瀛︿範濡備綍浣跨敤Spring Cloud涓嶴pring Boot缁撳悎锛屾瀯寤哄井鏈嶅姟鏋舵瀯锛涘浣曢泦鎴怱pring Security杩涜鏉冮檺绠$悊锛涘浣曠粨鍚圧eact鎴朧ue绛夊墠绔鏋讹紝鏋勫缓鍓嶅悗绔竴浣撳寲鐨勮В鍐虫柟妗堢瓑銆傝繖浜涙妧鏈爤鐨勫涔犲拰瀹炶返锛屽皢浣挎垜浠洿鍔犲叏闈㈠湴鎺屾彙Spring Boot鎶€鏈紝涓烘湭鏉ョ殑椤圭洰寮€鍙戞墦涓嬪潥瀹炵殑鍩虹銆?/p>
閫氳繃涓嶆柇瀛︿範鍜屽疄璺碉紝鎴戜滑灏嗚兘澶熸繁鍏ョ悊瑙pring Boot鎶€鏈殑鏍稿績鍘熺悊鍜屽簲鐢ㄥ疄璺点€傝鎴戜滑涓€璧疯笍涓婅繖涓妧鏈帰绱箣鏃咃紝鍏卞悓鎻紑Spring Boot鍙婂叾鐩稿叧鎶€鏈鍩熺殑鏃犲敖濂ョ鍚э紒 |