/******************************************************************************* * Copyright 2013 * Ubiquitous Knowledge Processing (UKP) Lab * Technische Universität Darmstadt * * 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 de.tudarmstadt.ukp.csniper.webapp.security; import static java.lang.String.format; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.wicket.authroles.authentication.AuthenticatedWebSession; import org.apache.wicket.authroles.authorization.strategies.role.Roles; import org.apache.wicket.injection.Injector; import org.apache.wicket.request.Request; import org.apache.wicket.spring.injection.annot.SpringBean; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; public class SpringAuthenticatedWebSession extends AuthenticatedWebSession { private static final long serialVersionUID = 1L; private final Log log = LogFactory.getLog(getClass()); @SpringBean(name = "org.springframework.security.authenticationManager") private AuthenticationManager authenticationManager; public SpringAuthenticatedWebSession(Request request) { super(request); injectDependencies(); ensureDependenciesNotNull(); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth != null) { signIn(auth.isAuthenticated()); } } private void ensureDependenciesNotNull() { if (authenticationManager == null) { throw new IllegalStateException("AdminSession requires an authenticationManager."); } } private void injectDependencies() { Injector.get().inject(this); } @Override public boolean authenticate(String username, String password) { boolean authenticated = false; try { Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(username, password)); SecurityContextHolder.getContext().setAuthentication(authentication); authenticated = authentication.isAuthenticated(); } catch (AuthenticationException e) { log.warn(format("User '%s' failed to login. Reason: %s", username, e.getMessage())); error(format("User '%s' failed to login. Reason: %s", username, e.getMessage())); authenticated = false; } return authenticated; } @Override public void signOut() { super.signOut(); SecurityContextHolder.clearContext(); } @Override public Roles getRoles() { Roles roles = new Roles(); getRolesIfSignedIn(roles); return roles; } private void getRolesIfSignedIn(Roles roles) { if (isSignedIn()) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); addRolesFromAuthentication(roles, authentication); } } private void addRolesFromAuthentication(Roles roles, Authentication authentication) { for (GrantedAuthority authority : authentication.getAuthorities()) { roles.add(authority.getAuthority()); } } }