struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hernán <heam...@gmail.com>
Subject Re: How to pass the current logged in username to struts2 action from spring security 3
Date Fri, 01 Mar 2013 19:33:18 GMT
In order to have the user available in several action objects, you could
write an interceptor, this is the way struts2 handles the so called cross
cutting concerns (it would be Aspect Oriented Programming in Spring)

Here's the class:

package com.tipuana.csa.interceptor;

import java.lang.reflect.Method;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.tipuana.csa.model.User;

@SuppressWarnings("serial")
public class SpringSecurityInterceptor extends AbstractInterceptor {

    public String intercept(ActionInvocation actionInvocation)
            throws Exception {

        Object action = actionInvocation.getAction();

        Authentication currentUser =
SecurityContextHolder.getContext().getAuthentication();

        if (currentUser != null) {

            @SuppressWarnings("rawtypes")
            Class actionClass = action.getClass();
            boolean isDone = false;

            while (((actionClass = actionClass.getSuperclass()) != null) &&
(!isDone))
            {
                for (Method method : actionClass.getDeclaredMethods())
                {
                    if
((method.getAnnotation(SpringSecurityPrincipal.class) != null) &&
((currentUser.getPrincipal() instanceof User))) {
                        method.invoke(action, new Object[] {
currentUser.getPrincipal() });
                        isDone = true;

                        break;
                    }
                }

            }

        }

        return actionInvocation.invoke();
    }
}

package com.tipuana.csa.interceptor;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({java.lang.annotation.ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SpringSecurityPrincipal
{
}

Then in struts.xml something like this:


        <interceptors>
            <interceptor name="springSecurity"
class="com.tipuana.csa.interceptor.SpringSecurityInterceptor" />

            <interceptor-stack name="paramsPrepareParamsAuthenticatedStack">
                <interceptor-ref name="springSecurity" />
                <interceptor-ref name="paramsPrepareParamsStack" />
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref
name="paramsPrepareParamsAuthenticatedStack" />

On Fri, Mar 1, 2013 at 11:03 AM, Omar Ngarigari <ngarigarius@yahoo.com>wrote:

> Thanks Lukasz, I will give a try.
>
>
>
>
> ________________________________
>  From: Lukasz Lenart <lukaszlenart@apache.org>
> To: Struts Users Mailing List <user@struts.apache.org>
> Sent: Friday, March 1, 2013 7:01 AM
> Subject: Re: How to pass the current logged in username to struts2 action
> from spring security 3
>
> 2013/3/1 Omar Ngarigari <ngarigarius@yahoo.com>:
> > Thanks Luksz for replying, but what i want is to use the username in
> Struts 2 action class and display in struts2 jsp tags
>
> Use that code to read username in your action and then show it using S2
> tags
>
>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>



-- 
Hernán

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message