Java tutorial
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.Collection; import javax.persistence.NoResultException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import nc.noumea.mairie.appock.services.AppUserService; public class MockAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { /** Service utilisateur */ @Autowired private AppUserService appUserService; private AppockAuthoritiesPopulator appockAuthoritiesPopulator; private UserDetails user; public UserDetails getUser() { return user; } public MockAuthenticationProvider(AppockAuthoritiesPopulator authoritiesPopulator) { appockAuthoritiesPopulator = authoritiesPopulator; } @Override protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException { } @Override protected UserDetails retrieveUser(String s, final UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException { try { AppUser appUser = appUserService.findByLogin(usernamePasswordAuthenticationToken.getName()); if (appUser == null || !appUser.isActif()) { throw new BadCredentialsException("Connection l'application APPOCK impossible"); } } catch (NoResultException e) { throw new BadCredentialsException("Connection l'application APPOCK impossible"); } user = new UserDetails() { @Override public Collection<? extends GrantedAuthority> getAuthorities() { return appockAuthoritiesPopulator.getGrantedAuthorities(null, usernamePasswordAuthenticationToken.getName()); } @Override public String getPassword() { return null; } @Override public String getUsername() { return usernamePasswordAuthenticationToken.getName(); } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return true; } }; return user; } }