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