Java tutorial
/* * Copyright(C) 2014 * NEC Corporation All rights reserved. * * No permission to use, copy, modify and distribute this software * and its documentation for any purpose is granted. * This software is provided under applicable license agreement only. */ package com.nec.harvest.service.impl; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.lang.StringUtils; import org.hibernate.HibernateException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataAccessException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.nec.core.exception.ObjectNotFoundException; import com.nec.core.exception.TooManyObjectsException; import com.nec.harvest.exception.ConnectionException; import com.nec.harvest.exception.HarvestAuthenticationException; import com.nec.harvest.exception.OrganizationNotFoundException; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.service.UserService; /** * {@link UserDetailsService} * * @author hungpd * */ public class UserDetailServiceImpl implements UserDetailsService { private static final Logger logger = LoggerFactory.getLogger(UserDetailServiceImpl.class); private static final String ROLE_ORGANIZATION = "ROLE_ORGANIZATION"; private static final String ROLE_EXTERNAL = "ROLE_EXTERNAL"; private static final String ROLE_MANAGER = "ROLE_MANAGER"; private static final String ROLE_HEADQUARTER = "ROLE_HEADQUARTER"; private com.nec.harvest.model.User user; private UserService userService; public UserDetailServiceImpl(UserService userService) { this.userService = userService; } /** * Locates the user based on the username. In the actual implementation, the * search may possibly be case sensitive, or case insensitive depending on * how the implementation instance is configured. In this case, the * <code>UserDetails</code> object that comes back may have a username that * is of a different case than what was actually requested.. * * @param username * the username identifying the user whose data is required. * * @return a fully populated user record (never <code>null</code>) * * @throws UsernameNotFoundException * if the user could not be found or the user has no * GrantedAuthority */ @Override @Transactional(readOnly = true, rollbackFor = { HibernateException.class }, propagation = Propagation.REQUIRES_NEW) public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { if (logger.isDebugEnabled()) { logger.debug("Locates the user based on the username or usercode"); } // The username or usercode identifying the user whose data is required if (StringUtils.isEmpty(username)) { logger.warn("The username or usercode identifying the user whose data is required"); // ?????????? throw new HarvestAuthenticationException( "??????????"); } // Trying to authenticate by username or usercode {} and password [ protected ] logger.info("Trying to authenticate by username or usercode {} and password [ protected ]", username); try { user = userService.findByUsrCode(username); } catch (IllegalArgumentException ex) { // User's code must not be null or empty logger.warn(ex.getMessage()); // ?????????? throw new HarvestAuthenticationException( "??????????"); } catch (ObjectNotFoundException ex) { logger.warn(ex.getMessage()); // ?????????? throw new HarvestAuthenticationException( "??????????"); } catch (org.hibernate.ObjectNotFoundException ex) { logger.warn(ex.getMessage()); // ????????????? throw new OrganizationNotFoundException( "?????????????"); } catch (TooManyObjectsException ex) { logger.error(ex.getMessage(), ex); // ??????????? throw new TooManyObjectsException("???????????"); } catch (ConnectionException | ServiceException ex) { logger.error(ex.getMessage(), ex); // ??????????? throw new ServiceException("???????????"); } // Wait seconds... The system trying to check the granted authorities for authenticating user {usernameOrUsrCode} logger.info( "Please wait second(s)... The system trying to check the granted authorities for authenticating user " + username); // ? // 1?2?3??4 int kengenkodo = 0; String usrKbn = user.getUsrKbn(); if (StringUtils.isNotEmpty(usrKbn)) { kengenkodo = Integer.parseInt(usrKbn); } // The authorities that should be granted to the caller if they // presented the correct username and password and the user is enabled. Not null. Collection<GrantedAuthority> authorities = getRolesToBeGranted(kengenkodo); // Construct the User with the details required by DaoAuthenticationProvider return new org.springframework.security.core.userdetails.User(username, user.getPassword(), authorities); } /** * Get a collection of granted authority based on number of ROLE * of logged-in user * * @param role * @return A collection of granted authority */ protected Collection<GrantedAuthority> getRolesToBeGranted(int role) { Collection<GrantedAuthority> roles = new ArrayList<GrantedAuthority>(); if (role == 2) { roles.add(new SimpleGrantedAuthority(ROLE_EXTERNAL)); } else if (role == 3) { roles.add(new SimpleGrantedAuthority(ROLE_MANAGER)); } else if (role == 4) { roles.add(new SimpleGrantedAuthority(ROLE_HEADQUARTER)); } else { // Default of role is organization roles.add(new SimpleGrantedAuthority(ROLE_ORGANIZATION)); } return roles; } }