1 package org
.argeo
.security
.core
;
3 import java
.io
.InputStream
;
4 import java
.util
.ArrayList
;
6 import java
.util
.Properties
;
8 import org
.springframework
.core
.io
.Resource
;
9 import org
.springframework
.security
.AuthenticationException
;
10 import org
.springframework
.security
.BadCredentialsException
;
11 import org
.springframework
.security
.GrantedAuthority
;
12 import org
.springframework
.security
.GrantedAuthorityImpl
;
13 import org
.springframework
.security
.providers
.UsernamePasswordAuthenticationToken
;
14 import org
.springframework
.security
.providers
.dao
.AbstractUserDetailsAuthenticationProvider
;
15 import org
.springframework
.security
.userdetails
.User
;
16 import org
.springframework
.security
.userdetails
.UserDetails
;
18 public class MatchingAuthenticationProvider
extends
19 AbstractUserDetailsAuthenticationProvider
{
21 private Resource mapping
;
22 private Properties properties
;
24 private List
<String
> defaultRoles
= new ArrayList
<String
>();
27 protected void doAfterPropertiesSet() throws Exception
{
28 properties
= new Properties();
29 InputStream propIn
= mapping
.getInputStream();
31 properties
.load(propIn
);
38 protected void additionalAuthenticationChecks(UserDetails userDetails
,
39 UsernamePasswordAuthenticationToken authentication
)
40 throws AuthenticationException
{
41 if (!userDetails
.getPassword().equals(authentication
.getCredentials()))
42 throw new BadCredentialsException(
43 "Invalid credentails provided by "
44 + authentication
.getName());
48 protected UserDetails
retrieveUser(String username
,
49 UsernamePasswordAuthenticationToken authentication
)
50 throws AuthenticationException
{
51 String value
= properties
.getProperty(username
);
53 throw new BadCredentialsException("User " + username
54 + " is not registered");
55 List
<GrantedAuthority
> grantedAuthorities
= new ArrayList
<GrantedAuthority
>();
56 for (String role
: defaultRoles
)
57 grantedAuthorities
.add(new GrantedAuthorityImpl(role
));
66 .toArray(new GrantedAuthority
[grantedAuthorities
.size()]));
69 public void setMapping(Resource mapping
) {
70 this.mapping
= mapping
;
73 public void setDefaultRoles(List
<String
> defaultRoles
) {
74 this.defaultRoles
= defaultRoles
;