2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 * This document is a part of the source code and related artifacts
25 * for CollectionSpace, an open source collections management system
26 * for museums and related institutions:
28 * http://www.collectionspace.org
29 * http://wiki.collectionspace.org
31 * Copyright 2009 University of California at Berkeley
33 * Licensed under the Educational Community License (ECL), Version 2.0.
34 * You may not use this file except in compliance with this License.
36 * You may obtain a copy of the ECL 2.0 License at
38 * https://source.collectionspace.org/collection-space/LICENSE.txt
40 * Unless required by applicable law or agreed to in writing, software
41 * distributed under the License is distributed on an "AS IS" BASIS,
42 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43 * See the License for the specific language governing permissions and
44 * limitations under the License.
46 package org.collectionspace.authentication.spring;
48 import java.util.LinkedHashSet;
51 import javax.security.auth.login.AccountException;
52 import javax.security.auth.login.AccountNotFoundException;
54 import org.collectionspace.authentication.CSpaceTenant;
55 import org.collectionspace.authentication.CSpaceUser;
56 import org.collectionspace.authentication.realm.CSpaceRealm;
57 import org.springframework.security.authentication.AuthenticationServiceException;
58 import org.springframework.security.core.GrantedAuthority;
59 import org.springframework.security.core.authority.SimpleGrantedAuthority;
60 import org.springframework.security.core.userdetails.UserDetails;
61 import org.springframework.security.core.userdetails.UserDetailsService;
62 import org.springframework.security.core.userdetails.UsernameNotFoundException;
65 * A Spring UserDetailsService for CollectionSpace.
67 public class CSpaceUserDetailsService implements UserDetailsService {
68 private CSpaceRealm realm = null;
70 public CSpaceUserDetailsService(CSpaceRealm realm) {
75 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
76 String password = null;
77 Set<CSpaceTenant> tenants = null;
78 Set<GrantedAuthority> grantedAuthorities = null;
81 password = realm.getPassword(username);
82 tenants = getTenants(username);
83 grantedAuthorities = getAuthorities(username);
85 catch (AccountNotFoundException e) {
86 throw new UsernameNotFoundException(e.getMessage(), e);
88 catch (AccountException e) {
89 throw new AuthenticationServiceException(e.getMessage(), e);
100 protected Set<GrantedAuthority> getAuthorities(String username) throws AccountException {
101 Set<String> roles = realm.getRoles(username);
102 Set<GrantedAuthority> authorities = new LinkedHashSet<GrantedAuthority>(roles.size());
104 for (String role : roles) {
105 authorities.add(new SimpleGrantedAuthority(role));
111 protected Set<CSpaceTenant> getTenants(String username) throws AccountException {
112 Set<CSpaceTenant> tenants = realm.getTenants(username);