]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/MatchingAuthenticationProvider.java
Improve system execution
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / core / MatchingAuthenticationProvider.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.security.core;
18
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Properties;
23
24 import org.springframework.core.io.Resource;
25 import org.springframework.security.AuthenticationException;
26 import org.springframework.security.BadCredentialsException;
27 import org.springframework.security.GrantedAuthority;
28 import org.springframework.security.GrantedAuthorityImpl;
29 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
30 import org.springframework.security.providers.dao.AbstractUserDetailsAuthenticationProvider;
31 import org.springframework.security.userdetails.User;
32 import org.springframework.security.userdetails.UserDetails;
33
34 public class MatchingAuthenticationProvider extends
35 AbstractUserDetailsAuthenticationProvider {
36
37 private Resource mapping;
38 private Properties properties;
39
40 private List<String> defaultRoles = new ArrayList<String>();
41
42 @Override
43 protected void doAfterPropertiesSet() throws Exception {
44 properties = new Properties();
45 InputStream propIn = mapping.getInputStream();
46 try {
47 properties.load(propIn);
48 } finally {
49 propIn.close();
50 }
51 }
52
53 @Override
54 protected void additionalAuthenticationChecks(UserDetails userDetails,
55 UsernamePasswordAuthenticationToken authentication)
56 throws AuthenticationException {
57 if (!userDetails.getPassword().equals(authentication.getCredentials()))
58 throw new BadCredentialsException(
59 "Invalid credentails provided by "
60 + authentication.getName());
61 }
62
63 @Override
64 protected UserDetails retrieveUser(String username,
65 UsernamePasswordAuthenticationToken authentication)
66 throws AuthenticationException {
67 String value = properties.getProperty(username);
68 if (value == null)
69 throw new BadCredentialsException("User " + username
70 + " is not registered");
71 List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
72 for (String role : defaultRoles)
73 grantedAuthorities.add(new GrantedAuthorityImpl(role));
74 return new User(
75 username,
76 value,
77 true,
78 true,
79 true,
80 true,
81 grantedAuthorities
82 .toArray(new GrantedAuthority[grantedAuthorities.size()]));
83 }
84
85 public void setMapping(Resource mapping) {
86 this.mapping = mapping;
87 }
88
89 public void setDefaultRoles(List<String> defaultRoles) {
90 this.defaultRoles = defaultRoles;
91 }
92
93 }