Struts 2 how to display messages stored in Interceptor that will be redirected to another action?

in my interceptor, if the user does not have sufficient rights, there would be a warning:

    public String intercept(ActionInvocation invocation) throws Exception {

    ActionContext actionContext = invocation.getInvocationContext();
    Map<String, Object> sessionMap = actionContext.getSession();
    User loginUser = (User) sessionMap.get("user");

    Object action = invocation.getAction();

    if (loginUser != null && loginUser.getRole().getId() != Constant.AUTHORITY_ADMIN) {

        ((ValidationAware) action).addFieldError("user.authority",
                ((DefaultAction) action).getText("user.action.authority.not.enough"));

        return DefaultAction.HOME_PAGE;
    }

    return invocation.invoke();
}

then it will be redirected to the action "HOME_PAGE" if success is displayed in jsp. So how to display a warning?

I used two interceptors configured in strust.xml for the correct admin request:

            <interceptor-stack name="authorityStack">
            <interceptor-ref name="authority" />
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="store">
                <param name="operationMode">STORE</param>
            </interceptor-ref>
        </interceptor-stack>

default:

<interceptor-stack name="default">
            <interceptor-ref name="login" />
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="store">
                <param name="operationMode">AUTOMATIC</param>
            </interceptor-ref>
        </interceptor-stack>
+3
source share
2 answers

This is how I handle access control in Struts2. It is really easy and quite possible to use:

First create an interface called SecurityCheckAware.

public interface SecurityCheckAware {
    void checkRight();
}

Then create an interceptor called SecurityCheckInterceptor.

public class SecurityCheckInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(final ActionInvocation invocation) throws Exception {
        if (invocation.getAction() instanceof SecurityCheckAware) {
            SecurityCheckAware action = (SecurityCheckAware) invocation.getAction();
            action.checkRight();
        }

        return invocation.invoke();
    }
}

.

, , SecurityCheckAware. :

@Override
public void checkRight() {
    User loginUser = (User) session.get("user");
    if (loginUser != null && loginUser.getRole().getId() != Constant.AUTHORITY_ADMIN) {
        throw new AccessViolation("You do not have permission to access this page.");
    }
}

, RuntimeException ( ). AccessViolation.

, AccessViolation ​​ struts.xml, :

<global-results>
    <result name="accessDenied">/WEB-INF/jsp/accessDenied.jsp</result>
</global-results>

<global-exception-mappings>
    <exception-mapping exception="com.example.AccessViolation" result="accessDenied"/>
</global-exception-mappings>

. SecurityCheckAware SecurityCheckInterceptor Preparable PrepareInterceptor, .

/ ( ), , .

+3

MessageStoreInterceptor, .

MessageStoreInterceptor - http://struts.apache.org/release/2.3.x/docs/message-store-interceptor.html

, .

+1

Source: https://habr.com/ru/post/1784205/