Java tutorial
/* * * Springstrap * * @author Jan Philipp Knller <info@pksoftware.de> * * Homepage: http://ui5strap.com/springstrap * * Copyright (c) 2013-2014 Jan Philipp Knller <info@pksoftware.de> * * 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. * Released under Apache2 license: http://www.apache.org/licenses/LICENSE-2.0.txt * */ package de.pksoftware.springstrap.core.service; import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.WebAuthenticationDetails; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import de.pksoftware.springstrap.core.domain.IAccount; import de.pksoftware.springstrap.core.domain.Identity; import de.pksoftware.springstrap.core.exception.NoSuchAccountException; import de.pksoftware.springstrap.core.exception.NoSuchIdentityException; @Service public abstract class AccountServiceBase implements IAccountService, ApplicationListener<InteractiveAuthenticationSuccessEvent> { private List<IdentityService> identityServices = new ArrayList<IdentityService>(); // Logger private Logger logger = LoggerFactory.getLogger(AccountServiceBase.class); public abstract IAccount loadAccountByUsernameOrEmail(String username) throws NoSuchAccountException; public abstract IAccount loadAccountById(long accountId) throws NoSuchAccountException; public abstract IAccount saveAccount(IAccount account); /** * Returns the List of IdentityService's that are used in this * AccountService. * * @return */ public List<IdentityService> getIdentityServices() { return identityServices; } /** * Adds a new IdentityService to this AccountService. * * @param identityService */ public void addIdentityService(IdentityService identityService) { Assert.notNull(identityService, "Identity service cannot be null!"); this.identityServices.add(identityService); } /** * Returns a Set of Identities associated to the given Account. * * @param account * @return */ public Set<Identity> retrieveIdentities(IAccount account) { Set<Identity> identities = new HashSet<Identity>(); for (IdentityService identityService : this.identityServices) { try { Identity identity = identityService.loadIdentityByAccount(account); identities.add(identity); } catch (NoSuchIdentityException e) { logger.debug("No Indentity of type {} found for Account {}", identityService.getClass(), account); } } return identities; } /** * Triggered on account login. * @param event The Spring interactive authentication event. * TODO implement lastLoginIp and lastVisitIp */ @Override @Transactional public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) { Authentication userAuth = event.getAuthentication(); logger.info("Authenticated using {}", userAuth); String remoteAddress = null; WebAuthenticationDetails details = (WebAuthenticationDetails) userAuth.getDetails(); if (null != details) { remoteAddress = details.getRemoteAddress(); } else { logger.warn("Cannot determine remote address!"); } IAccount account = (IAccount) userAuth.getPrincipal(); ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime lastLoginDate = account.getLastLoginDate(); String lastLoginIp = account.getLastLoginIp(); if (null != lastLoginDate) { account.setLastVisitDate(lastLoginDate); } if (null != lastLoginIp) { account.setLastVisitIp(lastLoginIp); } account.setLastLoginDate(now); account.setLastLoginIp(remoteAddress); this.saveAccount(account); logger.info("SUCCESSFUL Authentication with {}", account); } }