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