人在江湖

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

前言
开始花了两三天的时间学Spring Security,还是云山雾罩的,大受打击。于是重新总结一下,飞越迷雾,梳理思路,写这样一篇文字。网上有个雷锋写了Spring Security2 学习精讲:http://www.javaeye.com/topic/319965里面包含可以运行的代码,如果你对spring scurity感兴趣,可以快速浏览一下下面的笔记,然后debug code,然后再看看笔记。Spring Security的内容远比笔记复杂,我只是根据自己的理解挑重要的记录并整理一下。把sample code也当作笔记的一部分,那个code还是比较精简地,更重要的是实用。
官方提供的sample code包居然没有源代码,faint, google半天找到http://grepcode.com/snapshot/repo1.maven.org/maven2/org.springframework.security/spring-security-samples-contacts/2.0.0 当然,如果你会用git的话也可以自己check out code, 不过我没用过git这种高级货。

正文
跟权限有关的两个概念是 认证 和 授权, 先上个图:

image

Run-As Manager 和 After-Invocation Manager不重要

The  actual  implementation  of  a  security  interceptor  will  depend  on  what resource is being secured. If you’re securing a URL in a web application, the security  interceptor  will  be  implemented  as  a  servlet  filter.  But  if  you’re  securing  a method invocation, aspects will be used to enforce security.

这篇只说Authentication Manager:

认证是通过AuthenticationManager来管的,

public interface AuthenticationManager {
  public Authentication authenticate(Authentication authentication)
      throws AuthenticationException;

}

The  authenticate()  method  will  attempt  to  authenticate  the  user  using  the org.acegisecurity.Authentication object (which carries the principal and credentials). If successful, the authenticate() method returns a complete Authentication  object,  including  information  about  the  user’s  granted  authorities (which will be considered by the authorization manager).

具体的工作是交给各个 authentication provider来做的:

image

这里provider manager包含多个具体的providers:

<bean id="authenticationManager"
    class="org.acegisecurity.providers.ProviderManager">
  <property name="providers">
    <list>
      <ref bean="daoAuthenticationProvider"/>
      <ref bean="ldapAuthenticationProvider"/>
    </list>
  </property>
</bean>
ProviderManager is given its list of authentication providers through its providers property.

以DaoAuthenticationProvider举例:

<bean id="authenticationProvider"
    class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
  <property name="userDetailsService"
      ref="userDetailsService"/>
</bean>

它会要求一个UserDetailsService, 跟它相关的是UserDetails接口

UserDetailsService接口是个简单的接口

public interface UserDetailsService {
    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException;
}

 

UserDetails接口如下:

public interface UserDetails extends Serializable {
    GrantedAuthority[] getAuthorities();

    String getPassword();

    String getUsername();

    boolean isAccountNonExpired();

    boolean isAccountNonLocked();

    boolean isCredentialsNonExpired();

    boolean isEnabled();
}

解释一下getAuthorities:该方法返回一个GrantedAuthority[]数组对象,GrantedAuthority是用户权限信息对象,这个对象中定义了一个获取用户权限描述信息的getAuthority()方法。

需要注意Authentication对象才是Spring Security使用的进行安全访问控制用户信息安全对象。实际上,Authentication对象有未认证和已认证两种状态,在作为参数传入认证管理器(AuthenticationManager)的authenticate方法时,是一个未认证的对象,它从客户端获取用户的身份信息(如用户名,密码),可以是从一个登录页面,也可以从Cookie中获取,并由系统自动构造成一个Authentication对象。而这里提到的UserDetails代表一个用户安全信息的源(从数据库,LDAP服务器,CA中心返回),Spring Security要做的就是将这个未认证的Authentication对象和UserDetails进行匹配,成功后将UserDetails中的用户权限信息拷贝到Authentication中组成一个完整的Authentication对象,共其它组件共享。

 
下一篇: 总结Spring Security之 关于授权,保护web和保护方法 
 

参考:

Spring in Action

Spring Security学习总结: http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html

Spring Security2 学习精讲: http://www.javaeye.com/topic/319965

posted on 2011-03-08 11:02 人在江湖 阅读(18496) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航: