java过滤器优先级,Servlet中过滤器的安全性约束优先级

While studying about security-constraints and filters in servlets, I made the following declarations in the web.xml file, which didn't work as I expected:

BeerSelector

/SelectBeer.do

GET

POST

Admin

LoginFilter

model.MyFilter

LoginFilter

/SelectBeer.do

According to what I read: filters should be encountered before the request reaches a certain url, so, how come the security-constraint is invoked first ?

I know that it makes sense from a security wise (to reach the filter you mush be authenticated), but I'd like to know the sequence triggered by the request.

Does the container searches first for the secured resources thus it triggers the security-constraint?

But this will contradict with the following paragraph quoted from Head First Servlets and Jsp "

Remember that in the DD, the is about what

happens after the request. In other words, the client has already made

the request when the Container starts looking at the

elements to decide how to respond. The request

data has already been sent over the wire

or maybe the request just triggers both: filter and security-constraint, but the security-constraint is favored over the filter ?

解决方案

The container processes the security constraints first.

In a nutshell the Servlet container first examines the incoming URL and checks if it matched the so-called excluded or unchecked constraints. Excluded means the URL can not be accessed by anyone, while unchecked means the opposite and allows everyone to access the URL.

At this stage the container can call your own code if you installed a so-called JACC provider.

After this the container may try to authenticate the current user, where it can call your own code again. If you registered a SAM (ServerAuthModule) this will always be called at this point, if you didn't register a SAM or when you are working with a non-full Java EE implementation (e.g. a Java EE web profile server like TomEE or a bare Servlet container like Tomcat) it depends on the server if some kind of server specific login module is always called (rare) or only called when access is not granted to the unauthenticated user (typical).

The SAM is a filter-like thing as it can redirect, forward and wrap the request and response, but it's not an HTTP Servlet Filter.

After authentication succeeds your JACC Policy will be called again, or when you haven't installed one the container will use a proprietary mechanism to see if you now have access when authenticated.

If it's indeed determined that you have access, the so-called "resource" will be invoked, which means the container will call the first Filter in the filtering chain, which will eventually call through to the target Servlet to which the requested URL was mapped.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值