nc.noumea.mairie.appock.core.security.AppockAuthenticationProvider.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.core.security.AppockAuthenticationProvider.java

Source

package nc.noumea.mairie.appock.core.security;

/*-
 * #%L
 * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma
 * %%
 * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;

import nc.noumea.mairie.appock.services.AppUserService;

/**
 * Implmentation de AuthenticationProvider pour l'authentification  l'application
 * 
 */
public class AppockAuthenticationProvider implements AuthenticationProvider {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppockAuthenticationProvider.class);

    /** Message par dfaut */
    private static final String DEFAULT_MESSAGE = "Connexion  l'application APPOCK impossible";

    /** Authentication provider */
    private AuthenticationProvider provider;

    /** Message d'erreur si chec d'authentification par le provider */
    private String messageProvider = DEFAULT_MESSAGE;

    /** Message d'erreur si chec d'authentification  l'application */
    private String messageAppock = DEFAULT_MESSAGE;

    /** Service utilisateur */
    @Autowired
    private AppUserService appUserService;

    /**
     * Override la mthode authenticate
     * 
     * @param authentication Authentication
     * @throws AuthenticationException Exception d'authentification
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        Authentication authenticationResult = null;

        if (provider != null)
            try {
                authenticationResult = provider.authenticate(authentication);
            } catch (BadCredentialsException e) {
                LOGGER.error("Error lors de l'authentification", e);
                throw new BadCredentialsException(messageProvider);
            }

        String username = authentication.getName();
        String password = (String) authentication.getCredentials();
        List<GrantedAuthority> roles = new ArrayList<>();

        try {
            AppUser appUser = appUserService.findByLogin(username);
            if (appUser == null || !appUser.isActif()) {
                throw new BadCredentialsException(messageAppock);
            }
        } catch (NoResultException e) {
            throw new BadCredentialsException(messageAppock);
        }

        return (provider == null) ? new UsernamePasswordAuthenticationToken(username, password, roles)
                : authenticationResult;
    }

    /**
     * Override de la mthode supports
     * 
     * @param authentication Authentication
     */
    @Override
    public boolean supports(Class<?> authentication) {
        return (provider == null) ? true : provider.supports(authentication);
    }

    /**
     * get Provider
     * 
     * @return provider
     */
    public AuthenticationProvider getProvider() {
        return provider;
    }

    /**
     * set Provider
     * 
     * @param provider Provider to set
     */
    public void setProvider(AuthenticationProvider provider) {
        this.provider = provider;
    }

    /**
     * get MessageProvider
     * 
     * @return message
     */
    public String getMessageProvider() {
        return messageProvider;
    }

    /**
     * set MessageProvider
     * 
     * @param messageProvider message to set
     */
    public void setMessageProvider(String messageProvider) {
        this.messageProvider = messageProvider;
    }

    /**
     * get messageAppock
     * 
     * @return message
     */
    public String getMessageAppock() {
        return messageAppock;
    }

    /**
     * set messageAppock
     * 
     * @param messageAppock message to set
     */
    public void setMessageAppock(String messageAppock) {
        this.messageAppock = messageAppock;
    }

}