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 7664287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:16:09+00:00 2026-05-31T14:16:09+00:00

I’m using Spring 3.1.0.RELEASE with Spring Security 3.1. I want to inject my Spring

  • 0

I’m using Spring 3.1.0.RELEASE with Spring Security 3.1. I want to inject my Spring user (i.e. the user who is currently logged in) into a controller. I want to do this as opposed to using

SecurityContextHolder.getContext().getAuthentication().getPrincipal();

because it allows me to test the controller more easily with JUnit. However, I’m having a problem with my current setup. My question is, what is the correct way to inject my user (per request) into my controller? In my application context file, I have …

<bean id="userDetails" class="com.myco.eventmaven.security.SecurityHolder" factory-method="getUserDetails" scope="request">
    <aop:scoped-proxy />
</bean>

where I define my factory class as …

public class SecurityHolder {

@Autowired
private static UserService userService;

public static MyUserDetails getUserDetails() {
    final Authentication a = SecurityContextHolder.getContext().getAuthentication();
    if (a == null) {
        return null;
    } else {
        final MyUserDetails reg = (MyUserDetails) a.getPrincipal();
        final int userId = reg.getId();
        final MyUserDetails foundUser = userService.findUserById(userId);
        return foundUser;
    } // if
}   // getUserDetails

}

but the factory class repeatedly dies because “userService” fails to get autowired (the value is always null). I’m looking for a better way to do all this that can easily also integrate into my JUnit test. Any ideas?

Edit: Here’s the JUnit test I’m looking to work with …

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "file:src/test/resources/testApplicationContext.xml" })
public class UserEventFeedsControllerTest extends AbstractTransactionalJUnit4SpringContextTests {

private MockHttpServletRequest request;
private MockHttpServletResponse response;
...
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;

@Autowired
private RequestMappingHandlerMapping handlerMapping;

@Before
public void setUp() {
    ...
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
}
...
@Test
public void testSubmitUserEventFeedsForm() throws Exception {
    request.setRequestURI("/eventfeeds.jsp");
    request.setMethod("POST");
    final List<EventFeed> allEventFeeds = getAllEventFeeds();
    request.setParameter("userEventFeeds", allEventFeeds.get(0).getId().toString());

    final Object handler = handlerMapping.getHandler(request).getHandler();
    final ModelAndView mav = handlerAdapter.handle(request, response, handler);

    assertViewName(mav, "user/eventfeeds");
}
  • 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. Editorial Team
    Editorial Team
    2026-05-31T14:16:11+00:00Added an answer on May 31, 2026 at 2:16 pm

    You cannot autowire static fields. There are some workarounds, but I don’t want to show them to you…

    There are plenty of ways to access current user in an easier and more elegant matter:

    • Inject Principal to your controller (see When using Spring Security, what is the proper way to obtain current username (i.e. SecurityContext) information in a bean?):

      public ModelAndView showResults(final HttpServletRequest request, Principal principal) {
        final String currentUser = principal.getName();
        UserDetails ud = ((Authentication)principal).getPrincipal()
      
    • Develop your custom facade over SecurityContext

    • Replace built-in contextHolderStrategy in SecurityContextHolder for the purpose of testing

    See also

    • How to get active user's UserDetails
    • Spring 3 MVC Controller integration test – inject Principal into method
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.