2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.security
.core
;
18 import java
.io
.InputStream
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.Properties
;
23 import org
.springframework
.core
.io
.Resource
;
24 import org
.springframework
.security
.AuthenticationException
;
25 import org
.springframework
.security
.BadCredentialsException
;
26 import org
.springframework
.security
.GrantedAuthority
;
27 import org
.springframework
.security
.GrantedAuthorityImpl
;
28 import org
.springframework
.security
.providers
.UsernamePasswordAuthenticationToken
;
29 import org
.springframework
.security
.providers
.dao
.AbstractUserDetailsAuthenticationProvider
;
30 import org
.springframework
.security
.userdetails
.User
;
31 import org
.springframework
.security
.userdetails
.UserDetails
;
35 public class MatchingAuthenticationProvider
extends
36 AbstractUserDetailsAuthenticationProvider
{
38 private Resource mapping
;
39 private Properties properties
;
41 private List
<String
> defaultRoles
= new ArrayList
<String
>();
44 protected void doAfterPropertiesSet() throws Exception
{
45 properties
= new Properties();
46 InputStream propIn
= mapping
.getInputStream();
48 properties
.load(propIn
);
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());
65 protected UserDetails
retrieveUser(String username
,
66 UsernamePasswordAuthenticationToken authentication
)
67 throws AuthenticationException
{
68 String value
= properties
.getProperty(username
);
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
));
83 .toArray(new GrantedAuthority
[grantedAuthorities
.size()]));
86 public void setMapping(Resource mapping
) {
87 this.mapping
= mapping
;
90 public void setDefaultRoles(List
<String
> defaultRoles
) {
91 this.defaultRoles
= defaultRoles
;