java Spring Security 总结一 6

来源:互联网 发布:英语造句软件 编辑:程序博客网 时间:2024/04/30 15:54

即使是系统的开发者或者说是最终用户,都不应该看到系统中有明文的密码。所以,Spring Security考虑的还是很周到的,为我们提供的密码加密的功能。正如你在Dao认证提供者(DaoAuthenticationProvider)中看到的,passwordEncoder属性配置的就是一个密码加密程序(密码编码器)。这里我们使用MD5加密,可以看配置文件中的scott用户,你还能看出他的密码是什么吗?当然这里只是演示功能,其它用户还是没有改变,你可以自己试试。系统为我们提供了一些常用的密码编码器(这些编码器都位于 org.springframework.secu rity.providers.encoding包下):

    PlaintextPasswordEncoder(默认)——不对密码进行编码,直接返回未经改变的密码;

    Md4PasswordEncoder ——对密码进行消息摘要(MD4)编码;

    Md5PasswordEncoder ——对密码进行消息摘要(MD5)编码;

    ShaPasswordEncoder ——对密码进行安全哈希算法(SHA)编码。

    你可以根据需要选择合适的密码编码器,你也可以设置编码器的种子源(salt source)。一个种子源为编码提供种子(salt),或者称编码的密钥,这里不再赘述。

    这里附加介绍了不少东西,希望你还没有忘记在AuthenticationManager(认证管理器)中还配置了一个名为 sessionController的Bean,这个Bean可以阻止用户在进行了一次成功登录以后在进行一次成功的登录。在 applicationContext-security.xml配置文件添加sessionController的配置:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->1 <bean id="concurrentSessionController"
2 
3 class="org.springframework.security.concurrent.ConcurrentSessionControllerImpl"
4     p:maximumSessions="1"
5     p:exceptionIfMaximumExceeded="true"
6     p:sessionRegistry-ref="sessionRegistry"/>
7 <bean id="sessionRegistry"
8 
9 class="org.springframework.security.concurrent.SessionRegistryImpl"/>

    maximumSessions属性配置了只允许同一个用户登录系统一次,exceptionIfMaximumExceeded属性配置了在进行第二次登录是是否让第一次登录失效。这里设置为true不允许第二次登录。要让此功能生效,我们还需要在web.xml文件中添加一个监听器,以让Spring Security能获取Session的生命周期事件,配置如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->1 <listener>
2      <listener-class>
3         org.springframework.security.ui.session.HttpSessionEventPublisher
4      </listener-class>
5 </listener>

    HttpSessionEventPublisher类实现javax.servlet.http.HttpSessionListener接口,在 Session被创建的时候通过调用ApplicationContext的publishEvent(ApplicationEvent event)发布HttpSessionCreatedEvent类型的事件,HttpSessionCreatedEvent类继承自 org.springframework.context.ApplicationEvent类的子类 HttpSessionApplicationEvent抽象类。

    concurrentSessionController使用sessionRegistry来完成对发布的Session的生命周期事件的处理,org.springframework.security.concurrent.SessionRegistryImpl(实现了 SessionRegistry接口), SessionRegistryImpl类还实现了Spring Framework 的事件监听org.springframework.context.Application Listener接口,并实现了该接口定义的onApplicationEvent(ApplicationEvent event)方法用于处理Applic ationEvent类型的事件,如果你了解Spring Framework的事件处理,那么这里你应该可以很好的理解。

    认证管理器到此介绍完毕了,认证过程过滤器也介绍完了,接下来我们继续介绍过滤器链的下一个过滤器securityContextHolderAwareRequestFilter:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->1 <bean id="securityContextHolderAwareRequestFilter"
2     class="org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter"/>

    这个过滤器使用装饰模式(Decorate Model),装饰的HttpServletRequest对象。其Wapper是ServletRequest包装类 HttpServletRequestWrapper的子类(如SavedRequestAwareWrapper或 SecurityContextHolderAwareRequestWrapper),附上获取用户权限信息,request参数,headers 和 cookies 的方法。

    rememberMeProcessingFilter过滤器配置:

    <bean id="rememberMeProcessingFilter"

    class="org.springframework.security.ui.rememberme.RememberMeProcessingFilter"

    p:authenticationManager-ref="authenticationManager"

    p:rememberMeServices-ref="rememberMeServices"/>

    当SecurityContextHolder中不存在Authentication用户授权信息时,rememberMeProcessingFilter就会调用rememberMeServices 的autoLogin()方法从cookie中获取用户信息自动登录。

    anonymousProcessingFilter过滤器配置:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->1 <bean id="anonymousProcessingFilter"
2     class="org.springframework.security.providers.anonymous.AnonymousProcessingFilter"
3     p:key="springsecurity"
4     p:userAttribute="anonymousUser,ROLE_ANONYMOUS"/>

    如果不存在任何授权信息时,自动添加匿名用户身份至SecurityContextHolder中,就是这里配置的userAttribute,系统为用户分配一个ROLE_ANONYMOUS权限。

    exceptionTranslationFilter(异常处理过滤器),该过滤器用来处理在系统认证授权过程中抛出的异常,主要是处理 AccessDeniedException和AuthenticationException两个异常并根据配置跳转到不同URL:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->1 <bean id="exceptionTranslationFilter"
2     class="org.springframework.security.ui.ExceptionTranslationFilter"
3     p:accessDeniedHandler-ref="accessDeniedHandler"
4      p:authenticationEntryPoint-ref
="authenticationEntryPoint"/>
5   <!-- 处理AccessDeniedException -->
6 <bean id="accessDeniedHandler"
7     class
="org.springframework.security.ui.AccessDeniedHandlerImpl"
8     p:errorPage="/accessDenied.jsp"/>
9 <bean id="authenticationEntryPoint"
10      class
="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint"
11     p:loginFormUrl="/login.jsp"
12     p:forceHttps="false"/>