Java tutorial
/* * Copyright 2002-2012 the original author or authors. * * 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 org.client.one.service; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.client.one.model.Profile; import org.client.one.model.UserIdentity; import org.springframework.security.authentication.encoding.Md5PasswordEncoder; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; @Service public class UserIdentityDetailsService implements AuthenticationUserDetailsService<Authentication>, UserDetailsService, Serializable { private static final long serialVersionUID = 2590610157568192532L; public UserDetails loadUserByUsername(String userName) { Profile user = new Profile(); // user.setEmail(userName); // user.setClave(generateEncryptedPassword("xxx","")); // user.setEsActivo(true); List<String> roles = new ArrayList<String>(); roles.add("ROLE_USER"); UserIdentity iden = new UserIdentity(); iden.setIdentity(user); iden.setCustomerAuthorities(roles); return (UserDetails) iden; } /** * Returns the domain User object for the currently logged in user, or null * if no User is logged in. * * @return User object for the currently logged in user, or null if no User * is logged in. */ public static UserIdentity getCurrentUser() { SecurityContext sc = SecurityContextHolder.getContext(); if (sc == null) return null; if (sc.getAuthentication() == null) return null; Object principal = sc.getAuthentication().getPrincipal(); if (principal instanceof UserIdentity) return ((UserIdentity) principal); // principal object is either null or represents anonymous user - // neither of which our domain User object can represent - so return null return null; } /** * Returns the domain User object for the currently logged in user, or null * if no User is logged in. * * @return User object for the currently logged in user, or null if no User * is logged in. */ public static Profile getIdentity() { UserIdentity user = getCurrentUser(); if (user == null) return null; return user.getIdentity(); } /** * Utility method to determine if the current user is logged in / * authenticated. * <p> * Equivalent of calling: * <p> * <code>getCurrentUser() != null</code> * * @return if user is logged in */ public static boolean isLoggedIn() { return getCurrentUser() != null; } public String generateEncryptedPassword(String clearPassword, String salt) { Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder(); String encodedPassword = passwordEncoder.encodePassword(clearPassword, salt); return encodedPassword; } @Override public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException { return (UserDetails) token.getPrincipal(); } }