I tried your example below using interceptor, but I am having difficult 
displaying the username value in JSP. I used the User class as follow:

public class User
private String username;
private String password;

public String getUsername(){
return username;
}

public String getPassword(){
return password;
}
public void setUsername(String a){
a = username;
}

in struts.xml
<package name="default" extends="struts-default">
 <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" />
<action name="addAction" method="doAdd" class="AddAction">
 <interceptor-ref name="springSecurity" />
<result name="success">/add.jsp</result>
</action>

public class AddAction extends ActionSupport implements ModelDriven<Add>{
Add addCustomer = new Add();

public Object getModel(){
return addCustomer;
}
}

JSP
<s:textfield name="addCustomer.postUserId" 
value="%{springSecurity.currentUser}"/>

I am not sure this is correct way to display the current logged in username;

Thanks,




________________________________
 From: Hernán <heam...@gmail.com>
To: Struts Users Mailing List <user@struts.apache.org>; Omar Ngarigari 
<ngarigar...@yahoo.com> 
Sent: Friday, March 1, 2013 2:33 PM
Subject: Re: How to pass the current logged in username to struts2 action from 
spring security 3
 
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 <ngarigar...@yahoo.com>wrote:

> Thanks Lukasz, I will give a try.
>
>
>
>
> ________________________________
>  From: Lukasz Lenart <lukaszlen...@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 <ngarigar...@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-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>



-- 
Hernán

Reply via email to