Java tutorial
/* * Copyright (C) 2011 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.scoppelletti.programmerpower.security; import java.util.*; import javax.persistence.*; import org.apache.commons.codec.digest.*; import org.springframework.security.authentication.*; import org.springframework.security.core.*; import org.springframework.security.core.context.*; import org.springframework.security.core.userdetails.*; import org.springframework.transaction.annotation.*; import it.scoppelletti.programmerpower.*; import it.scoppelletti.programmerpower.data.*; import it.scoppelletti.programmerpower.reflect.*; import it.scoppelletti.programmerpower.types.*; /** * Implementazione di default dell’interfaccia {@code UserManager}. * * @since 1.0.0 */ @Final @Transactional public class DefaultUserManager implements UserManager, UserDetailsService { @PersistenceContext(unitName = DataUtils.PERSISTENCE_UNIT) private EntityManager myEntityMgr; /** * Costruttore. */ public DefaultUserManager() { } @Transactional(readOnly = true) public User loadUser(int userId) { return myEntityMgr.find(User.class, userId); } @Transactional(readOnly = true) public User loadLoggedUser() { User principal; Authentication auth; SecurityContext secCtx = SecurityContextHolder.getContext(); auth = secCtx.getAuthentication(); if (auth == null || !auth.isAuthenticated() || auth instanceof AnonymousAuthenticationToken) { return null; } principal = (User) auth.getPrincipal(); return loadUser(principal.getId()); } /** * Legge un utente. * * @param userName Codice per l’autenticazione dell’utente. * @return Utente. Non può essere {@code null}. */ @Transactional(readOnly = true) public UserDetails loadUserByUsername(String userName) { Query query; if (Strings.isNullOrEmpty(userName)) { throw new ArgumentNullException("userName"); } query = myEntityMgr.createQuery("from User where code = :code").setParameter("code", userName); return (User) query.getSingleResult(); } public void saveUser(User user) { if (user == null) { throw new ArgumentNullException("user"); } myEntityMgr.persist(user); } public User updateUser(User user) { if (user == null) { throw new ArgumentNullException("user"); } return myEntityMgr.merge(user); } public void deleteUser(User user) { if (user == null) { throw new ArgumentNullException("user"); } myEntityMgr.remove(user); } @Transactional(readOnly = true) public List<User> listUsers() { User user; Query query; List<User> list; list = new ArrayList<User>(); query = myEntityMgr.createQuery("from User order by code"); for (Object item : query.getResultList()) { user = (User) item; list.add(user); } return list; } @Transactional(propagation = Propagation.SUPPORTS) public String digestPassword(SecureString value) { return DigestUtils.sha256Hex(value.getBytes()); } }