Upgrade to Pro — share decks privately, control downloads, hide ads and more …

B2B SaaSでSpringSecurityによる Roleを用いたユーザー権限設定の 実装について

Rakus_Dev
November 11, 2023

B2B SaaSでSpringSecurityによる Roleを用いたユーザー権限設定の 実装について

Rakus_Dev

November 11, 2023
Tweet

More Decks by Rakus_Dev

Other Decks in Programming

Transcript

  1. © RAKUS Co., Ltd. B2B SaaSでSpringSecurityによる Roleを用いたユーザー権限設定の 実装について JJUG CCC

    2023 Fall 株式会社ラクス 開発統括部 HUYNH PHUONG(フィン フォン) 2023/11/11 1
  2. © RAKUS Co., Ltd. 2 自己紹介 HUYNH PHUONG(フィン フォン) ・株式会社ラクス

    開発統括部 ・バックエンド エンジニア ・担当サービス:楽楽電子保存 自己紹介
  3. © RAKUS Co., Ltd. 8 • アクセス違反の理由に応じて、エラーメッセージを 出し分ける 開発要件 オプションが有

    効でない 権限がない 指定タグに閲覧 権限がない 指定帳票に閲覧 権限がない 無料版ユーザー 有料版ユーザー 有料版ユーザー 有料版ユーザー
  4. © RAKUS Co., Ltd. 12 実装方法選定 Spring security 使う強み ・セキュリティ機能は最低限の設定で実装できること

    ・一般的な脆弱性の対策を広くカバーしている ・単体テストもサポートされている
  5. © RAKUS Co., Ltd. 13 実装方法選定 Spring AOP(AspectJ) 使う強み ・クリーナーコード

    メインロジックから横断的関心(Crosscutting Concern) (ロギング、セキュリティなど)を取り除く ・メンテナンスが楽になる AOPで注入する処理は共通処理として一元管理される
  6. © RAKUS Co., Ltd. 16 実装方法選定 指定タグに閲覧 権限がない 指定帳票に閲覧 権限がない

    有料版ユーザー 有料版ユーザー SpringSecurityで実装できない部分も出てしまったので Spring AOP(AspectJ)で回避できた (セキュリティーは横断的関心(Crosscutting Concern)の 中で一つなので似合っている)
  7. © RAKUS Co., Ltd. 19 SpringSecurityの機能を使う部分:実装事例 現場での実装事例 ログインの際にSecurityContextのAuthentication情報 にGrantedAuthority一覧を追加する List<GrantedAuthority>

    authorities = List.of( new SimpleGrantedAuthority(“MANAGER”) ); SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken(principal, credentials, authorities));
  8. © RAKUS Co., Ltd. 20 SpringSecurityの機能を使う部分:実装事例 http.authorizeHttpRequests(auth -> { authenticationProperties.getAuthorities().forEach(authority

    -> auth.requestMatchers(authority.getHttpMethod(), authority.getEndPoint()) .hasAnyRole(authority.getRole())); }) HttpServletRequestsを利用し、認可定義 Httpメソッドによる マッチング RequestMatcherによる マッチング
  9. © RAKUS Co., Ltd. 21 SpringSecurityの機能を使う部分:実装事例 AccessDeniedHandlerの処理関数を用意する public AccessDeniedHandler accessDeniedHandler()

    { return (request, response, accessDeniedException) -> { request.getRequestDispatcher(【該当パス】) .forward(request, response); }; } 無料版/有料版を判断し、 該当のエラーコントローラーへ移動
  10. © 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
  11. © 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の対象
  12. © 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):実装事例
  13. © RAKUS Co., Ltd. Spring AOP(AspectJ):実装事例 AOP注入 データアクセス制御が必要なサービスのメソッドに 専用アノテーションを簡単に付ける @Service

    public class XXXUpdateService { @BrowsableChecker public void execute(XXXCommand cmd) { // メイン処理を行う(略) } } excuteメソッドの処 理を実行する前に、 権限チェックのAOP を実行する
  14. © RAKUS Co., Ltd. Spring AOP(AspectJ):実装事例 単体テスト 単体テストでもMockとして共通化に用意する public class

    BrowsableCheckerMock { public void setup(XXXCommand command) { // 共通Mock処理を行う } } 権限チェックMockクラス
  15. © RAKUS Co., Ltd. Spring AOP(AspectJ):実装事例 単体テスト @SpringBootTest @Import([BrowsableCheckerMock.class]) class

    XXXUpdateServiceSpec extends Specification { def "アクセスする権限がありません"() { setup: browsableCheckerMock.setup(cmd) } } @Importを定義し、使用する
  16. © RAKUS Co., Ltd. おわりに おわりに Spring securityの認可機能はエンドポイント単位も データ単位も制限可能 但し、SpringSecurityの認可で実装すれば、アクセス拒否の時に

    AccessDeniedExceptionのみを返すため、エラーの理由を区別する ことができないので注意必要 セキュリティーとは横断的関心(Crosscutting Concern)の中で一つ なのでSpringSecurity認可の代わりに、AOPで回避可能
  17. © 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/