Thursday, December 28, 2006

Spring Security with Acegi Security

Acegi Security provides a comprehensive security solution for J2EE-based enterprise software applications, built using the Spring Framework. Acegi provides authentication service through the use of a web filter, an AuthenticationProvider and an AuthenticationEntryPoint. Authorization in Acegi can be done at different levels: authorizing web requests, authorizing which methods can be invoked, and authorizing access to individual domain object instances. In this example I will describe how to implement form-based authentication and URL level authorization using Acegi.
For this example, I use two secure JSPs, the secure/authenticatedusers.jsp file is available to all the authenticated users, and secure/admin/admin.jsp is only available to the users with ADMIN role. The login.jsp is used for login and denied.jsp is used to redirect unauthorized users. Follow these steps to run the example:
  1. Download Acegi and Spring.
  2. Start with a dynamic web project in eclipse.
  3. Copy the following Jar files into the WEB-INF/lib directory.
    acegi-security-1.0.3.jarMain classes of the Acegi security system.
    commons-codec-1.3.jarEncoders and decoders such as Base64, Hex, Phonetic and URLs.
    ehcache-1.2.3.jarUsed by the authentication provider.
    jstl.jar, standard.jarThe JSTL tag library
    spring.jarSpring framework
    commons-logging.jar, cglib-nodep-2.1_3.jarAvailable in the spring download (spring with dependencies)
  4. Create the Login page: The following is a listing for the login page.
    <jsp:root version="1.2" xmlns:jsp=""
    < contentType="text/html; charset=UTF-8" />
    import="org.acegisecurity.ui.AbstractProcessingFilter, org.acegisecurity.ui.webapp.AuthenticationProcessingFilter, org.acegisecurity.AuthenticationException" />
    <form action="j_acegi_security_check" method="POST">
    </td><input type='text' name='j_username' />
    <td><input type='password' name='j_password' /></td>
    <td><input type="checkbox" name="_acegi_security_remember_me" /></td>
    <td>Remember me (14 days)</td>
    <tr><td colspan='2'><input name="submit" type="submit" /></td></tr>
    <tr><td colspan='2'><input name="reset" type="reset" /></td></tr>

    • The login form uses j_acegi_security_check instead of j_security_check normally used for form-based authentication. Similarly for logout, we use j_acegi_logout action.
    • In order to remember a user for a period of time, use the _acegi_security_remember_me attribute. This is configured in the applicatonContext.xml file(see below).
  5. Create the other JSP files: The listings for the JSP pages used for this example are shown below, along with their respective directories.
    <p><a href="secure/authenticatedusers.jsp">Authenticated users only</a>
    <p><a href="secure/admin/admin.jsp">Admins only</a>
    Access Denied.
    <%@ page import="org.acegisecurity.context.SecurityContextHolder" %>
    <h1>Welcome: <%= SecurityContextHolder.getContext().getAuthentication().getName() %></h1>
    <p><a href="../">Home</a>
    <p><a href="../j_acegi_logout">Logout</a>
    <%@ page import="org.acegisecurity.context.SecurityContextHolder" %>
    <h1>Welcome: <%= SecurityContextHolder.getContext().getAuthentication().getName() %> is an Admin</h1>
    <p><a href="../../">Home</a>
    <p><a href="../../j_acegi_logout">Logout</a>
  6. The file: This example uses an in memory user registry and the WEB-INF/ file is used to store the usernames and passwords.
  7. Configure the Acegi filter in the Web deployment descriptor: The web.xml file is shown below
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4"

    <filter-name>Acegi Filter Chain Proxy</filter-name>

    <filter-name>Acegi Filter Chain Proxy</filter-name>


  8. Configure Acegi in applicationContext.xml file: The following is a listing of the WEB-INF/applicationContext.xml file, followed be explanations of the different beans defined in there.
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">


    <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
    <property name="filterInvocationDefinitionSource">

    <bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter"/>

    <bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter">
    <constructor-arg value="/index.jsp"/>
    <ref bean="rememberMeServices"/>
    <bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler"/>

    <bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="authenticationFailureUrl" value="/login.jsp?errorId=1"/>
    <property name="defaultTargetUrl" value="/"/>
    <property name="filterProcessesUrl" value="/j_acegi_security_check"/>
    <property name="rememberMeServices" ref="rememberMeServices"/>

    <bean id="securityContextHolderAwareRequestFilter" class="org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter"/>

    <bean id="rememberMeProcessingFilter" class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="rememberMeServices" ref="rememberMeServices"/>

    <bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
    <property name="key" value="changeThis"/>
    <property name="userAttribute" value="anonymousUser,ROLE_ANONYMOUS"/>

    <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
    <property name="authenticationEntryPoint">
    <bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
    <property name="loginFormUrl" value="/login.jsp"/>
    <property name="forceHttps" value="false"/>
    <property name="accessDeniedHandler">
    <bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
    <property name="errorPage" value="/denied.jsp"/>

    <bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="accessDecisionManager">
    <bean class="">
    <property name="allowIfAllAbstainDecisions" value="false"/>
    <property name="decisionVoters">
    <bean class=""/>
    <bean class=""/>
    <property name="objectDefinitionSource">

    <bean id="rememberMeServices" class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices">
    <property name="userDetailsService" ref="userDetailsService"/>
    <property name="tokenValiditySeconds" value="1800"></property>
    <property name="key" value="changeThis"/>

    <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
    <property name="providers">
    <ref local="daoAuthenticationProvider"/>
    <bean class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
    <property name="key" value="changeThis"/>
    <bean class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
    <property name="key" value="changeThis"/>

    <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
    <property name="userDetailsService" ref="userDetailsService"/>
    <property name="userCache">
    <bean class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
    <property name="cache">
    <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean">
    <property name="cacheManager">
    <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
    <property name="cacheName" value="userCache"/>

    <bean id="userDetailsService" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl">
    <property name="userProperties">
    <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="/WEB-INF/"/>

    <bean id="loggerListener" class="org.acegisecurity.event.authentication.LoggerListener"/>

    1. FilterChainProxy: Delegates Filter requests to a list of Spring-managed beans.
    2. HttpSessionContextIntegrationFilter: Populates the SecurityContextHolder with information obtained from the HttpSession.
    3. LogoutFilter: Logs a principal out.
    4. AuthenticationProcessingFilter: Processes an authentication form.
    5. SecurityContextHolderAwareRequestFilter: A Filter which populates the ServletRequest with a new request wrapper.
    6. RememberMeProcessingFilter: Detects if there is no Authentication object in the SecurityContext, and populates it with a remember-me authentication token if a RememberMeServices implementation so requests.
    7. AnonymousProcessingFilter: Detects if there is no Authentication object in the SecurityContextHolder, and populates it with one if needed.
    8. AnonymousProcessingFilter: Detects if there is no Authentication object in the SecurityContextHolder, and populates it with one if needed.
    9. ExceptionTranslationFilter: Handles any AccessDeniedException and AuthenticationException thrown within the filter chain.
    10. AuthenticationProcessingFilterEntryPoint: Used by the SecurityEnforcementFilter to commence authentication via the AuthenticationProcessingFilter. This object holds the location of the login form, relative to the web app context path, and is used to commence a redirect to that form.
    11. AccessDeniedHandlerImpl: Used by ExceptionTranslationFilter to handle an AccessDeniedException.
    12. FilterSecurityInterceptor: Performs security handling of HTTP resources via a filter implementation.
    13. TokenBasedRememberMeServices: Identifies previously remembered users by a Base-64 encoded cookie.
    14. DaoAuthenticationProvider: An AuthenticationProvider implementation that retrieves user details from an UserDetailsService.
    15. InMemoryDaoImpl: Retrieves user details from an in-memory list created by the bean context.


  1. great article! was having a hard time getting a prototype to work, but with your instructions i was up and running in no time. thanks!

  2. Very informative and detailed article. Was very helpful, thanks for sharing...

  3. not a good article..number of errors..looks like acopy of article from Acegi itself...

    Some suggestion:-

    provide the war file

    Good Thing :-

    Only the steps thats it..

  4. Great and comprehensive introductory article! Provides a very efficient path to implementing basic Acegi security w/o getting too much into the details of the framework itself, although explaining the necessary dependencies between the major concepts. Again - good work!

  5. here i got an error when i tried to log in with scott whose pwd was supposed to be tiger. the error msg was "The requested resource (/AcgSpring/j_acegi_security_check) is not available."

    can't figure out what's happening there. do you have any idea?

  6. Thanks for the example!

    I noticed a small typo in your 'login.jsp' HTML. you have an extra slash in your open TD tag.

    "</td><input type='text' name='j_username' />"

    should be

    "<td><input type='text' name='j_username' />"

  7. I could not make it work , I get this Error:

    java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    maybe someone can help me...

    thanks and great article...

  8. tonelli,
    you will have to include commons-logging.jar file in your classpath, it is available in the spring-with-dependencies download.

  9. thanks, great article *thumbs up*

    the same would be nice for basic authentication with spring security...

  10. hey Abhi
    Great article man!

    But I am trying to use form-based authentication WITHOUT acegi security

    After user enters its log name and password I get error:
    The requested resource (/someapp/j_security_check) is not available.

    What do you think coud cause this error ?

    Thanks for your help

  11. great. explained the most in the least. but after doing everything right, i.e. all libraries, code etc. in jboss4.2.2, i get this error: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '' is defined
    it's bugging me. any help or is it just a matter of using tomcat?

  12. HI, I found the detailed configuration file and usage details.


    Arivuvel Ramu

  13. But still i am facing one more issues.

    First time i have logged in with "X" Name and visited "XYZ.jsp" page. then i logged out.

    Second time i have logged in with "Y" Name and visited "YXSD.jsp" page, then i try to call "XYZ.jsp" from the browser, still the "X" person session is not experied.

    My Question: if i press /j_acegi_logout and my session also needs to be invalidated.

    Could u plz help me?

  14. Can you check the web.xml to make sure the /j_acegi_logout is accessible and not mapped to other resource?

  15. hi,

    when i put this in my web.xml

    code from context-param -end contenxt-param
    and filter
    name mapping and listener class


    my application won't run in apache
    response is
    Application at context path /loyalty_final could not be started
    plz tell me how to solve this error

  16. vikas,

    I had this problem too the solution was to explode the spring security demo war and to add log4j to the classpath for tomcat in lib or common/lib for pre Tomcat 6.

    Paul Sundling

  17. Hi,

    Abhi can u suggest!!!

    This is regarding the logout through Acegi. This is my problem:

    1: I have an URL ( --> clicked this URL a dialogue box appears where user enters the userid and password for authentication and clicks OK and this leads to my main page.

    2: Now if I click Logout button I want that it should redirect to the URL ( mentioned in point 1 above) and the same dialogue box should appear for the user to re-enter the login credentials.
    But instead of that it redirects to my main page again.

  18. Hi Abhi ,
    thanks for the good reference

    can i have this sample to download..

    Thanks in advance

  19. Hi

    I am Visha....
    Thanks for yr really helped me to figure out my problem

  20. hello it's very good article to understand the framework Acegi with Spring and got almost the same problem as mentioned by others when I try to connect the first time to the page(1) seeking to pass through I authentication redirects to the login page which is well within the safe side so I did authenticate but when I disconnect I can connect to this page (1) without using authentication via the url or the button at the top of previous page
    Pending your answers and thank you

  21. Hi Abhi,

    Nice Article. I have a question please anyone comment on this...

    I have a URL query string http://localhost:8080/DemoApp/admin.htm?action=display

    In filterInvocationInterceptor bean, how can i add this "/admin.htm?action=display" under the PATTERN_TYPE_APACHE_ANT.


  22. Investing Secne is the right resource to help traders find the best brokers, strategies and investment news online. We offer content marketing services, reviews, trading charts, and industry news so you can find what you're looking for quickly. Investing Secne is a portal created for traders, to help them find the best brokers and strategies online on a single platform.


Popular Posts