Springsecurity之CasAuthenticationProvider

    注:版本是Springsecurity4.3.x.RELEASEjava

    ProviderManager中有以下List-1的属性,AuthenticationProvider就是被ProviderManager使用到的,以下List-2所示。git

    List-1github

private List<AuthenticationProvider> providers

    List-2spring

public Authentication authenticate(Authentication authentication)
		throws AuthenticationException {
	Class<? extends Authentication> toTest = authentication.getClass();
	AuthenticationException lastException = null;
	Authentication result = null;
	boolean debug = logger.isDebugEnabled();

	for (AuthenticationProvider provider : getProviders()) {
		if (!provider.supports(toTest)) {
			continue;
		}
		if (debug) {
			logger.debug("Authentication attempt using "
					+ provider.getClass().getName());
		}
		try {
			result = provider.authenticate(authentication);
			if (result != null) {
				copyDetails(authentication, result);
				break;
			}
		}
		catch (AccountStatusException e) {
			prepareException(e, authentication);
			// SEC-546: Avoid polling additional providers if auth failure is due to
			// invalid account status
			throw e;
		}
		catch (InternalAuthenticationServiceException e) {
			prepareException(e, authentication);
			throw e;
		}
		catch (AuthenticationException e) {
			lastException = e;
		}
	}

    如List-2所示,会遍历List-1中的AuthenticationProvider,逐个provider的authenticate方法。ide

   

                                   图1 CasAuthenticationProvider的authenticate方法时序图debug

  • 步骤2中会判断是否支持。
  • 步骤4中,经过Cas20ServiceTicketValidator,将获得的ticket发送到CAS server,进行认证,以后CAS server会返还一些信息。
  • 步骤6和步骤7,用步骤4获得的数据,去调用UserDetailsService的loadUserByUsername。
  • 步骤3的最后,会建立CasAuthenticationToken,并返回。

    来一张图,描述下CasAuthenticationFilter、ProviderManager等的调用关系吧,以下图1所示,原图见个人Github3d

                                                                   图1 code

相关文章
相关标签/搜索