HttpAuthenticationMechanism

HttpAuthenticationMechanism allow customize your own HTTP authentication mechanism.

An examples for custom HttpAuthenticationMechanism.

@ApplicationScoped
public class TestAuthenticationMechanism implements HttpAuthenticationMechanism {

    @Inject
    private IdentityStoreHandler identityStoreHandler;

    @Override
    public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) throws AuthenticationException {
        final String name = request.getParameter("name");
        final String pwd = request.getParameter("password");

        if (name != null && pwd != null ) {

            // Get the (caller) name and password from the request
            // NOTE: This is for the smallest possible example only. In practice
            // putting the password in a request query parameter is highly
            // insecure

            Password password = new Password(pwd);

            // Delegate the {credentials in -> identity data out} function to
            // the Identity Store
            CredentialValidationResult result = identityStoreHandler.validate(
                    new UsernamePasswordCredential(name, password));

            if (result.getStatus() == VALID) {
                // Communicate the details of the authenticated user to the
                // container. In many cases the underlying handler will just store the details 
                // and the container will actually handle the login after we return from 
                // this method.
                return httpMessageContext.notifyContainerAboutLogin(
                        result.getCallerPrincipal(), result.getCallerGroups());
            }

            return httpMessageContext.responseUnauthorized();
        }

        return httpMessageContext.doNothing();
    }

}

validate of IdentityStoreHandler will transport the validation to an application scoped IdentityStore or container built-in approach to handle it.

Java EE Security API provides three built-in annotations('@BasicAuthenticationMechanismDefinition', 'FormAuthenticationMechanismDefinition', '@CustomFormAuthenticationMechanismDefinition') to handle HTTP Basic, Form, a custom form authentication.

An example of '@BasicAuthenticationMechanismDefinition'.

You can add @BasicAuthenticationMechanismDefinition on a Servlet class or a CDI ApplicationScoped bean.

Here is an example of @FormAuthenticationMechanismDefinition.

Declare a @FormAuthenticationMechanismDefinition, set LoginToContinue properties.

Create a servlet for login.

Note, for a @FormAuthenticationMechanismDefinition, in the login page, form submit action should j_security_check, username field name should be j_username, password field name should be j_password.

This seems a little unreasonable, to free you from these fixed settings, there is a @CustomFormAuthenticationMechanismDefinition, this annotation accept the same properties as @FormAuthenticationMechanismDefinition.

Create a login page.

And create a backing bean to handle the login manually.

In loginBean, use injected SecurityContext to handle authentication, it will delegate the process to the application server internally.

Grab the source codes from my GitHub account, and have a try.

Last updated

Was this helpful?