Better utilize Spring Security — remove reinvented wheel patterns#41
Merged
dmccoystephenson merged 2 commits intomainfrom Mar 15, 2026
Merged
Conversation
…terns - WebSocketAuthInterceptor: remove SecurityContextHolder.setAuthentication() call; in WebSocket/STOMP context accessor.setUser() is the correct Spring Security mechanism — SecurityContextHolder is thread-local and the call did not carry over to the messaging thread pool - UserService: remove verifyPassword() wrapper around PasswordEncoder.matches(); Spring Security's DaoAuthenticationProvider (via authenticationManager .authenticate()) already handles password verification — no need to duplicate it - UserController.login(): use Authentication.getName() returned by authenticationManager.authenticate() instead of re-trusting the raw request string for the subsequent user lookup - UserControllerTest: update testLogin_Success mock to return a proper Authentication object instead of null - UserServiceTest: remove tests for the deleted verifyPassword() method Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Identify opportunities to better utilize Spring Security
Better utilize Spring Security — remove reinvented wheel patterns
Mar 14, 2026
There was a problem hiding this comment.
Pull request overview
This PR removes custom authentication “wrapper” logic and relies more directly on Spring Security’s intended mechanisms for HTTP login and STOMP/WebSocket authentication propagation.
Changes:
- Remove
UserService.verifyPassword()and its unit tests (it duplicatedPasswordEncoder.matches()and wasn’t part of the real auth flow). - Fix
/loginto use the verifiedAuthenticationresult (authentication.getName()) rather than the raw request username for user lookup. - In STOMP CONNECT handling, stop setting
SecurityContextHolderand rely onaccessor.setUser(authentication)for per-message principal propagation.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| backend/src/main/java/com/accordion/controller/UserController.java | Uses the authenticated principal name from Spring Security for user lookup during login. |
| backend/src/test/java/com/accordion/controller/UserControllerTest.java | Updates login test to mock and return an Authentication with a username. |
| backend/src/main/java/com/accordion/service/UserService.java | Removes the unused verifyPassword() wrapper. |
| backend/src/test/java/com/accordion/service/UserServiceTest.java | Removes tests that only exercised the deleted verifyPassword() wrapper. |
| backend/src/main/java/com/accordion/security/WebSocketAuthInterceptor.java | Avoids thread-local SecurityContextHolder usage; continues to set the STOMP Principal via message headers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three places in the codebase duplicated or misused functionality Spring Security already provides out of the box.
Changes
WebSocketAuthInterceptor: RemovedSecurityContextHolder.getContext().setAuthentication(authentication). In STOMP context, messages are processed on Spring's messaging thread pool;SecurityContextHolderis thread-local so this call never reached the message handler.accessor.setUser(authentication)— already present — is the correct mechanism.UserService.verifyPassword(): Removed. This was a one-liner wrapper aroundpasswordEncoder.matches(), whichDaoAuthenticationProvideralready calls internally viaauthenticationManager.authenticate(). The method was never invoked in the actual auth flow.UserController.login(): TheAuthenticationresult fromauthenticationManager.authenticate()was being discarded; the raw request string was then used to look up the user. Now usesauthentication.getName()— the verified principal — for the lookup.Original prompt
📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.