Spring security核心组件

AuthenticationManager 和 AuthenticationProvider

  • AuthenticationManager

Authentication authenticate(Authentication authentication) throws AuthenticationException;

  • AuthenticationProvider
Authentication authenticate(Authentication authentication) throws AuthenticationException;

boolean supports(Class<?> authentication);

ProviderManager 委托给已配置的 AuthenticationProvider列表

按所示顺序(使用List暗示)进行尝试,每个提供程序都可以尝试进行身份验证,或者通过简单地返回null来跳过身份验证。如果所有实现都返回null,则ProviderManager将抛出ProviderNotFoundException

<bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
    <constructor-arg>
	<list>
	    <ref local="daoAuthenticationProvider"/>
	    <ref local="anonymousAuthenticationProvider"/>
	    <ref local="ldapAuthenticationProvider"/>
	</list>
    </constructor-arg>
</bean>

UserDetailService

In-Memory

<user-service id="userDetailsService">
    <user name="jimi" password="{noop}jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
    <user name="bob" password="{noop}bobspassword" authorities="ROLE_USER" />
</user-service>

Password Encoding

DelegatingPasswordEncoder

PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

String idForEncode = "bcrypt";
Map encoders = new HashMap<>();
encoders.put(idForEncode, new BCryptPasswordEncoder());
encoders.put("noop", NoOpPasswordEncoder.getInstance());
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
encoders.put("scrypt", new SCryptPasswordEncoder());
encoders.put("sha256", new StandardPasswordEncoder());

PasswordEncoder passwordEncoder =
    new DelegatingPasswordEncoder(idForEncode, encoders);

BCryptPasswordEncoder

// Create an encoder with strength 16
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(16);
String result = encoder.encode("myPassword");
assertTrue(encoder.matches("myPassword", result));

Pbkdf2PasswordEncoder

// Create an encoder with all the defaults
Pbkdf2PasswordEncoder encoder = new Pbkdf2PasswordEncoder();
String result = encoder.encode("myPassword");
assertTrue(encoder.matches("myPassword", result));

SCryptPasswordEncoder

DelegatingFilterProxy 过滤链

  • ChannelProcessingFilter,因为它可能需要重定向到不同的协议
  • SecurityContextPersistenceFilter,因此可以在Web请求开始时在SecurityContextHolder中设置SecurityContext,并且当Web请求结束时(可以使用下一个Web请求准备好),可以将对SecurityContext的任何更改复制到HttpSession。
  • ConcurrentSessionFilter,因为它使用SecurityContextHolder功能并需要更新SessionRegistry以反映来自主体的持续请求
  • 身份验证处理机制 -UsernamePasswordAuthenticationFilter,CasAuthenticationFilter,BasicAuthenticationFilter等 - 以便可以修改SecurityContextHolder以包含有效的Authentication请求令牌
  • SecurityContextHolderAwareRequestFilter,如果您使用它将Spring安全感知HttpServletRequestWrapper安装到您的servlet容器中
  • JaasApiIntegrationFilter,如果JaasAuthenticationToken位于SecurityContextHolder中,则会将FilterChain作为JaasAuthenticationToken中的Subject进行处理
  • RememberMeAuthenticationFilter,这样如果没有更早的身份验证处理机制更新SecurityContextHolder,并且请求提供了一个启用记住我服务的cookie,那么一个合适的记忆Authentication对象将放在那里
  • AnonymousAuthenticationFilter,这样如果没有早期的身份验证处理机制更新SecurityContextHolder,那么匿名身份验证对象将被放在那里
  • ExceptionTranslationFilter,用于捕获任何Spring Security异常,以便可以返回HTTP错误响应或启动相应的AuthenticationEntryPoint
  • FilterSecurityInterceptor,用于保护Web URI并在访问被拒绝时引发异常

猜你喜欢

转载自my.oschina.net/u/3847565/blog/2963944