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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:41:33+00:00 2026-05-26T08:41:33+00:00

I have an application that uses Spring Security to control access to pages, to

  • 0

I have an application that uses Spring Security to control access to pages, to manage user roles (GrantedAuthority) and for ACL. The application uses the standard UsernamePasswordAuthenticationFilter that intercepts requests to /j_spring_security_check (with j_username and j_password request parameters), and using a ProviderManager it authenticates the user and on success stores it in the SecurityContextHolder.

The above is configured in the security context, using a customized UserDetailsService:

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref='myUserDetailsService'/>
</authentication-manager>

The above approach in my case is not optimal, for the following reasons:

  • Adding a captcha requires extra filters
  • In order to customize the login logic, I need to replace the AuthenticationProvider as well
  • showing errors in the login form is complex, since I cannot use Spring MVC’s forms

My idea is to remove the interceptor based login and put all the logic inside a Spring 3 MVC controller. The pseudo-code is as following:

RequestMapping(value="/login/", method = RequestMethod.POST)
public String attemptLogin(HttpServletRequest request, HttpServletResponse response,
    @ModelAttribute("login") LoginCmd login, Model model) {

    // validate command (username, password, captcha)
    // ...

    // load user from DB
    User user = userService.loadUserByUsername(login.getUsername());

    // extra logic (check number of failed logins + other stuff)
    // ...

    // In case everything is fine, create a spring security User

    /* Instead of creating the user, read it from DB */
    org.springframework.security.core.userdetails.User authUser =
        new org.springframework.security.core.userdetails.User(
        login.getUsername() /*username*/,
        login.getPassword() /*password*/,
        true /*enabled*/,
        true /*accountNonExpired */,
        true /*credentialsNonExpired */,
        true /*accountNonLocked*/,
        new ArrayList<GrantedAuthority>() /*authorities*/
    );

    // build the AuthenticationToken
    UsernamePasswordAuthenticationToken authResult =
        new UsernamePasswordAuthenticationToken(authUser, login.getPassword(),
        authUser.getAuthorities());
    // use WebAuthenticationDetailsSource do build details
    authResult.setDetails(detailsSource.buildDetails(request));
    SecurityContextHolder.getContext().setAuthentication(authResult);

    return SUCCESS_VIEW;
}

Do you see any problem with the solution here above? Is setting the authentication inside the SecurityContextHolder enough? Am I missing something?

Comments and suggestions are welcome 😉
Thanks a lot to everyone
Andrea

  • 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-26T08:41:34+00:00Added an answer on May 26, 2026 at 8:41 am

    I went through the Spring Security code, and on successful authentication also the original code just stores the Authentication object in the SecurityContextHolder, nothing else is done.

    For example, in class AbstractAuthenticationProcessingFilter (which is used by the standard login intercepting requests to /j_spring_security_check) does that:

     protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
            Authentication authResult) throws IOException, ServletException {
    
        if (logger.isDebugEnabled()) {
            logger.debug("Authentication success. Updating SecurityContextHolder to contain: " + authResult);
        }
    
        SecurityContextHolder.getContext().setAuthentication(authResult);
    
        rememberMeServices.loginSuccess(request, response, authResult);
    
        // Fire event
        if (this.eventPublisher != null) {
            eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
        }
    
        successHandler.onAuthenticationSuccess(request, response, authResult);
    }
    

    I implemented this on my application and everything works fine.

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

Sidebar

Related Questions

I have an existing grails application that uses spring-security plugin for authentication. I would
I have a grails application that uses spring-security-core and spring-security-ldap, with authentication against Active
I have a JSP struts application that uses Spring to integrate with the services/dao/database.
I have a spring application that uses an embedded Jetty instance. Since I am
We have an application that uses a dual monitor setup - User A will
I have a spring application that uses JBoss Drools 5 . It works fine
We have a Spring application that uses Apache DBCP for connection pooling. Our app
I have a JPA/Spring application that uses Hibernate as the JPA provider. In one
I have a JSF2 application that uses Spring 3.0. I have a bean that
I currently have a Spring Webflow application that uses Webflow + Ajax. I have

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.