问题描述
我们正在使用struts2 StrutsPrepareAndExecuteFilter
. web.xml中的配置为:
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>
我们需要在此公共拦截器内实现身份验证/权限.简单的方法是什么?我们是否可以创建扩展StrutsPrepareAndExecuteFilter
的自定义拦截器,并在其中进行实现?
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?
推荐答案
永远不要对org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
进行自定义.
You should never do a customization of org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
.
即使不是像其他框架中那样的final
和package protected
,也不需要扩展该过滤器.
Even if it's not final
and not package protected
like in other frameworks, extending that filter is not necessary.
您应该学习Struts2体系结构的基础.来自坚果.
You should learn basics of the Struts2 architecture. from the Nutshell.
您可以看到基本的Struts2流程. Struts2被实现为过滤器,用于处理所有请求,并通过一堆利用Struts2框架的大部分功能的拦截器将它们分配给操作.
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.
因此,如果要添加诸如身份验证之类的功能,则应首先考虑通过身份验证拦截器对其进行扩展.该拦截器应配置为在传入请求中的每个操作上被调用.在 struts2中为所有动作类在struts.xml
中添加拦截器,了解其工作方式.
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.
最近,您可以使用一个链接来编写用于身份验证的自定义拦截器.请参见是否可以在不使用struts.xml
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
这篇关于struts2 StrutsPrepareAndExecuteFilter定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!