spring security3.x学习(5)_如何拦截用户请求

本课程为转载学习

原文链接:http://blog.csdn.net/dsundsun/article/details/11797835

上次看到了关于认证管理的过程,其中还有很多迷糊的地方,比如说,请求是如何被拦截的。

我们看一下第一个运行的例子,让他跑起来:
我访问一下这个url:http://localhost:8080/JBCPPets/home.do

页面将会自动跳转到:http://localhost:8080/JBCPPets/spring_security_login

我感到很奇怪,哪里出现的spring_security_login呢?、

然后我去查看了 spring security的配置文件。

<http auto-config="true" >
        <intercept-url pattern= "/*" access="ROLE_USER" />
</http >

还记得auto-config吧,他自动配置了form-login标签。 那么form-login标签究竟是什么呢。 我们查一下spring security的帮助文档
<form-login>中默认的属性中login-page和login-processing-url.咱们先来看这两个属性

172410_KLzj_1417419.jpg

所以。我们看出来了。 如果配置了form-login标签的话,登陆方面至少涉及了两个拦截,一个是"/spring_security_login"(匹配映射登陆页面)"/j_spring_security_check"(映射匹配UsernamePasswordAuthenticationFilter的filterProcessesUrl这个属性)

当配置了auto-config="true"时,<form-login>标签将自动配置(这个好像已经说过了),然后通过拦截顺序进行依次的匹配拦截。

在这里我们只需要知道。当我有<form-login>这个标签的时候,会配置两个url拦截,如果没有登录的话,却要访问授权资源文件的话,会被拦截到登陆页面,这个登陆页面是系统自动进行匹配的

"/_spring_security_check"是拦截提交过来的数据用的。这样说可能会很枯燥, 还记得刚开始我们提到的表单么。我们来看一下他的源码就应该知道大概的流程了:

<pre name="code" class="html"><html>
    
    <head>
        <title>
            Login Page
        </title>
    </head>
    
    <body onload='document.f.j_username.focus();'>
        <h3>
            Login with Username and Password
        </h3>
        <form name='f' action='/JBCPPets/j_spring_security_check' method='POST'>
            <table>
                <tr>
                    <td>
                        User:
                    </td>
                    <td>
                        <input type='text' name='j_username' value=''>
                    </td>
                </tr>
                <tr>
                    <td>
                        Password:
                    </td>
                    <td>
                        <input type='password' name='j_password' />
                    </td>
                </tr>
                <tr>
                    <td colspan='2'>
                        <input name="submit" type="submit" value="Login" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html></pre><br><br>

也就是说当表单提交的时候,action指向了"/j_spring_security_check"。好的, 再想一下我们的spring security文档。 他说如果遇到这样的请求url会指向UserPasswordAuthenticationFilte。那我们去看一下他的源码。

173209_iCQ6_1417419.jpg

我们可以猜到了 j_username j_password 和j_spring_security_check都是一些特定的拦截。 j_username主要拦截用户请求的用户名,而j_password主要拦截用户请求的密码.

spring security中也提供了部分的过滤器请求拦截(很可惜,我去查找spring security官方文档时,发现文档中没有提供一系列的列表),如下:

173402_YjnN_1417419.jpg

我们现在已经了解到了spring security是如何拦截用户请求的了。 归根结底还是过滤器啊。


转载于:https://my.oschina.net/heroShane/blog/196997

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值