Java tutorial
/* * Copyright (c) 2009 - 2010. School of Information Technology and Electrical * Engineering, The University of Queensland. This software is being developed * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project. * PODD is a National e-Research Architecture Taskforce (NeAT) project * co-funded by ANDS and ARCS. * * PODD 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. * * PODD 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 PODD. If not, see <http://www.gnu.org/licenses/>. */ package podd.server.authn.impl; import org.springframework.dao.DataAccessException; import org.springframework.orm.ObjectRetrievalFailureException; import org.springframework.security.GrantedAuthority; import org.springframework.security.GrantedAuthorityImpl; import org.springframework.security.userdetails.UserDetails; import org.springframework.security.userdetails.UserDetailsService; import org.springframework.security.userdetails.UsernameNotFoundException; import podd.dataaccess.RepositoryRoleDAO; import podd.dataaccess.UserDAO; import podd.model.user.RepositoryRole; import podd.model.user.User; import static podd.model.user.UserStatus.ACTIVE; /** * @author Yuan-Fang Li * @version $Id$ */ public class PoddUserDetailsServiceImpl implements UserDetailsService { private UserDAO userDao; private RepositoryRoleDAO rrDao; public PoddUserDetailsServiceImpl(UserDAO userDao, RepositoryRoleDAO rrDao) { this.userDao = userDao; this.rrDao = rrDao; } @Override public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException, DataAccessException { try { User user = userDao.loadByUserName(s.toLowerCase()); if (null == user) { throw new UsernameNotFoundException("Cannot find user " + s + " in the system."); } else { boolean accountActive = user.getStatus().equals(ACTIVE); final GrantedAuthority[] grantedAuthorities = getUserAuthorities( user.getRepositoryRole().getName()); return new org.springframework.security.userdetails.User(s, user.getPasswordHash(), accountActive, true, true, accountActive, grantedAuthorities); } } catch (podd.dataaccess.exception.DataAccessException e) { throw new ObjectRetrievalFailureException("Error loading user by name: " + s, e); } } private GrantedAuthority[] getUserAuthorities(String userName) throws podd.dataaccess.exception.DataAccessException { final RepositoryRole role = rrDao.getRepositoryRole(userName); if (null == role) { throw new UsernameNotFoundException("User " + userName + " does not have a valid role in the system."); } GrantedAuthority authority = new GrantedAuthorityImpl(role.getName()); return new GrantedAuthority[] { authority }; } }