Java tutorial
/** * Atomsphere is an enterprise mobile application server. * Copyright (C) 2011 JatakaSource Ltd. * * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Atomsphere 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Atomsphere. If not, see <http://www.gnu.org/licenses/>. */ package org.atomsphere.management.authentication; import org.atomsphere.management.model.authentication.User; import org.atomsphere.management.svc.authentication.NoDBConnectionException; import org.atomsphere.management.svc.authentication.UserService; import org.jatakasource.common.authentication.AuthenticationMessages; import org.jatakasource.common.properties.MessageSourceUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.dao.DataAccessException; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.transaction.CannotCreateTransactionException; /** * Spring bean defined at common-svc-security-context.xml */ public class UserDetailsServiceImpl implements UserDetailsService { private static final Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class); @Autowired private UserService userService; @Autowired @Qualifier(value = "authenticationMessageSource") private ResourceBundleMessageSource authenticationMessageSource; @Override public UserDetails loadUserByUsername(String userName) throws AuthenticationException, DataAccessException { User user = null; try { // Return member from DB and populate roles. user = (User) userService.getUserByUserName(userName); if (user == null) { if (logger.isDebugEnabled()) { logger.debug("User name " + userName + " is missing in database !!!"); } throw new BadCredentialsException(MessageSourceUtils.getMessage(authenticationMessageSource, AuthenticationMessages.class, AuthenticationMessages.AUTHENTICATION_FAILED.name())); } user.setAuthorities(AuthenticationUtils.toGrantedAuthority((User) user)); logger.trace("User: " + user.getUsername() + " grantedAuthorities: " + user.getAuthorities()); } catch (CannotCreateTransactionException e) { logger.error("No connection to the database. Exception: " + e.getMessage()); if (logger.isDebugEnabled()) { logger.debug("No connection to the database. Exception: " + e.getMessage(), e); } throw new NoDBConnectionException("No connection to the database. Exception: ", e); } return user; } }