001/* 002 * Copyright (c) 2013 - 2016 TDP Ltd All Rights Reserved. 003 * TDP Ltd grants permission, free of charge, to any person obtaining copies 004 * of this software and its associated documentation files (the "Software"), 005 * to deal in the Software without restriction, including to use, copy, adapt, 006 * publish, distribute, display, perform, sublicense, and sell copies of the 007 * Software, subject to the following condition: You must include the above 008 * copyright notice and this permission notice in all full or partial copies 009 * of the Software. 010 * 011 * TDP LTD PROVIDES THE SOFTWARE "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, 012 * INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 013 * PARTICULAR PURPOSE, AND NON-INFRINGMENT. TDP LTD, THE AUTHORS OF THE SOFTWARE, 014 * AND THE OWNERS OF COPYRIGHT IN THE SOFTWARE ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, 015 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING 016 * FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 017 * THE SOFTWARE. 018 */ 019package cz.tdp.kshield.springsecurity3; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.springframework.beans.factory.annotation.Autowired; 024import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 025import org.springframework.security.core.Authentication; 026import org.springframework.security.core.userdetails.UserDetails; 027import org.springframework.security.core.userdetails.UserDetailsService; 028import org.springframework.security.core.userdetails.UsernameNotFoundException; 029 030import cz.tdp.kshield.client.UserInfo; 031import cz.tdp.kshield.integration.AuthenticationFactory; 032 033/** 034 * Implementation of AuthenticationFactory used in SpringAuthenticationServiceImpl based on UserDetailsService. 035 * 036 * @see #createAuthenication(UserInfo) 037 * @see cz.tdp.kshield.springsecurity3.SpringAuthenticationServiceImpl 038 * @see org.springframework.security.core.userdetails.UserDetailsService 039 */ 040public class SpringAuthenticationFactory implements AuthenticationFactory<Authentication> 041{ 042 /** 043 * Retrieves userDetails and creates new UsernamePasswordAuthenticationToken instance 044 * 045 * @return new UsernamePasswordAuthenticationToken instance or null if authentication cannot be created 046 */ 047 @Override 048 public Authentication createAuthenication(UserInfo userInfo) { 049 final UserDetails userDetails = userDetailsService.loadUserByUsername(userInfo.getUsername()); 050 051 if (userDetails != null) { 052 try { 053 return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getUsername(), userDetails.getAuthorities()); 054 } 055 catch (UsernameNotFoundException e) { 056 log.warn("Unknown username", e); 057 } 058 } 059 060 return null; 061 } 062 063 @Autowired 064 private UserDetailsService userDetailsService; 065 066 public void setUserDetailsService(UserDetailsService userDetailsService) { 067 this.userDetailsService = userDetailsService; 068 } 069 070 private static final Log log = LogFactory.getLog(SpringAuthenticationFactory.class); 071}