All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.formkiq.server.service.UserDetailsAuthenticationProviderImpl Maven / Gradle / Ivy

/*
 * Copyright (C) 2016 FormKiQ Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.formkiq.server.service;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.formkiq.server.config.DateService;

import javassist.bytecode.stackmap.TypeData.ClassName;

/**
 * Custom Authentication Provider.
 *
 */
@Component
public class UserDetailsAuthenticationProviderImpl
        extends AbstractUserDetailsAuthenticationProvider {

    /** Logger. */
    private static final Logger LOG = Logger.getLogger(ClassName.class
            .getName());

    /** UserService Instance. */
    @Autowired
    private UserService userservice;

    /** DateService. */
    @Autowired
    private DateService dateservice;

    @Override
    protected void additionalAuthenticationChecks(final UserDetails userDetails,
            final UsernamePasswordAuthenticationToken authentication)
            throws AuthenticationException {
        // no additional authentication checks
    }

    @Override
    @Transactional
    public Authentication authenticate(final Authentication authentication)
            throws AuthenticationException {
        return super.authenticate(authentication);
    }

    @Override
    protected UserDetails retrieveUser(final String username,
            final UsernamePasswordAuthenticationToken authentication)
            throws AuthenticationException {

        String password = authentication.getCredentials() != null
                ? authentication.getCredentials().toString() : "";

        try {

            UserDetails details = this.userservice.findActiveUser(username,
                    password);

            this.userservice.updateLastLogin(username, this.dateservice.now());

            return details;

        } catch (AuthenticationFailureException e) {

            LOG.log(Level.INFO, "Authentication failed. ", e);
            throw e;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy