Java tutorial
/** * Copyright 2013 Stockholm County Council * * This file is part of APIGW * * APIGW is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * APIGW 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with APIGW; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package org.apigw.authserver.shibboleth; import org.apigw.authserver.types.domain.User; import org.apigw.commons.logging.CitizenLoggingUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.util.Assert; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; public class ShibbolethSamlUserDetailsService implements AuthenticationUserDetailsService, InitializingBean { private static final Logger logger = LoggerFactory.getLogger(ShibbolethSamlUserDetailsService.class); private static final Pattern ssnPattern = Pattern.compile("^\\d{12}$"); //good enough? private CitizenLoggingUtil citizenLoggingUtil; private Set<String> grantedAuthorities; @Override public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException { final Map<String, String> principal = (Map) token.getPrincipal(); //TODO: principal should be something better final String subjectSerialNumber = principal.get("subjectSerialNumber"); final String subjectCommonName = principal.get("subjectCommonName"); if (subjectSerialNumber != null && ssnPattern.matcher(subjectSerialNumber).matches()) { final User user = new User(subjectSerialNumber, grantedAuthorities); user.setFullName(subjectCommonName); user.setEnabled(true); logger.debug("returning user"); return user; } throw new UsernameNotFoundException("expected subjectSerialNumber but couldn't find it"); } public void setGrantedAuthorities(Set<String> grantedAuthorities) { this.grantedAuthorities = grantedAuthorities; } public void setCitizenLoggingUtil(CitizenLoggingUtil citizenLoggingUtil) { this.citizenLoggingUtil = citizenLoggingUtil; } @Override public void afterPropertiesSet() throws Exception { Assert.notNull(grantedAuthorities, "grantedAuthorities can not be null"); Assert.notNull(citizenLoggingUtil, "citizenLoggingUtil must be configured"); } }