Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
B2B SaaSでSpringSecurityによる Roleを用いたユーザー権限設定の 実装...
Search
Rakus_Dev
November 11, 2023
Programming
1
600
B2B SaaSでSpringSecurityによる Roleを用いたユーザー権限設定の 実装について
Rakus_Dev
November 11, 2023
Tweet
Share
More Decks by Rakus_Dev
See All by Rakus_Dev
AIは精算業務をどう変える? 自律型エージェントが実現する未来のワークフロー / RAKUS AI Meetup-1
rakus_dev
0
3
メールディーラーにおけるAI活用事例 ~クレーム検知機能リリースの舞台裏~ / RAKUS AI Meetup-2
rakus_dev
0
3
ラクス開発本部のAI駆動開発推進事例 / RAKUS AI Meetup-3
rakus_dev
0
5
読書シェア会 vol.5 / Yumemi.grow 20250526
rakus_dev
0
1.6k
PdM採用とAIの製品活用を同時に頑張ってみた話 / EM oasis 20250418
rakus_dev
0
170
多様なマネジメント経験から導き出した、事業成長を支えるEMの4つのコンピテンシー / 4 Key EM Competencies for Growth
rakus_dev
2
2.2k
圧倒的な『顧客志向』の文化の創り方 / Product Engineer Night 20250221
rakus_dev
0
250
読書シェア会 vol.2 / Yumemi.grow 20250225
rakus_dev
0
160
ラクスCTOが語る顧客視点を重視したプロダクト開発 / RAKUSTechCon2024_Kude
rakus_dev
0
3k
Other Decks in Programming
See All in Programming
Webの外へ飛び出せ NativePHPが切り拓くPHPの未来
takuyakatsusa
2
360
XP, Testing and ninja testing
m_seki
3
190
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
190
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
210
DroidKnights 2025 - 다양한 스크롤 뷰에서의 영상 재생
gaeun5744
3
320
Team operations that are not burdened by SRE
kazatohiei
1
210
都市をデータで見るってこういうこと PLATEAU属性情報入門
nokonoko1203
1
570
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
540
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
1k
A2A プロトコルを試してみる
azukiazusa1
2
1.1k
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
980
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
2
270
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
299
21k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
How STYLIGHT went responsive
nonsquared
100
5.6k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Done Done
chrislema
184
16k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
How to Ace a Technical Interview
jacobian
277
23k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
5
210
Transcript
© RAKUS Co., Ltd. B2B SaaSでSpringSecurityによる Roleを用いたユーザー権限設定の 実装について JJUG CCC
2023 Fall 株式会社ラクス 開発統括部 HUYNH PHUONG(フィン フォン) 2023/11/11 1
© RAKUS Co., Ltd. 2 自己紹介 HUYNH PHUONG(フィン フォン) ・株式会社ラクス
開発統括部 ・バックエンド エンジニア ・担当サービス:楽楽電子保存 自己紹介
© RAKUS Co., Ltd. 3 本日お話すること ・開発要件 ・実装方法選定 ・SpringSecurityの機能を使った事例 ・SpringSecurityでは実現できない部分
Spring AOP(AspectJ)で回避
© RAKUS Co., Ltd. 4 はじめに 本事例の対象サービス「楽楽電子保存」 オンライン上で請求書の受取・管理 はじめに
© RAKUS Co., Ltd. 5 開発要件
© RAKUS Co., Ltd. 6 こんな開発要件がありました。 無料版ユーザー 有料版ユーザー • 有料版/無料版で利用できる機能を制限する
開発要件 エンドポイント 制限
© RAKUS Co., Ltd. 7 • ユーザーの持っている権限に応じてアクセスできる データを制限する 開発要件 データ制限
© RAKUS Co., Ltd. 8 • アクセス違反の理由に応じて、エラーメッセージを 出し分ける 開発要件 オプションが有
効でない 権限がない 指定タグに閲覧 権限がない 指定帳票に閲覧 権限がない 無料版ユーザー 有料版ユーザー 有料版ユーザー 有料版ユーザー
© RAKUS Co., Ltd. 9 開発要件を実現するにあたり実装時 に遭遇したいくつかの課題を紹介さ せていただきます。
© RAKUS Co., Ltd. 10 実装方法選定
© RAKUS Co., Ltd. 11 実装方法の選択肢 Spring bootを利用しているのでこの前提で 以下のいずれかの方法で検討しました ・Spring
security ・Spring AOP(AspectJ) ・Filter 実装方法選定
© RAKUS Co., Ltd. 12 実装方法選定 Spring security 使う強み ・セキュリティ機能は最低限の設定で実装できること
・一般的な脆弱性の対策を広くカバーしている ・単体テストもサポートされている
© RAKUS Co., Ltd. 13 実装方法選定 Spring AOP(AspectJ) 使う強み ・クリーナーコード
メインロジックから横断的関心(Crosscutting Concern) (ロギング、セキュリティなど)を取り除く ・メンテナンスが楽になる AOPで注入する処理は共通処理として一元管理される
© RAKUS Co., Ltd. 14 実装方法選定 Filter 使う強み ・Springは、Filterを作成するためのサポートクラスも 提供されている
・各コントローラーの前後に共通処理を実行することが 担保できる
© RAKUS Co., Ltd. 15 実装方法選定 Spring securityとAOPを併用しました 現場で認証処理はSpringSecurityで実装されていたので 認可処理でも簡単に実装できた
無料版ユーザー 有料版ユーザー
© RAKUS Co., Ltd. 16 実装方法選定 指定タグに閲覧 権限がない 指定帳票に閲覧 権限がない
有料版ユーザー 有料版ユーザー SpringSecurityで実装できない部分も出てしまったので Spring AOP(AspectJ)で回避できた (セキュリティーは横断的関心(Crosscutting Concern)の 中で一つなので似合っている)
© RAKUS Co., Ltd. 17 SpringSecurityの機能を使った事例
© RAKUS Co., Ltd. 18 SpringSecurityの機能を使う部分 SpringSecurityの認可機能なら、 アクセス権限を制御することは簡単に出来る 図:HttpServletRequest を認証する(Spring公開ドキュメント)
ここで触る
© RAKUS Co., Ltd. 19 SpringSecurityの機能を使う部分:実装事例 現場での実装事例 ログインの際にSecurityContextのAuthentication情報 にGrantedAuthority一覧を追加する List<GrantedAuthority>
authorities = List.of( new SimpleGrantedAuthority(“MANAGER”) ); SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken(principal, credentials, authorities));
© RAKUS Co., Ltd. 20 SpringSecurityの機能を使う部分:実装事例 http.authorizeHttpRequests(auth -> { authenticationProperties.getAuthorities().forEach(authority
-> auth.requestMatchers(authority.getHttpMethod(), authority.getEndPoint()) .hasAnyRole(authority.getRole())); }) HttpServletRequestsを利用し、認可定義 Httpメソッドによる マッチング RequestMatcherによる マッチング
© RAKUS Co., Ltd. 21 SpringSecurityの機能を使う部分:実装事例 AccessDeniedHandlerの処理関数を用意する public AccessDeniedHandler accessDeniedHandler()
{ return (request, response, accessDeniedException) -> { request.getRequestDispatcher(【該当パス】) .forward(request, response); }; } 無料版/有料版を判断し、 該当のエラーコントローラーへ移動
© RAKUS Co., Ltd. 22 SpringSecurityの機能を使う部分:実装事例 http.exceptionHandling(e -> e.accessDeniedHandler(accessDeniedHandler())); Spring
SecurityのAccessDeniedHandlerを定義する
© RAKUS Co., Ltd. 23 SpringSecurityでは実現できない部分 Spring AOP(AspectJ)で回避
© RAKUS Co., Ltd. 24 Spring AOP(AspectJ) Spring認可の課題 アクセス権限がない時にAccessDeniedExceptionのみを 返すため、エラーケースを区別するのは簡単にできない
P22~23で使用してい るが、無料版/有料版 の判断がすごく簡単 だから!
© RAKUS Co., Ltd. 25 指定タグに閲覧権限がない 指定帳票に閲覧権限がない 指定アップロードファイルに閲覧権限がない Spring AOP(AspectJ):Spring認可の課題
・データベースへ アクセスが必要 ・分岐ケースも 多い
© RAKUS Co., Ltd. 26 Spring AOP(AspectJ):Spring認可の課題 複雑で同様処理だが二重実行が発生 ・認可 ・エラー出し分け
AOP
© RAKUS Co., Ltd. 27 AOP概要 Spring AOP(AspectJ):AOP概要 クラスを横断した処理(例外処理やロギングなど)をビジ ネスロジックから分離するようにコードを記述する
Advice Join points Point cut Program Execution Advice:抽出した共通処理 Joinpoint:Adviceを入れる場所 (@Before、 @Afterなど) Pointcut:Adviceを適用する条件 例: @Before("@annotation(XXX)") https://spring.pleiades.io/spring- framework/reference/core/aop.html
© RAKUS Co., Ltd. 28 Spring AOP(AspectJ):実装事例 現場での実装事例 ・専用アノテーションを作成する ・AOPのAdviceを定義して権限チェック処理を実装する
・権限チェックのAOPをメソッドに注入する
© RAKUS Co., Ltd. 29 Spring AOP(AspectJ):実装事例 専用アノテーション @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME)
public @interface BrowsableChecker { } メソッドのみに使用できる
© RAKUS Co., Ltd. 30 Spring AOP(AspectJ):実装事例 Advice定義 AOPのAdviceを定義し、権限チェック処理を実装する @Aspect
@Component public class BrowsableCheckerAspect { @Before("@annotation(BrowsableChecker)") public void check(JoinPoint joinPoint) { // 権限判断処理を行う } } メソッドが実行される前に、 Advice実行 AOP宣言 指定したアノテーションが付いて いるメソッドのみがAOPの対象
© RAKUS Co., Ltd. Advice定義(権限判断処理) @Before("@annotation(BrowsableChecker)") public void check(JoinPoint joinPoint)
{ // パラメーターを取得する Object command = joinPoint.getArgs()[0]; // commandを該当な型に変換する if (target instanceof XXXCommand cmd) { // データベースからデータを取得して権限チェ ック // アクセス拒否の場合、例外をThrowする } } Joinpointから メソッドの パラメーターが取 得可能 Spring AOP(AspectJ):実装事例
© RAKUS Co., Ltd. Spring AOP(AspectJ):実装事例 AOP注入 データアクセス制御が必要なサービスのメソッドに 専用アノテーションを簡単に付ける @Service
public class XXXUpdateService { @BrowsableChecker public void execute(XXXCommand cmd) { // メイン処理を行う(略) } } excuteメソッドの処 理を実行する前に、 権限チェックのAOP を実行する
© RAKUS Co., Ltd. Spring AOP(AspectJ):実装事例 単体テスト 単体テストでもMockとして共通化に用意する public class
BrowsableCheckerMock { public void setup(XXXCommand command) { // 共通Mock処理を行う } } 権限チェックMockクラス
© RAKUS Co., Ltd. Spring AOP(AspectJ):実装事例 単体テスト @SpringBootTest @Import([BrowsableCheckerMock.class]) class
XXXUpdateServiceSpec extends Specification { def "アクセスする権限がありません"() { setup: browsableCheckerMock.setup(cmd) } } @Importを定義し、使用する
© RAKUS Co., Ltd. おわりに おわりに Spring securityの認可機能はエンドポイント単位も データ単位も制限可能 但し、SpringSecurityの認可で実装すれば、アクセス拒否の時に
AccessDeniedExceptionのみを返すため、エラーの理由を区別する ことができないので注意必要 セキュリティーとは横断的関心(Crosscutting Concern)の中で一つ なのでSpringSecurity認可の代わりに、AOPで回避可能
© RAKUS Co., Ltd. おわりに Spring Security in Action Laurentiu
Spilca 参照 Spring によるアスペクト指向プログラミング https://spring.pleiades.io/spring- framework/reference/core/aop.html Spring Security - リファレンス https://spring.pleiades.io/spring-security/reference/
© RAKUS Co., Ltd. ご清聴ありがとうございました