Skip to content

Allow setting principal on connect [SPR-11228] #15853

@spring-projects-issues

Description

@spring-projects-issues

Jean-Baptiste Nizet opened SPR-11228 and commented

I have a Spring MVC restful app that doesn't use Spring security, but uses a HandlerInterceptor to extract a token from a security cookie and gets the current user information thanks to this token.

The current user is then stored inside a request-scoped Spring bean (currentUser) and injected in every controller or service which needs access to the current user.

This works beautifully, until we add websockets to the mix. Indeed, the request-scoped bean is not usable from inside a method annotated with @MessageMapping, and the only way (that I know of) to have the current user accessible from this method is, AFAIK, to use a filter instead of an interceptor and to wrap the HTTP request so that getUserPrincipal() returns the current user.

It would be really helpful to

  • be able to provide a callback method when the connect message is received by the server, and be able to associate a Principal (other than the one returned by ServletRequest.getUserPrincipal()) to the socket session from this callback method
  • be able, from this callback method, to use the login and passcode sent with the connect message, or a cookie, in case HTTP authentication is not used
  • [UPDATE: this bullet is now superceded by Provide a websocket scope [SPR-11305] #15929] provide a websocket scope, which would work as the HTTP request and session scopes, but would last only for the duration of the web socket session. The callback could then initialize some beans in this scope, and the various @MessageMapping method could read and modify beans from this scope, maintaining some sort of conversation state linked to the websocket session. An alternative would be to maintain the request scope alive until the websocket session is closed, to be able to use the same request-scoped currentUser bean both in @RequestMapping and @MessageMapping methods.

Sorry if I missed something from the documentation or even from the way this all works: I've only been playing with this for a few days.


Affects: 4.0 GA

Issue Links:

Referenced from: commits a5c3143

0 votes, 6 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions