作者:一代天骄的小男人_580 | 来源:互联网 | 2023-01-27 19:52
看springsecurity有一段时间了,一直迷惑于springsecurity使用的不方便,今天看书的时候,终于提到了自定义的springsecurity,下边就是书中提出的神图(我是看了一
看spring security有一段时间了,一直迷惑于spring security使用的不方便,今天看书的时候,终于提到了自定义的spring security,下边就是书中提出的神图(我是看了一下,没怎么看懂,但是我觉得自己做完再回来看肯定就差不多了。)
![](https://img1.php1.cn/3cd4a/24c6f/339/318090c338942816.jpeg)
直接看配置文件:
filters="
securityContextPersistenceFilter,
usernamePasswordAuthenticationFilter,
anonymousAuthenticationFilter,
filterSecurityInterceptor" />
这样就可以获得一个自己的过滤器链顺序了。“如果将这与风格的配置进行对比的话,我们要注意以下的配置元素:
1. 默认过滤器链的建立是在处理元素的时候自动包含的并不需要直接配置。尽管使用security命名空间的重写或扩展标准过滤器链的时候,允许很大程度的灵活性,但它并不能够得到FilterChainProxy本身。
2. 基于URL模式修改过滤器链并不适用于风格的声明。如果应用的某些部分不需要特定的处理这将会有用处,并且能使得过滤器的调用尽可能得少。 需要意识到很重要的一点是,不同于Spring 的一些配置(比较明显的如,在web.xml中的contextConfigLocation),在过滤器的名字之间需要需要使用逗号分隔。”
接下来就应该是慢慢配置了: class="org.springframework.security.web.context.SecurityContextPersistenceFilter"/>
" SecurityContextPersistenceFilter用来建立SecurityContext,而它被用来贯穿整个request过程以跟踪请求者的认证信息。你可能记得我们在上一章的Spring MVC代码中,为了得到当前认证过的Principa时,访问过SecurityContext对象。 "
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
" 正如我们在第二章中详细介绍的那样,UsernamePasswordAuthenticationFilter用来处理form 提交并检查认证存储是否为合法凭证。明确配置这个过滤器,对比security命名空间的配置." class="org.springframework.security.web.authentication.AnonymousAuthenticationFilter">
"我们的站点允许匿名访问。尽管对于比较特殊的条件AnonymousAuthenticationFilter并不需要,但是通常情况下会使用它,因为只对请求添加了一点的预处理。你可能并不认识这个 过 滤 器 , 除 了 我 们 在 第 二 章 对 其 简 短 提 到 以 外 。 这 是 因 为 对 于AnonymousAuthenticationFilter的配置都掩盖在security命名空间之中。列出的这两个属性都是需要的。userAttribute 属性声明了为匿名用户提供的用户名和GrantedAuthority。用户名和GrantedAuthority可能在我们的应用中用来验证用户是不是匿名用户。Key 可 能 是 随 机 生 成 的 , 但 是 需 要 在 一 个 bean 中 使 用(o.s.s.authentication.AnonymousAuthenticationProvider),我们稍后将会进行配置。" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
“ 在我们基本处理过滤器链的最后一个是最终负责检查Authentication的,而这是前面已配置的安全过滤器的处理结果。正是这个过滤器确定一个特定的请求最终是被拒绝还是被接受。 ”
再建立一些bean去辅助完成上边的功能:
id="affirmativeBased">
id="roleVoter" />
id="authenticatedVoter" />
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
class="org.springframework.security.authentication.AnonymousAuthenticationProvider">
class="org.springframework.security.authentication.ProviderManager">
然后看到这里,我决定自己写一个连接mysql的spring security自定义例子。
这样就可以连接我们自己的mysql数据库了。
连接数据库以后,我继续研究这本书中写的知识点,发现突然又跳到了Session生命周期,估计后边肯定能用到,所以我就也跟着看了:“pring Security有很多地方影响用户的HttpSession的生命周期。有很多功能只有将相关类配置成Spring bean时才可用。以下的表格列出了能够影响session创建和销毁的bean属性:![](https://img1.php1.cn/3cd4a/24c6f/339/6b0902b64493d7c8.jpeg)
![](https://img1.php1.cn/3cd4a/24c6f/339/f583c5ff8dd9efb0.jpeg)
我们了解这些知识以后,重新进行过滤器链的配置:
filters="
securityContextPersistenceFilter,
logoutFilter,
usernamePasswordAuthenticationFilter,
rememberMeAuthenticationFilter,
anonymousAuthenticationFilter,
exceptionTranslationFilter,
filterSecurityInterceptor" />
配置一些这里我们还没有写的过滤器。
,接下来又配置了记住密码过滤器:
class="org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
class="org.springframework.security.authentication.RememberMeAuthenticationProvider">
因为rememberme是有一个认证提供者,所以我们需要把它加入认证管理器中:
class="org.springframework.security.authentication.ProviderManager">
那好,加入这个以后,我们知道,当用户登陆时也会判断是否已经记住密码,所以在验证时也会进行判断:
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
还有一步,就是当我们退出时,我们需要在退出中调用我们的记住密码服务,进而进行退出登陆:
class="org.springframework.security.web.authentication.logout.LogoutFilter">
还有一个过滤器要配置,就是我们的异常过滤器(ExceptionTranslationFilter):
class="org.springframework.security.web.access.ExceptionTranslationFilter">
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
class="org.springframework.security.web .access.AccessDeniedHandlerImpl">
终于配置完成了,咱们是可以把这个项目运行起来的,先不说这个,书中给了我们两个选择,一个是security命名空间,一个是明确的bean定义,那么如何选择呢:
![](https://img1.php1.cn/3cd4a/24c6f/339/75663fc868093da5.jpeg)
。。 我整理了一下这个项目,现在是可以跑起来的,而且使用mysql进行了集成。 不用那个hsql了,我觉得看起来可能会好一点。
下载地址:http://download.csdn.net/detail/dulei294948/6312707