1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.dao;
17
18 import org.acegisecurity.AuthenticationException;
19 import org.acegisecurity.AuthenticationServiceException;
20 import org.acegisecurity.BadCredentialsException;
21 import org.acegisecurity.providers.AuthenticationProvider;
22 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
23 import org.acegisecurity.providers.encoding.PasswordEncoder;
24 import org.acegisecurity.providers.encoding.PlaintextPasswordEncoder;
25 import org.acegisecurity.userdetails.UserDetails;
26 import org.acegisecurity.userdetails.UserDetailsService;
27 import org.springframework.dao.DataAccessException;
28 import org.springframework.util.Assert;
29
30
31
32
33
34
35
36
37
38 public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
39
40
41
42
43 private PasswordEncoder passwordEncoder = new PlaintextPasswordEncoder();
44
45 private SaltSource saltSource;
46
47 private UserDetailsService userDetailsService;
48
49 private boolean includeDetailsObject = true;
50
51
52
53
54 protected void additionalAuthenticationChecks(UserDetails userDetails,
55 UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
56 Object salt = null;
57
58 if (this.saltSource != null) {
59 salt = this.saltSource.getSalt(userDetails);
60 }
61
62 if (authentication.getCredentials() == null) {
63 throw new BadCredentialsException(messages.getMessage(
64 "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"),
65 includeDetailsObject ? userDetails : null);
66 }
67
68 String presentedPassword = authentication.getCredentials() == null ? "" : authentication.getCredentials()
69 .toString();
70
71 if (!passwordEncoder.isPasswordValid(userDetails.getPassword(), presentedPassword, salt)) {
72 throw new BadCredentialsException(messages.getMessage(
73 "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"),
74 includeDetailsObject ? userDetails : null);
75 }
76 }
77
78 protected void doAfterPropertiesSet() throws Exception {
79 Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
80 }
81
82 public PasswordEncoder getPasswordEncoder() {
83 return passwordEncoder;
84 }
85
86 public SaltSource getSaltSource() {
87 return saltSource;
88 }
89
90 public UserDetailsService getUserDetailsService() {
91 return userDetailsService;
92 }
93
94 protected final UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
95 throws AuthenticationException {
96 UserDetails loadedUser;
97
98 try {
99 loadedUser = this.getUserDetailsService().loadUserByUsername(username);
100 }
101 catch (DataAccessException repositoryProblem) {
102 throw new AuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem);
103 }
104
105 if (loadedUser == null) {
106 throw new AuthenticationServiceException(
107 "UserDetailsService returned null, which is an interface contract violation");
108 }
109 return loadedUser;
110 }
111
112
113
114
115
116
117
118
119 public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
120 this.passwordEncoder = passwordEncoder;
121 }
122
123
124
125
126
127
128
129
130
131
132 public void setSaltSource(SaltSource saltSource) {
133 this.saltSource = saltSource;
134 }
135
136 public void setUserDetailsService(UserDetailsService userDetailsService) {
137 this.userDetailsService = userDetailsService;
138 }
139
140 public boolean isIncludeDetailsObject() {
141 return includeDetailsObject;
142 }
143
144 public void setIncludeDetailsObject(boolean includeDetailsObject) {
145 this.includeDetailsObject = includeDetailsObject;
146 }
147 }