struts2 StrutsPrepareAndExecuteFilter customization

怎甘沉沦 提交于 2019-12-08 08:20:57

问题


We are using struts2 StrutsPrepareAndExecuteFilter. The configuration in web.xml is:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
    <init-param>
      <param-name>struts.devMode</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

We need to implement authentication/permission inside this common interceptor. What will be the easy way for it? Can we create a customized interceptor which extends StrutsPrepareAndExecuteFilter and do our implementation inside that?


回答1:


You should never do a customization of org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.

Even if it's not final and not package protected like in other frameworks, extending that filter is not necessary.

You should learn basics of the Struts2 architecture. from the Nutshell.

you can see the basic Struts2 flow. Struts2 is implemented as a filter to handle all requests and dispatch them to actions through a stack of interceptors that leverage most of the functionality and features of Struts2 framework.

So if you want to add some feature like authentication, then you should first think about extending it via the authentication interceptor. This interceptor should be configured to be invoked on every action from the incoming requests. See how it's done in struts2 adding interceptors in struts.xml for all action class.

Latter there's a link that you could use to write a custom interceptor for authentication purposes. See Is there a way to redirect to another action class without using on struts.xml



来源:https://stackoverflow.com/questions/36857576/struts2-strutsprepareandexecutefilter-customization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!