Spring Boot セキュリティレビュー
認証の追加、入力処理、エンドポイント作成、またはシークレット処理時に使用します。
認証
- ステートレスJWTまたは失効リスト付き不透明トークンを優先
- セッションには
httpOnly、Secure、SameSite=Strictクッキーを使用 OncePerRequestFilterまたはリソースサーバーでトークンを検証
@Component
public class JwtAuthFilter extends OncePerRequestFilter {
private final JwtService jwtService;
public JwtAuthFilter(JwtService jwtService) {
this.jwtService = jwtService;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain chain) throws ServletException, IOException {
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
if (header != null && header.startsWith("Bearer ")) {
String token = header.substring(7);
Authentication auth = jwtService.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(auth);
}
chain.doFilter(request, response);
}
}
認可
- メソッドセキュリティを有効化:
@EnableMethodSecurity @PreAuthorize("hasRole('ADMIN')")または@PreAuthorize("@authz.canEdit(#id)")を使用- デフォルトで拒否し、必要なスコープのみ公開
入力検証
@Validを使用してコントローラーでBean Validationを使用- DTOに制約を適用:
@NotBlank、@Email、@Size、カスタムバリデーター - レンダリン…