网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > 开源技术 > Spring > Spring security 命名空间的使用  

Spring security 命名空间的使用

2008-06-17  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

    1. Introduction 在application context中,你的开始使用security 命名空间,必须在application context文件中添加schema声明。 Java代码
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:security="http://www.springframework.org/schema/security"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.2.xsd
                  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd">
        ...
    </beans>

网管论坛bbs_bitsCN_com

    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:security="http://www.springframework.org/schema/security"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.2.xsd
                  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd">
        ...
    </beans>如果把spring security配置文件单独配置在一个文件,可以把security命名空间设置为默认的,以简化配置。更改beans tag的 配饰,如下:
    引用
    <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd"> 网管bitscn_com
    2.1.1. Design of the Namespace
    Web/HTTP Security
    Business Object (Method) Security
    AuthenticationManager
    AccessDecisionManager
    AuthenticationProviders
    UserDetailsService
    2.2. Getting Started with Security Namespace Configuration 2.2.1. web.xml Configuration 首先在app的web.xml文件中添加下面的过滤器声明: 2.2.2. A Minimal <http> Configuration 启用web security从一下配置开始: Java代码
    <http auto-config='true'>
      <intercept-url pattern="/**" access="ROLE_USER" />
    </http> 中国网管论坛bbs.bitsCN.com

      <http auto-config='true'>
        <intercept-url pattern="/**" access="ROLE_USER" />
      </http>上面的配置说明,所有的URLs都被保护了,只有ROLE_USER角色的用户才可以访问 可以配置多个 <intercept-url>来适应多种不同的访问控制,但这些 <intercept-url>是有作用顺序的, 排在前面的是优先使用的。 添加一些用户,你能定义一些测试数据在namespace中 Java代码
    <authentication-provider>
      <user-service>
        <user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
        <user name="bob" password="bobspassword" authorities="ROLE_USER" />
      </user-service>
    </authentication-provider>

网管网www.bitscn.com

      <authentication-provider>
        <user-service>
          <user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
          <user name="bob" password="bobspassword" authorities="ROLE_USER" />
        </user-service>
      </authentication-provider>在上面auto-config设置为true的配置相当于以下的配置: Java代码
    <http>
      <intercept-url pattern="/**" access="ROLE_USER" />
      <form-login />
      <anonymous />
      <http-basic />
      <logout />
      <remember-me />
    </http> 网管联盟bitsCN@com

      <http>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login />
        <anonymous />
        <http-basic />
        <logout />
        <remember-me />
      </http>同时注意:auto-config的设置要求有一个UserDetailsService 在表单认证中,默认会自动提供一个页面给你,你也可以定制自己的登录页面: Java代码
    <http auto-config='true'>
      <intercept-url pattern="/login.jsp*" filters="none"/>
      <intercept-url pattern="/**" access="ROLE_USER" />
      <form-login login-page='/login.jsp'/>
    </http> 网管论坛bbs_bitsCN_com

      <http auto-config='true'>
        <intercept-url pattern="/login.jsp*" filters="none"/>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page='/login.jsp'/>
      </http>filters="none"表示排除在拦截链之外。 启用Basic Auth的配置 Java代码
    <http auto-config="true">
        <intercept-url pattern="/**" access="ROLE_USER" />
        <http-basic />
    </http>

网管u家u.bitscn@com

      <http auto-config="true">
       <intercept-url pattern="/**" access="ROLE_USER" />
       <http-basic />
      </http>在上面的配置中Form login仍然是可以使用,值是Basic Auth是优先采用的 2.2.3. Using other Authentication Providers 灵活配置authentication-provider Java代码
    <authentication-provider user-service-ref='myUserDetailsService'/> 网管网www.bitscn.com

    <authentication-provider user-service-ref='myUserDetailsService'/>如果你使用数据库,还可以这样配置: Java代码
    <authentication-provider>
      <jdbc-user-service data-source-ref="securityDataSource"/>
    </authentication-provider>

网管联盟bitsCN@com

      <authentication-provider>
        <jdbc-user-service data-source-ref="securityDataSource"/>
      </authentication-provider>这样要求你的数据库表要符合一定的要求,你也可以这配置Spring Security JdbcDaoImpl。 指向user-service-ref。 添加加密配置: Java代码
    <authentication-provider>
      <password-encoder hash="sha"/>
      <user-service>
        <user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities="ROLE_USER, ROLE_ADMIN" />
        <user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities="ROLE_USER" />
      </user-service>
    </authentication-provider> 网管bitscn_com

     <authentication-provider>
       <password-encoder hash="sha"/>
       <user-service>
         <user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities="ROLE_USER, ROLE_ADMIN" />
         <user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities="ROLE_USER" />
       </user-service>
     </authentication-provider>你也能定制自己的加密算法,在文件中配置为一个bean。把bean的name指向password-encoder的ref属性, 不过定制的类要实现PasswordEncoder接口。 2.3. Advanced Web Features 如果你的应用支持HTTP 和 HTTPS,你能要求你特定的URLS仅能通过HTTPS访问, 这样的要求可以通过<intercept-url>的requires-channel属性进行设置得到支持,。 Java代码
    <http>
      <intercept-url pattern="/secure/**" access="ROLE_USER" requires-channel="https"/> 网管网www.bitscn.com
      <intercept-url pattern="/**" access="ROLE_USER" requires-channel="any"/>
      ...
    </http> 网管联盟bitsCN@com

      <http>
        <intercept-url pattern="/secure/**" access="ROLE_USER" requires-channel="https"/>
        <intercept-url pattern="/**" access="ROLE_USER" requires-channel="any"/>
        ...
  

网管网www_bitscn_com

网管网www_bitscn_com


TAGs   使用   空间   命名       <   />   一个   ROLE_USER   可以      
 上一篇:spring AOP面向切面编程   下一篇:Spring MVC与struts比较
Spring security 命名空间的使用 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: