Skip to content

Add recipe to migrate Filter to OncePerRequestFilter#911

Closed
jkschneider wants to merge 1 commit intomainfrom
feat/filter-to-onceper-request-filter
Closed

Add recipe to migrate Filter to OncePerRequestFilter#911
jkschneider wants to merge 1 commit intomainfrom
feat/filter-to-onceper-request-filter

Conversation

@jkschneider
Copy link
Member

@jkschneider jkschneider commented Jan 25, 2026

What's Changed

Adds a new recipe MigrateFilterToOncePerRequestFilter that migrates classes implementing javax.servlet.Filter (or jakarta.servlet.Filter) to extend Spring's OncePerRequestFilter.

The recipe performs the following transformations:

  • Changes implements Filter to extends OncePerRequestFilter
  • Renames doFilter method to doFilterInternal
  • Changes visibility from public to protected
  • Changes parameter types from ServletRequest/ServletResponse to HttpServletRequest/HttpServletResponse
  • Adds @Override annotation to doFilterInternal
  • Removes empty init() and destroy() methods (preserves non-empty ones)
  • Handles classes implementing multiple interfaces (e.g., Filter, Serializable)

Context

  • Addresses moderneinc/customer-requests#1740

This recipe helps with migrating from raw servlet Filters (commonly used in Acegi 1) to Spring's OncePerRequestFilter which became the standard in Spring Security 3+.

Testing

Added comprehensive test cases covering:

  • Basic filter migration
  • Filter with empty init/destroy methods (removed)
  • Filter with non-empty init method (preserved)
  • No-op for classes already extending OncePerRequestFilter
  • No-op for classes not implementing Filter
  • Filter implementing multiple interfaces

Checklist

  • Tests pass locally
  • New tests added for the change
  • No breaking changes (or breaking changes documented)

Adds a new recipe that migrates classes implementing javax.servlet.Filter
(or jakarta.servlet.Filter) to extend Spring's OncePerRequestFilter.

The recipe performs the following transformations:
- Changes `implements Filter` to `extends OncePerRequestFilter`
- Renames `doFilter` method to `doFilterInternal`
- Changes visibility from public to protected
- Changes parameter types from ServletRequest/ServletResponse to
  HttpServletRequest/HttpServletResponse
- Adds @OverRide annotation to doFilterInternal
- Removes empty init() and destroy() methods

Fixes moderneinc/customer-requests#1740
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jan 25, 2026
@jkschneider jkschneider deleted the feat/filter-to-onceper-request-filter branch January 25, 2026 23:44
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant