]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/core/MatchingAuthenticationProvider.java
Node registration
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / core / MatchingAuthenticationProvider.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.core;
17
18 import java.io.InputStream;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.springframework.core.io.Resource;
24 import org.springframework.security.authentication.BadCredentialsException;
25 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
26 import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
27 import org.springframework.security.core.AuthenticationException;
28 import org.springframework.security.core.GrantedAuthority;
29 import org.springframework.security.core.authority.GrantedAuthorityImpl;
30 import org.springframework.security.core.userdetails.User;
31 import org.springframework.security.core.userdetails.UserDetails;
32
33 /** @deprecated */
34 @Deprecated
35 public class MatchingAuthenticationProvider extends
36 AbstractUserDetailsAuthenticationProvider {
37
38 private Resource mapping;
39 private Properties properties;
40
41 private List<String> defaultRoles = new ArrayList<String>();
42
43 @Override
44 protected void doAfterPropertiesSet() throws Exception {
45 properties = new Properties();
46 InputStream propIn = mapping.getInputStream();
47 try {
48 properties.load(propIn);
49 } finally {
50 propIn.close();
51 }
52 }
53
54 @Override
55 protected void additionalAuthenticationChecks(UserDetails userDetails,
56 UsernamePasswordAuthenticationToken authentication)
57 throws AuthenticationException {
58 if (!userDetails.getPassword().equals(authentication.getCredentials()))
59 throw new BadCredentialsException(
60 "Invalid credentails provided by "
61 + authentication.getName());
62 }
63
64 @Override
65 protected UserDetails retrieveUser(String username,
66 UsernamePasswordAuthenticationToken authentication)
67 throws AuthenticationException {
68 String value = properties.getProperty(username);
69 if (value == null)
70 throw new BadCredentialsException("User " + username
71 + " is not registered");
72 List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
73 for (String role : defaultRoles)
74 grantedAuthorities.add(new GrantedAuthorityImpl(role));
75 return new User(username, value, true, true, true, true,
76 grantedAuthorities);
77 }
78
79 public void setMapping(Resource mapping) {
80 this.mapping = mapping;
81 }
82
83 public void setDefaultRoles(List<String> defaultRoles) {
84 this.defaultRoles = defaultRoles;
85 }
86
87 }