Results 1 to 7 of 7

Thread: How to get the error message to velocity.

  1. #1
    Join Date
    Sep 2004
    Location
    Sydney
    Posts
    27

    Default How to get the error message to velocity.

    OK, I'm quite new to Spring and Acegi especially. I'm using velocity for the view.
    So, I have acegi returning me to the correct page when a login fails. The example jsp has this code:
    Code:
        <c&#58;if test="$&#123;not empty param.login_error&#125;">
          <font color="red">
            Your login attempt was not successful, try again.<BR><BR>
            Reason&#58; <%= &#40;&#40;AuthenticationException&#41; session.getAttribute&#40;AbstractProcessingFilter.ACEGI_SECURITY_LAST_EXCEPTION_KEY&#41;&#41;.getMessage&#40;&#41; %>
          </font>
        </c&#58;if>
    Obviously velocity doesn't have access to the session, so I can't get the message out. Is there a way I can get Acegi to populate a model so I can get the message (as well as determine if there was one - param.login_error)?

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    You'll need to write a tool, much like the authz tag in Acegi Security, and expose it via a VelocityView subclass that overrides exposeHelpers(Map, HttpServletRequest).

  3. #3

    Default

    Alternatively, you can create a controller that puts the exception on the model and return the VM you want to show (you can use an AbstractController as it doesn't have to do much). In the vm you can check if the exception is empty and if not, print out the message of the exception. You need to specify that controller instead of the VM in the failureURL of the authenticationProcessingFilter and loginFormUrl of the authenticationEntryPoint. Finally, you need to make sure the controller is not secured by the filterInvocationInterceptor.

    This is how I got around this problem (and be able to put other things in the model in order to show that in the vm).

    Code:
    public class LogonController extends AbstractController &#123;
    
    	/**
    	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal&#40;javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse&#41;
    	 */
    	protected ModelAndView handleRequestInternal&#40;HttpServletRequest request, HttpServletResponse response&#41; throws Exception &#123;
    		Map model = new HashMap&#40;&#41;;
    
    		// Put the authentication exception on the model.
    		AuthenticationException authenticationException = &#40;AuthenticationException&#41;WebUtils.getSessionAttribute&#40;request, AbstractProcessingFilter.ACEGI_SECURITY_LAST_EXCEPTION_KEY&#41;;
    		if &#40;authenticationException != null&#41; &#123;
    			model.put&#40;"acegiSecurityException", authenticationException&#41;;
    		&#125;
    		
    		// Do other stuff
    		
    		// Depending on the exception, return the normal logon page or
    		// the expired password page.
    		String view = "logon/logon";
    		if &#40;authenticationException != null && &#40;authenticationException instanceof PasswordExpiredException&#41;&#41; &#123;
    			view = "logon/expired";
    		&#125;
    		
    		return new ModelAndView&#40;view, model&#41;;
    	&#125;
    &#125;
    VM:
    Code:
        #if &#40;$acegiSecurityException&#41;
          <font color="red">
            Your login attempt was not successful, try again.<BR><BR>
    			$acegiSecurityException.message&#40;&#41;
          </font>
        #end
    Cheers, Stefaan.

  4. #4
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    A nice approach, Steffan.

  5. #5
    Join Date
    Dec 2004
    Posts
    9

    Default

    Hi;


    Could you tell me how to specify that controller instead of the VM? I mean, those properties require a String, not a bean. Do I have to modify something to make it work? Thanks.

  6. #6
    Join Date
    Dec 2004
    Posts
    9

    Default

    OK. I think I figured out how to do it. What I did (and seems it is working is as follows)

    Instead of replacing VM with controller (which will not work since the types mismatch), replacing vm page with URL handeld by the LogonController, which Steffan wrote. Then, it seems everything working fine.

    yufeng

  7. #7

    Default

    That's possibly, but then you have a strange URL mapping. Instead of doing so, you could change the authenticationFailureUrl of the authentication processing filter:

    Code:
    	<!-- Authentication through Acegi Security Framework --> 
    	<bean id="authenticationProcessingFilter" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
    		<property name="authenticationManager"><ref bean="authenticationManager"/></property>
    		<property name="authenticationFailureUrl"><value>/logon.lctrl</value></property>
    		<property name="defaultTargetUrl"><value>/</value></property>
    		<property name="filterProcessesUrl"><value>/logon/j_acegi_security_check</value></property>
    	</bean>
    with logon.lctrl being my login controller (I took the .lctrl extention so the security enforcer knows there is no authentication necessary to see the logon page).

Similar Threads

  1. Velocity and Classpath Mega-woes
    By jopaki in forum Web
    Replies: 2
    Last Post: May 9th, 2007, 03:21 AM
  2. How to configure Velocity tools?
    By alr in forum Web
    Replies: 7
    Last Post: Jan 31st, 2007, 09:38 AM
  3. FreeMarker vs Velocity
    By Martin Kersten in forum Architecture
    Replies: 8
    Last Post: May 30th, 2005, 09:21 AM
  4. Velocity OutOfMemoryError during UnitTests
    By camach in forum Container
    Replies: 2
    Last Post: May 26th, 2005, 05:28 PM
  5. Exposing velocity context
    By masum in forum Web
    Replies: 1
    Last Post: Oct 28th, 2004, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •