Spring Boot 安全審查
在新增認證、處理輸入、建立端點或處理機密時使用。
何時啟用
- 新增認證(JWT、OAuth2、Session 基礎)
- 實作授權(@PreAuthorize、角色基礎存取)
- 驗證使用者輸入(Bean Validation、自訂驗證器)
- 配置 CORS、CSRF 或安全標頭
- 管理機密(Vault、環境變數)
- 新增速率限制或暴力破解防護
- 掃描相依性中的 CVE
認證
- 偏好無狀態 JWT 或不透明令牌搭配撤銷清單
- 使用
httpOnly、Secure、SameSite=StrictCookie 管理 Session - 使用
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)") - 預設拒絕,僅公開必要的範圍
@RestController
@RequestMapping("/api/admin")
public class AdminController {
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/users")
public List<UserDto> listUsers() {
return userService.findAll();
}
@PreAuthorize("@authz.isOwner(#id, authentication)")
@DeleteMapping("/users/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
userService.delete(id);
return ResponseEntity.noContent().build();
}
}
輸入驗證
- 使用 Bean Validation 搭配控制器上的
@Valid - 對 DTO 套用限制:
@NotBlank、@Email、@Size、自訂驗證器 - 在渲染前使用允許清單清理任何 HTML
// 錯誤:無驗證
@PostMapping("/users")
public User createUser(@RequestBody UserDto dto) {
return userService.create(dto);
}
// 正確:已驗證的 DTO
public record CreateUserDto(
@NotBlank @Size(max = 100) String name,
@NotBlank @Email String email,
@NotNull @Min(0) @Max(150) Integer age
) {}
@PostMapping("/users")
public ResponseEntity<UserDto> createUser(@Valid @RequestBody CreateUserDto dto) {
return ResponseEntity.status(HttpStatus.CREATED)
.body(userService.create(dto));
}
SQL 注入防護
- 使用 Spring Data Repository 或參數化查詢
- 對於原生查詢,使用
:param綁定;絕不進行字串拼接
// 錯誤:原生查詢中的字串拼接
@Query(value = "SELECT * FROM users WHERE name = '" + name + "'", nativeQuery = true)
// 正確:參數化原生查詢
@Query(value = "SELECT * FROM users WHERE name = :name", nativeQuery = true)
List<User> findByName(@Param("name") String name);
// 正確:Spring Data 衍生查詢(自動參數化)
List<User> findByEmailAndActiveTrue(String email);
密碼編碼
- 永遠使用 BCrypt 或 Argon2 對密碼進行雜湊處理——絕不儲存明文
- 使用
PasswordEncoderBean,不要手動雜湊
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12); // 成本因子 12
}
// 在 Service 中
public User register(CreateUserDto dto) {
String hashedPassword = passwordEncoder.encode(dto.password());
return userRepository.save(new User(dto.email(), hashedPassword));
}
CSRF 保護
- 對於瀏覽器 Session 應用程式,保持 CSRF 啟用;在表單/標頭中包含令牌
- 對於純 Bearer 令牌的 API,停用 CSRF 並依賴無狀態認證
http
.csrf(csrf -> csrf.disable())
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
機密管理
- 原始碼中無機密;從環境變數或 Vault 載入
- 保持
application.yml沒有憑證;使用佔位符 - 定期輪換令牌和資料庫憑證
# 錯誤:硬編碼在 application.yml 中
spring:
datasource:
password: mySecretPassword123
# 正確:環境變數佔位符
spring:
datasource:
password: ${DB_PASSWORD}
# 正確:Spring Cloud Vault 整合
spring:
cloud:
vault:
uri: https://vault.example.com
token: ${VAULT_TOKEN}
安全標頭
http
.headers(headers -> headers
.contentSecurityPolicy(csp -> csp
.policyDirectives("default-src 'self'"))
.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin)
.xssProtection(Customizer.withDefaults())
.referrerPolicy(rp -> rp.policy(ReferrerPolicyHeaderWriter.ReferrerPolicy.NO_REFERRER)));
CORS 配置
- 在安全過濾器層級配置 CORS,而不是每個控制器
- 限制允許的來源——在生產環境中絕不使用
*
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("https://app.example.com"));
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
config.setAllowedHeaders(List.of("Authorization", "Content-Type"));
config.setAllowCredentials(true);
config.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/api/**", config);
return source;
}
// 在 SecurityFilterChain 中:
http.cors(cors -> cors.configurationSource(corsConfigurationSource()));
速率限制
- 對昂貴的端點套用 Bucket4j 或閘道層級限制
- 記錄和警示突發流量;返回 429 並附帶重試提示
// 使用 Bucket4j 進行每端點速率限制
@Component
public class RateLimitFilter extends OncePerRequestFilter {
private final Map<String, Bucket> buckets = new ConcurrentHashMap<>();
private Bucket createBucket() {
return Bucket.builder()
.addLimit(Bandwidth.classic(100, Refill.intervally(100, Duration.ofMinutes(1))))
.build();
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain chain) throws ServletException, IOException {
String clientIp = request.getRemoteAddr();
Bucket bucket = buckets.computeIfAbsent(clientIp, k -> createBucket());
if (bucket.tryConsume(1)) {
chain.doFilter(request, response);
} else {
response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
response.getWriter().write("{\"error\": \"Rate limit exceeded\"}");
}
}
}
相依性安全性
- 在 CI 中執行 OWASP Dependency Check / Snyk
- 保持 Spring Boot 和 Spring Security 在支援的版本上
- 針對已知 CVE 使建置失敗
日誌與個人識別資訊(PII)
- 絕不記錄機密、令牌、密碼或完整的 PAN 資料
- 對敏感欄位進行遮罩;使用結構化 JSON 日誌
檔案上傳
- 驗證大小、內容類型和副檔名
- 儲存在 Web 根目錄之外;必要時掃描
發布前檢查清單
- 認證令牌已正確驗證且未過期
- 每個敏感路徑都有授權守衛
- 所有輸入皆已驗證和清理
- 無字串拼接的 SQL
- CSRF 策略符合應用程式類型
- 機密已外部化;無提交的機密
- 安全標頭已配置
- API 上有速率限制
- 相依性已掃描且為最新版本
- 日誌中無敏感資料
切記:預設拒絕、驗證輸入、最小權限、安全優先配置。