Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 87027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:20:18+00:00 2026-05-10T22:20:18+00:00

My company has been evaluating Spring MVC to determine if we should use it

  • 0

My company has been evaluating Spring MVC to determine if we should use it in one of our next projects. So far I love what I’ve seen, and right now I’m taking a look at the Spring Security module to determine if it’s something we can/should use.

Our security requirements are pretty basic; a user just needs to be able to provide a username and password to be able to access certain parts of the site (such as to get info about their account); and there are a handful of pages on the site (FAQs, Support, etc) where an anonymous user should be given access.

In the prototype I’ve been creating, I have been storing a ‘LoginCredentials’ object (which just contains username and password) in Session for an authenticated user; some of the controllers check to see if this object is in session to get a reference to the logged-in username, for example. I’m looking to replace this home-grown logic with Spring Security instead, which would have the nice benefit of removing any sort of ‘how do we track logged in users?’ and ‘how do we authenticate users?’ from my controller/business code.

It seems like Spring Security provides a (per-thread) ‘context’ object to be able to access the username/principal info from anywhere in your app…

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 

… which seems very un-Spring like as this object is a (global) singleton, in a way.

My question is this: if this is the standard way to access information about the authenticated user in Spring Security, what is the accepted way to inject an Authentication object into the SecurityContext so that it is available for my unit tests when the unit tests require an authenticated user?

Do I need to wire this up in the initialization method of each test case?

protected void setUp() throws Exception {     ...     SecurityContextHolder.getContext().setAuthentication(         new UsernamePasswordAuthenticationToken(testUser.getLogin(), testUser.getPassword()));     ... } 

This seems overly verbose. Is there an easier way?

The SecurityContextHolder object itself seems very un-Spring-like…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:20:19+00:00Added an answer on May 10, 2026 at 10:20 pm

    The problem is that Spring Security does not make the Authentication object available as a bean in the container, so there is no way to easily inject or autowire it out of the box.

    Before we started to use Spring Security, we would create a session-scoped bean in the container to store the Principal, inject this into an ‘AuthenticationService’ (singleton) and then inject this bean into other services that needed knowledge of the current Principal.

    If you are implementing your own authentication service, you could basically do the same thing: create a session-scoped bean with a ‘principal’ property, inject this into your authentication service, have the auth service set the property on successful auth, and then make the auth service available to other beans as you need it.

    I wouldn’t feel too bad about using SecurityContextHolder. though. I know that it’s a static / Singleton and that Spring discourages using such things but their implementation takes care to behave appropriately depending on the environment: session-scoped in a Servlet container, thread-scoped in a JUnit test, etc. The real limiting factor of a Singleton is when it provides an implementation that is inflexible to different environments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can only sort Array/Lists, not fields in an object.… May 11, 2026 at 10:05 am
  • added an answer I dont think this is possible. It is the website… May 11, 2026 at 10:05 am
  • added an answer It is possible, in fact the hard thing is that… May 11, 2026 at 10:05 am

Related Questions

My company has been evaluating Spring MVC to determine if we should use it
My company has been working on a Flex dashboard that displays realtime financial information.
My company has a web document management application and I have been assigned to
My company has developed a long standing product using MFC in Visual C++ as
My company has a common code library which consists of many class libary projects
My company has a number of relatively small Access databases (2-5MB) that control our
My company has login integration with GroupWise, and Exchange 5.5/2000+. The Exchange 5.5/GroupWise logic
My company has code that integrates with activedirectory/LDAP for centralized userid/password login. Currently, the
My company has recently became a Microsoft Certified Partner and from a business perspective
My company has an existing established WinForm application which in running on WinXP. The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.