This document contains code snippets related to Spring Security configuration and authentication. It defines classes and methods for configuring security, processing login requests, loading user details, and authenticating users. Key aspects include configuring security filters and authorization rules, processing username/password authentication, validating login credentials against encoded passwords, and loading pre-authenticated users based on access tokens.
AWS Japan YouTube 公式チャンネルでライブ配信された 2022年4月26日の AWS Developer Live Show 「Infrastructure as Code 談議 2022」 の資料となります。 当日の配信はこちら からご確認いただけます。
https://youtu.be/ed35fEbpyIE
This document contains code snippets related to Spring Security configuration and authentication. It defines classes and methods for configuring security, processing login requests, loading user details, and authenticating users. Key aspects include configuring security filters and authorization rules, processing username/password authentication, validating login credentials against encoded passwords, and loading pre-authenticated users based on access tokens.
AWS Japan YouTube 公式チャンネルでライブ配信された 2022年4月26日の AWS Developer Live Show 「Infrastructure as Code 談議 2022」 の資料となります。 当日の配信はこちら からご確認いただけます。
https://youtu.be/ed35fEbpyIE
17. #jjug_ccc #ccc_e5
目次
(C) CASAREAL, Inc. All rights reserved. 17
1.新しいコースを作ることになった背景
2.Spring Bootによるサーバサイド開発
3.Angularによるクライアント開発
4.どう組み合わせるの?
18. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 18
Pivotal社が中心に開発
Spring Framework の設定地獄から脱却を目指したもの
Starter ライブラリで Maven の dependency の記述を
減らせる
Auto Configuration で必要な設定は済んでいる
Spring Bootとは
20. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 20
https://start.spring.io/
Web 上でひな型を作成できる
IntelliJ IDEA や STS などの主要 IDE も連携している
今回は
• Maven
• Java 11
• Dependencies Spring Web
で作成
Spring Initializrで楽々初期構築
23. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 23
Spring MVC による Webアプリの開発用 Starter
内部に組み込み Tomcat を持つ
Javaアプリとして起動するだけで Webアプリとして
立ち上がる
JSON ⇔ Java オブジェクトの変換機能をあらかじ
め持っている
spring-boot-starter-web
24. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 24
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
pom.xmlにこれを追加するだけで、
Webアプリに必要な設定が完了する
spring-boot-starter-web
25. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 25
デフォルトでは jar ファイルが出来上がる
必要となるライブラリが内包される
実行するだけで Webアプリとして立ち上がる
パッケージングで出来上がるもの
26. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 26
DispatcherServlet
JSONを送信
RestControllerRestControllerRestControllerRestController
リクエストでは
JSON ⇒ Java オブジェクト
に変換される
レスポンスでは
Java オブジェクト ⇒ JSON
に変換される
JSONを返却
RESTとしてのSpring Boot
27. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 27
@RestController
@RequestMapping("/api/tasks")
public class TaskRestController {
@PostMapping
public ResponseEntity order(@RequestBody TaskRequest taskRequest) {
// 要件に応じた処理
return ResponseEntity.created(uri).build();
}
}
RESTful Web Service
が構築できる
Javaオブジェクトを受け取り返すだけで
28. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 28
static locations
src/main/resources/static 配下に静的コンテンツ
を配置することでクライアントに返却できる
Welcome Page
src/main/resources/static 配下に置いた
index.html が自動的に Welcome Page に設定され
トップページアクセスすると返却される
Webサーバとしての機能も充実
29. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 29
http://localhost:8080
にアクセスすると返却される
http://localhost:8080/js/index.js
とリクエストすると返却される
Webサーバとしての機能も充実
30. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 30
Spring Security を導入する
Spring Boot なら Starterライブラリを追加するだけ
ログイン・ログアウトだけじゃない!
クロスサイト・リクエストフォージェリ(CSRF)対策も
デフォルトでサポート
しかも、Angular アプリ向けのモードあり
もちろん新規コースで取り扱います!
Security対策も楽々!
31. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 31
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
pom.xmlにこれを追加するだけで、
Spring Securityが使えるようになる
spring-boot-starter-security
32. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 32
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository
.withHttpOnlyFalse());
}
} Security用のJavaConfigでこの太字の
部分を追加するだけ!
Angularアプリ用のCSRF対策の設定
33. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 33
レスポンスヘッダーの Cookie に XSRF-TOKEN という
名前で CSRFトークンを発行する
デフォルトの発行先は HttpSession
Spring Security 4.1 からある
実は、Angularの動作に合わせたもの
CookieCsrfTokenRepositoryとは
34. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 34
A CsrfTokenRepository that persists the CSRF token in a
cookie named "XSRF-TOKEN" and reads from the header
"X-XSRF-TOKEN" following the conventions of AngularJS.
AngularJSの規則に従ってヘッダー「X-XSRF-TOKEN」か
らCSRFトークンから読み取る
Spring Securityのドキュメントにも
35. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 35
正規のWebサイト
①購入ページをリクエスト
②CSRFトークンを発行し、
レスポンスヘッダーのCookieに含める
③CookieからCSRFトークンを取得し、
リクエストボディ(またはヘッダー)に含めて
購入リクエストを送信
④CookieのCSRFトークンと
リクエストボディのCSRFトークンを比較し、
同じであれば購入OKとする
Cookieを利用したCSRF対策の動作概要
36. #jjug_ccc #ccc_e5
目次
(C) CASAREAL, Inc. All rights reserved. 36
1.新しいコースを作ることになった背景
2.Spring Bootによるサーバサイド開発
3.Angularによるクライアント開発
4.どう組み合わせるの?
37. #jjug_ccc #ccc_e5
自己紹介:菊池真登
(C) CASAREAL, Inc. All rights reserved. 37
研修トレーナー@カサレアル
登壇実績:Spring / Java SE
開発歴[Spring]:2012年から
開発歴[Angular]:2015年頃に
1.5年間AngularJSに触れて冬眠
今回新規コースのコンテンツ
作成を任命される
38. #jjug_ccc #ccc_e5
(C) CASAREAL, Inc. All rights reserved. 38
Google製のフレームワーク
JavaScript上で動作するWebアプリケーション用
フルスタック
TypeScript推奨
Angularとは
71. #jjug_ccc #ccc_e5
Spring MVC で CORS 対応
(C) CASAREAL, Inc. All rights reserved. 71
@RestController
@RequestMapping("/api/tasks")
public class TaskRestController {
@GetMapping
@CrossOrigin
public List<TaskResponse> findAll() {
return this.todoService.findAll().stream()
.map(todo -> new TaskResponse(todo))
.collect(Collectors.toList());
}
}
@CrossOriginを付けるだけ
72. #jjug_ccc #ccc_e5
サーバホスト名解決
(C) CASAREAL, Inc. All rights reserved. 72
Angular Spring Boot
http://localhost:4200 http://localhost:8080
Angular Spring Boot
https://angular.com https://spring.com
開発環境
本番環境
開発環境の Angular アプリは http://localhost:8080
本番環境のAngular アプリは https://boot.example.com
に非同期通信を行うように切り替える
81. #jjug_ccc #ccc_e5
Spring Bootアプリの依存性に追加する
(C) CASAREAL, Inc. All rights reserved. 81
MavenのdependencyにAngularアプリを追加
jar の中にAngularアプリが組み込まれる
<dependency>
<groupId>com.example</groupId>
<artifactId>client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
82. #jjug_ccc #ccc_e5
親プロジェクトをパッケージング
(C) CASAREAL, Inc. All rights reserved. 82
Spring Boot アプリの jar :最終成果物
Angular アプリの jar
BOOT-
INF
lib
META-
INF
resource
s
Angular ⇒ Spring Boot
の順でパッケージング
され最終的にこの形に
なる
83. #jjug_ccc #ccc_e5
ソースコードを公開しています
(C) CASAREAL, Inc. All rights reserved. 83
https://github.com/Masato1986/angular-spring-parent
紹介しきれなかった細かな設定は参照願います。
環境構築手順も書いています。
84. #jjug_ccc #ccc_e5
まとめ
(C) CASAREAL, Inc. All rights reserved. 84
Angular と Spring Boot は相性抜群
サービスやDIなどの構成要素が似ている
CSRF対策もばっちり
SPA開発の最初の一歩として、
ぜひ弊社研修をご利用ください!