spring-projects / spring-projects/spring-security

Support loading security context into reactive context for reactive-returning Servlet controllers

Open
#4,859 0 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Summary

Spring Servlet MVC supports returning Reactive types from annotated controllers. For webflux applications, the reactive filter ReactorContextWebFilter loads security contexts into the Reactive context using ReactiveSecurityContextHolder. For Servlet applications, there is no such support. It would be nice if the reactive code in the controller could access the security context in the same way for both web stacks without modification to the controller code.

A workaround that doesn't involve changing controller code is to override the ResponseBodyEmitterReturnValueHandler instance in the RequestMappingHandlerAdapter and simply chain the context onto the reactive return value. Unfortunately this gets a bit messy as the class hasn't been written in a way conducive to customization. And regardless it would be nice if this worked out-of-the-box.

Actual Behavior

The Security context is correctly set and retrievable using normal Servlet MVC methods, but the reactive context is empty.

Expected Behavior

Accessing the security context to work regardless of stack type. e.g.

@RequestMapping
public Mono< String > sayHello( )
{
    return ReactiveSecurityContextHolder.getContext( ).map( c -> "Hello " + c.getAuthentication( ).getPrincipal( ).toString( ) );
}

should work identically for both webflux and sevlet mvc.

Configuration

Any Spring Servlet webapp using security.

Version

5.0

Sample

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ResponseBodyEmitterReturnValueHandler and RequestMappingHandlerAdapter, then compare their Servlet MVC handling with ReactorContextWebFilter and ReactiveSecurityContextHolder in WebFlux. Done means the sample reactive controller can access the security context under Servlet MVC as it already does under WebFlux, without controller changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.