spring security开发步骤

1.web.xml中加载spring ,spring security

2.spring security配置文件中配置好....

3.自己写一个myFilter代替原有的FilterSecurityInterceptor过滤器,并分别实现AccessDecisionManager、

InvocationSecurityMetadataSourceService和UserDetailsService.如下:

 <beans:bean id="myFilter" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">

        <beans:property name="authenticationManager"

            ref="authenticationManager" />

        <beans:property name="accessDecisionManager"

            ref="myAccessDecisionManager" />

        <beans:property name="securityMetadataSource"

            ref="securityMetadataSource" />

    </beans:bean>

4.配置上面的3个bean,具体如下: 

 4.1 认证管理器     即:是否可以登录(包括是否锁定了,是否过期了等....)

<authentication-manager alias="authenticationManager">

        <authentication-provider

            user-service-ref="myUserDetailService">

            <!--   如果用户的密码采用加密的话

                <password-encoder hash="md5" />

            -->

        </authentication-provider>

    </authentication-manager>

4.2决策管理器     即:是否有权限访问资源

<beans:bean id="MyAccessDecisionManager"
 class="org.springframework.security.access.vote.AffirmativeBased"
  <beans:property name="decisionVoters">
   <beans:list>
    <beans:bean class="org.springframework.security.access.vote.RoleVoter">
     <!-- 将授权名称的默认前缀由ROLE_改为空. -->
     <beans:property name="rolePrefix" value=""></beans:property>
    </beans:bean>
    <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"></beans:bean>
   </beans:list>
  </beans:property>
 </beans:bean>

4.3 授权处理器    即定义某一资源可以被哪些用户组访问

 <beans:bean id="securityMetadataSource"
        class="com.aostarit.spring.security.MyInvocationSecurityMetadataSource" />

注意,4.3表示直接利用默认的授权处理器,它会从配置文件中读取group-->resource的对应关系,而非从数据库读取! 所以还需给告诉他初始化的时候用如下两个参数:

<b:constructor-arg type="org.springframework.security.web.util.UrlMatcher"
   ref="myUrlPathMatcher" />
  <!-- url对应authority的map -->
  <b:constructor-arg type="java.util.LinkedHashMap"
   ref="requestMap" />

既然如此,我们干脆自己写一个类得了,不用框架提供的类了.implements FilterInvocationSecurityMetadataSource 即可. 具体见资料.
      4.1.1

  真正干事的其实是这个myUserDetailService,他要实现UserDetailsService接口,

  这个接口中的方法:public UserDetails loadUserByUsername(String username)真正干事,<!--再调用什么dao层我就不管了,呵呵-->

  注意咯:返回的 UserDetails,我们返回一个实现了 UserDetails接口的类A即可.

  注意咯:A还有两个特点,①.她里面的数据就是从数据库读出来的数据,比如username,password,她不管页面提交的password的. 究竟是怎么验证密码的,这个要找框架本身了.②.他的权限set的时候还有点小技巧,这个见我的项目的SecurityUser里的setAuthorities(List<Object[]> l)方法.

    <beans:bean id="myUserDetailService"  class="com.aostarit.spring.security.MyUserDetailService" >
     <beans:property name="securityDao" ref="securityDao"></beans:property>
    </beans:bean>

     ****securityDao,这个自己写咯......自己配咯.....

posted @ 2013-06-29 13:29  NewObject_1  阅读(2655)  评论(0编辑  收藏  举报