[英]Should spring security filters call authentication providers directly?
似乎有两种不同的模式:
模式#1
GenericFilterBean
自己进行身份验证。 正如大多数开箱即用的过滤器所使用的: UsernamePasswordAuthenticationFilter
, DigestAuthenticationFilter
等。
authenticated=false
创建Authentication
authenticated=false
AuthenticationProvider
(有时通过AuthenticationManager
) Authentication
并传回过滤器 在这种模式中,原始Authentication
只不过是传递给AuthenticationProvider
的POJO - 它永远不会进入上下文。
此外,过滤器通常也直接引用特定的EntryPoint
- 它最后调用它。
(我认为这种模式适合预身份验证过滤器?但Spring代码中没有那种一致性)。
模式#2
单独注册的AuthenticationProviders
进行身份验证。 正如大多数在线示例所使用的那样,但在开箱即用的过滤器中很少见到。
authenticated=false
创建Authentication
authenticated=false
AuthenticationProviders
Authentication
并尝试验证它 Authentication
变为authenticated=true
在此模式中,筛选器不直接调用AuthenticationProvider
或EntryPoint
。 这些是在外部注册的,适用于所有过滤器。 模式#2配置的典型示例:
<sec:http use-expressions="true" entry-point-ref="myCustomEntryPoint" pattern="/**"> <sec:custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="myCustomFilter" /> ... </sec:http> <sec:authentication-manager> <sec:authentication-provider ref="myCustomAuthenticationProvider" /> </sec:authentication-manager>
问题: 何时使用一种方法或另一种方法是否有任何逻辑?
模式#2 感觉最好。 但我认为无论哪种方式都有效,并且我不确定哪种方式正确/最佳/最安全/最具前瞻性/最不可能与其他过滤器等冲突。
如果上下文很重要,那就是Spring Security 3.2.5,它将用于基于令牌的身份验证,我们在授予访问权限之前验证对远程服务的令牌详细信息(取自请求标头)。
已经3年了,所以我认为结论是没有正确或错误的方式!
Spring Security的内容并没有太大变化,因为它是Acegi,它似乎是不同方法的混合。
最后,我选择了模式#1。 我不喜欢这样的事实:Pattern#2使用了从authenticated = false到true的神奇变化的可变对象!
模式#1允许我使用两个不可变对象(一个始终验证为false,另一个始终验证为true - 但仅在成功时添加),这实际上感觉更安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.