エンドポイントを保護するREST APIのAPIメソッドまたはコントローラーに適用できるSpring Bootの注釈を作成する方法を探しています。しかし、これを行う方法に関するガイドまたはドキュメントを見つけるのに問題があります。
例:
@RestController
@RequestMapping("auth")
public class AuthenticationController {
@RequestMapping(value = "login", method = RequestMethod.POST)
public TokenResponse login(@RequestBody LogInRequest request) throws InvalidLogInException {}
@Authorize
@RequestMapping(value = "me", method = RequestMethod.GET)
public UserResponse getMe() {}
}
または
@Authorize
@RestController
@RequestMapping("books")
public class AuthenticationController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public BooksResponse getMyBooks() {}
@RequestMapping(value = "/wish", method = RequestMethod.GET)
public BooksResponse getWishList() {}
}
私はSpring Securityのビルドを調べましたが、必要なものよりもはるかに詳細です。ヘッダーで提供されるトークンを検証するミドルウェアが必要なだけです。それが有効な場合は、ユーザーIDを要求コンテキストに追加して、要求を通過させます。そうでない場合は、401 Unauthorized Errorを返し、APIメソッドの実行を許可しません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加