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

相關文章
相關標籤/搜索