2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.security
.core
;
19 import java
.io
.InputStream
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
22 import java
.util
.Properties
;
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
;
34 public class MatchingAuthenticationProvider
extends
35 AbstractUserDetailsAuthenticationProvider
{
37 private Resource mapping
;
38 private Properties properties
;
40 private List
<String
> defaultRoles
= new ArrayList
<String
>();
43 protected void doAfterPropertiesSet() throws Exception
{
44 properties
= new Properties();
45 InputStream propIn
= mapping
.getInputStream();
47 properties
.load(propIn
);
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());
64 protected UserDetails
retrieveUser(String username
,
65 UsernamePasswordAuthenticationToken authentication
)
66 throws AuthenticationException
{
67 String value
= properties
.getProperty(username
);
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
));
82 .toArray(new GrantedAuthority
[grantedAuthorities
.size()]));
85 public void setMapping(Resource mapping
) {
86 this.mapping
= mapping
;
89 public void setDefaultRoles(List
<String
> defaultRoles
) {
90 this.defaultRoles
= defaultRoles
;