1

My webserver prevents access to the system if the user is not logged in, but some users may have more than one profile and the system only allows users to log in choosing one of them, i.e., a user may be a regular user with some permissions and this same user may logout and login again with admin permissions and have access to everything.

When this situation occurs I redirect the user to an action that forces the user to choose his/her profile, but the server no longer knows the action that the user tried to access. I'm able to retrieve this information through ActionInvocation like below:

public String initFeature() {
    final ActionInvocation invocation = ServletActionContext.getActionContext(BaseAction.getRequest())
            .getActionInvocation();
    final Object action = invocation.getAction();
    if (!this.isUserFullyLogged() && !(action instanceof ProfileSelectionAction)) {
        final Boolean isPendingProfileSelection = (Boolean) this
                .retrieveSessionAttribute(Constants.PENDING_PROFILE_SELECTION);
        if (isPendingProfileSelection != null && isPendingProfileSelection.booleanValue()) {
            this.saveSessionAttribute("action", action);
            this.saveSessionAttribute("namespace", invocation.getProxy().getNamespace());
            return Constants.PROFILE_SELECTION;
        }

        return Action.LOGIN;
    }
    return Action.SUCCESS;
}

When the system reaches to the profile selection action I have the action and namespace that the user have tried to access and was forbid but I don't know what I have to do to redirect the system to this namespace and action because I only have done it using action result of the struts.xml file so far.

Philippe Gioseffi
  • 1,488
  • 3
  • 24
  • 41
  • Maybe this will help you: http://struts.apache.org/development/2.x/docs/parameters-in-configuration-results.html. – Aleksandr M Mar 19 '14 at 17:00
  • `How to redirect to an action without using struts.xml?` -> by using Annotations of Convention plugin :) – Andrea Ligios Mar 19 '14 at 18:04
  • Keep the state of the interceptor in session when you want to return back as of you save only the action name with namespace you should build the url from action mapping. – Roman C Mar 19 '14 at 20:30
  • @AleksandrM That's exactly what I was looking for, despite being a little annoyed to be forced to create another property in my class. I'll study RomanC suggestion too, but if you post an answer I'll +1 at least. – Philippe Gioseffi Mar 20 '14 at 12:29
  • @AndreaLigios Would you provide a code snippet or a link for further studying? – Philippe Gioseffi Mar 20 '14 at 12:30
  • @RomanC The link provided by Andrea Ligios solved my problem, but as I said I'm a little annoyed by the fact that I had to create another property in my action just for that. Could you provide a code snippet about what you're talking about? – Philippe Gioseffi Mar 20 '14 at 12:32
  • @PhilippeGioseffi: BTW with annotations it would be the same as in xml just in annotations. ;) And with `#session` you can access variables from session so there is no need to create another properties. – Aleksandr M Mar 20 '14 at 12:55
  • I've never used annotations with Struts2, so I'm not to familiar with these approach. I know that is just like JPA annotations but I'll have to study Struts2 annotations. – Philippe Gioseffi Mar 20 '14 at 13:02
  • @PhilippeGioseffi How about [Struts2 redirect from login interceptor](http://stackoverflow.com/q/20993574/573032) and [struts 2 - Sending mail with embedded url](http://stackoverflow.com/a/17451525/573032). – Roman C Mar 20 '14 at 14:42
  • @RomanC I'm just checking the answer you wrote for the question [Is there a way to redirect to another action class without using on struts.xml](http://stackoverflow.com/questions/16254934/is-there-a-way-to-redirect-to-another-action-class-without-using-on-struts-xml) that uses the interceptor approach. That's why I asked about it in my last comment in Aleksandr M's answer. – Philippe Gioseffi Mar 20 '14 at 17:13
  • Until an user is authenticated in either approach the interceptor will not invoke any action. That is the link above described well. – Roman C Mar 20 '14 at 17:28
  • My problem occurs because the authentication can be a two-step process, otherwise Websphere handles it correctly. – Philippe Gioseffi Mar 20 '14 at 17:49

1 Answers1

1

You can use parameters in result configuration in struts.xml file. Create two properties inside your action class that will hold the name of the action and namespace and use them in struts.xml like that:

<action name="..." class="...">
  <result type="redirectAction">
    <param name="actionName">${action}</param>
    <param name="namespace">${namespace}</param>
  </result>
</action>

Additionally if don't want to create them and you already store your variables in session, you can retrieve them from session as well using #session expression.

<action name="..." class="...">
  <result type="redirectAction">
    <param name="actionName">${#session.action}</param>
    <param name="namespace">${#session.namespace}</param>
  </result>
</action>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • What's the difference between `` and ``that is suggested by your link in the question comments section? And shouldn't the result have a name? – Philippe Gioseffi Mar 20 '14 at 13:03
  • @PhilippeGioseffi: Basically they do the same, but the one in link puts some parameters (e.g. id). You need to use `actionName` and `namespace` parameters of `redirectAction` result to achieve what you want. And the default name for the result is `success`, so if you returning `success` from action you don't need to put it in result config. – Aleksandr M Mar 20 '14 at 13:09
  • In my case if I don't define a name in my `result`, *success* will redirect the user to the normal flow. If I want to define parameters such an id I can't use `redirectAction`? – Philippe Gioseffi Mar 20 '14 at 13:14
  • @PhilippeGioseffi: What do you mean by define parameters? `success` is just an intelligent default that S2 will use. Of course you can define name for result. – Aleksandr M Mar 20 '14 at 13:19
  • @PhilippeGioseffi: Take a look at examples of Redirect Action Result: http://struts.apache.org/development/2.x/docs/redirect-action-result.html. – Aleksandr M Mar 20 '14 at 13:23
  • Imagine for example that I want to redirect user to `namespace/action?param1=value1&param2=value2`, how would I define *param1*, *param2* and its values with `redirectAction`? – Philippe Gioseffi Mar 20 '14 at 13:24
  • @PhilippeGioseffi: With `param` tag: `value1`. – Aleksandr M Mar 20 '14 at 13:26
  • Just the way I imagined, Struts2 will handle some parameters such as *actionName* and *namespace* as reserved words and others as regulars parameters, right? – Philippe Gioseffi Mar 20 '14 at 13:29
  • An opinion: is it more elegant, flexible and/or robust to implement it with an interceptor? – Philippe Gioseffi Mar 20 '14 at 13:34
  • @PhilippeGioseffi: You mean the whole remembering last url/action feature? The better way is using some security framework (e.g. spring-security). If not then yep an interceptor could be the right place to do this. – Aleksandr M Mar 20 '14 at 13:37
  • I think the security is managed by WebSphere itself, but since we use Spring 3.0.7 here I'll study a solution. Do you have links about it? – Philippe Gioseffi Mar 20 '14 at 13:40