相关文章推荐
淡定的玉米  ·  select case when if ...·  1 年前    · 
大鼻子的筷子  ·  Stimulsoft ...·  1 年前    · 
温柔的豆浆  ·  WPF:Graphics图画-Brushes ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a custom handler like this:

Public class DatabaseAuthenticationHandler extends AbstractJdbcUsernamePasswordAuthenticationHandler {
  @Override
  protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(
        UsernamePasswordCredential credential, String originalPassword) throws GeneralSecurityException,    PreventedException {
    final String username = credential.getUsername();
    logger.debug("***Username:"+username);
    logger.debug("***Password:"+credential.getPassword());
    return createHandlerResult(credential, new SimplePrincipal(), null);
  @Override
  public boolean supports(final Credential credential)  {
    return true;

To me, this should always log a user in no matter what. But I see in the logs this:

ERROR [org.apereo.cas.authentication.PolicyBasedAuthenticationManager] 
- <Authentication has failed. Credentials may be incorrect or CAS cannot find authentication handler that supports
[UsernamePasswordCredential(username=sadf, source=MyJDBCAuthenticationManager)] of type [UsernamePasswordCredential].
 Examine the configuration to ensure a method of authentication is defined and analyze CAS logs at DEBUG level to trace the authentication event.

which makes no sense to me as I can see in the logs that cas is calling the authenticatUsernamePasswordInternal method. Obviously this handler supports, well everything.

Why can't I log in?

I think you best use principalFactory.createPrincipal to create the principal rather than returning an new SimplePrincipal().

In your AuthenticationEventExecutionPlanConfigurer & DatabaseAuthenticationHandler, add the following:

AuthenticationEventExecutionPlanConfigurer.java

    @Autowired
    @Qualifier("principalFactory")
    private PrincipalFactory principalFactory;
    @Bean
    public DatabaseAuthenticationHandler databaseAuthenticationHandler() {
         return new DatabaseAuthenticationHandler(principalFactory);

DatabaseAuthenticationHandler

Public class DatabaseAuthenticationHandler extends AbstractJdbcUsernamePasswordAuthenticationHandler {
  private final PrincipalFactory principalFactory;
  public DatabaseAuthenticationHandler (PrincipalFactory principalFactory){
     this.principalFactory = principalFactory;
  @Override
  protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(
        UsernamePasswordCredential credential, String originalPassword) throws GeneralSecurityException,    PreventedException {
    final String username = credential.getUsername();
    logger.debug("***Username:"+username);
    logger.debug("***Password:"+credential.getPassword());
    /////// below here's the change /////////
    return createHandlerResult(credential, this.principalFactory.createPrincipal(username), null);
  @Override
  public boolean supports(final Credential credential)  {
    return true;

See if the above works, thanks.

The root cause of this problem is that you pass a null parameter to createHandlerResult method,you can change it to new ArrayList<>. I also encountered this problem(My CAS version is 5.3.9).And I also tried the solution gaving by Ng Sek Long,but it didn't work.Then I tried to solve it by myself. I searched for the error message in CAS code and found it in PolicyBasedAuthenticationManager class.

            try {
                  PrincipalResolver resolver = this.getPrincipalResolverLinkedToHandlerIfAny(handler, transaction);
                  LOGGER.debug("Attempting authentication of [{}] using [{}]", credential.getId(), handler.getName());
                  this.authenticateAndResolvePrincipal(builder, credential, resolver, handler);
                  AuthenticationCredentialsThreadLocalBinder.bindInProgress(builder.build());
                  Pair<Boolean, Set<Throwable>> failures = this.evaluateAuthenticationPolicies(builder.build(), transaction);
                  proceedWithNextHandler = !(Boolean)failures.getKey();
                } catch (Exception var15) {
                  LOGGER.error("Authentication has failed. Credentials may be incorrect or CAS cannot find authentication handler that supports [{}] of type [{}]. Examine the configuration to ensure a method of authentication is defined and analyze CAS logs at DEBUG level to trace the authentication event.", credential, credential.getClass().getSimpleName());
                  this.handleAuthenticationException(var15, handler.getName(), builder);
                  proceedWithNextHandler = true;

In the above code snippet, the authenticateAndResolvePrincipal method declaired two kinds of exception.Looked at this method, I found there is a line of code which may throws that two.

 AuthenticationHandlerExecutionResult result = handler.authenticate(credential);

The key code which lead to this problem is in DefaultAuthenticationHandlerExecutionResult class.

  public DefaultAuthenticationHandlerExecutionResult(final AuthenticationHandler source, final CredentialMetaData metaData, final Principal p, @NonNull final List<MessageDescriptor> warnings) {
    this(StringUtils.isBlank(source.getName()) ? source.getClass().getSimpleName() : source.getName(), metaData, p, warnings);
    if (warnings == null) {
      throw new NullPointerException("warnings is marked @NonNull but is null");

So, if you use createHandlerResult(credential, new SimplePrincipal(), null), NullPointerException will throw at runtime.It will be catched by catch (Exception var15) code bock and log the error message you see.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.