]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/MatchingAuthenticationProvider.java
Add ActiveMQ security support
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / core / MatchingAuthenticationProvider.java
1 package org.argeo.security.core;
2
3 import java.io.InputStream;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Properties;
7
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;
17
18 public class MatchingAuthenticationProvider extends
19 AbstractUserDetailsAuthenticationProvider {
20
21 private Resource mapping;
22 private Properties properties;
23
24 private List<String> defaultRoles = new ArrayList<String>();
25
26 @Override
27 protected void doAfterPropertiesSet() throws Exception {
28 properties = new Properties();
29 InputStream propIn = mapping.getInputStream();
30 try {
31 properties.load(propIn);
32 } finally {
33 propIn.close();
34 }
35 }
36
37 @Override
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());
45 }
46
47 @Override
48 protected UserDetails retrieveUser(String username,
49 UsernamePasswordAuthenticationToken authentication)
50 throws AuthenticationException {
51 String value = properties.getProperty(username);
52 if (value == null)
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));
58 return new User(
59 username,
60 value,
61 true,
62 true,
63 true,
64 true,
65 grantedAuthorities
66 .toArray(new GrantedAuthority[grantedAuthorities.size()]));
67 }
68
69 public void setMapping(Resource mapping) {
70 this.mapping = mapping;
71 }
72
73 public void setDefaultRoles(List<String> defaultRoles) {
74 this.defaultRoles = defaultRoles;
75 }
76
77 }