Spring Security(08)——intercept-url配置

intercept-url配置

目录

1.1     指定拦截的url

1.2     指定访问权限

1.3     指定访问协议

1.4     指定请求方法

 

1.1    指定拦截的url

       通过pattern指定当前intercept-url定义应当作用于哪些url。

<security:intercept-url pattern="/**" access="ROLE_USER"/>

 

1.2     指定访问权限

       可以通过access属性来指定intercept-url对应URL访问所应当具有的权限。access的值是一个字符串,其可以直接是一个权限的定义,也可以是一个表达式。常用的类型有简单的角色名称定义,多个名称之间用逗号分隔,如:

<security:intercept-url pattern="/secure/**" access="ROLE_USER,ROLE_ADMIN"/>

       在上述配置中就表示secure路径下的所有URL请求都应当具有ROLE_USER或ROLE_ADMIN权限。当access的值是以“ROLE_”开头的则将会交由RoleVoter进行处理。

 

       此外,其还可以是一个表达式,上述配置如果使用表达式来表示的话则应该是如下这个样子。

   <security:http use-expressions="true">

      <security:form-login />

      <security:logout />

      <security:intercept-url pattern="/secure/**"access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>

   </security:http>

       或者是使用hasRole()表达式,然后中间以or连接,如:

   <security:intercept-url pattern="/secure/**" access="hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')"/>

       需要注意的是使用表达式时需要指定http元素的use-expressions=”true”。更多关于使用表达式的内容将在后文介绍。当intercept-url的access属性使用表达式时默认将使用WebExpressionVoter进行处理。

       此外,还可以指定三个比较特殊的属性值,默认情况下将使用AuthenticatedVoter来处理它们。IS_AUTHENTICATED_ANONYMOUSLY表示用户不需要登录就可以访问;IS_AUTHENTICATED_REMEMBERED表示用户需要是通过Remember-Me功能进行自动登录的才能访问;IS_AUTHENTICATED_FULLY表示用户的认证类型应该是除前两者以外的,也就是用户需要是通过登录入口进行登录认证的才能访问。如我们通常会将登录地址设置为IS_AUTHENTICATED_ANONYMOUSLY。

   <security:http>

      <security:form-login login-page="/login.jsp"/>

      <!-- 登录页面可以匿名访问 -->

      <security:intercept-url pattern="/login.jsp*"access="IS_AUTHENTICATED_ANONYMOUSLY"/>

      <security:intercept-url pattern="/**" access="ROLE_USER"/>

   </security:http>

 

1.3     指定访问协议

       如果你的应用同时支持Http和Https访问,且要求某些URL只能通过Https访问,这个需求可以通过指定intercept-url的requires-channel属性来指定。requires-channel支持三个值:http、https和any。any表示http和https都可以访问。

   <security:http auto-config="true">

      <security:form-login/>

      <!-- 只能通过https访问 -->

      <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" requires-channel="https"/>

      <!-- 只能通过http访问 -->

      <security:intercept-url pattern="/**" access="ROLE_USER" requires-channel="http"/>

   </security:http>

 

       需要注意的是当试图使用http请求限制了只能通过https访问的资源时会自动跳转到对应的https通道重新请求。如果所使用的http或者https协议不是监听在标准的端口上(http默认是80,https默认是443),则需要我们通过port-mapping元素定义好它们的对应关系。

   <security:http auto-config="true">

      <security:form-login/>

      <!-- 只能通过https访问 -->

      <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" requires-channel="https"/>

      <!-- 只能通过http访问 -->

      <security:intercept-url pattern="/**" access="ROLE_USER" requires-channel="http"/>

      <security:port-mappings>

         <security:port-mapping http="8899" https="9988"/>

      </security:port-mappings>

   </security:http>

 

1.4     指定请求方法

       通常我们都会要求某些URL只能通过POST请求,某些URL只能通过GET请求。这些限制Spring Security也已经为我们实现了,通过指定intercept-url的method属性可以限制当前intercept-url适用的请求方式,默认为所有的方式都可以。

   <security:http auto-config="true">

      <security:form-login/>

      <!-- 只能通过POST访问 -->

      <security:intercept-url pattern="/post/**" method="POST"/>

      <!-- 只能通过GET访问 -->

      <security:intercept-url pattern="/**" access="ROLE_USER" method="GET"/>

   </security:http>

 

       method的可选值有GET、POST、DELETE、PUT、HEAD、OPTIONS和TRACE。

 

(注:本文是基于Spring Security3.1.6所写)

(注:原创文章,转载请注明出处。原文地址:http://haohaoxuexi.iteye.com/blog/2161056

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值