1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.rcp;
17
18 import org.acegisecurity.Authentication;
19 import org.acegisecurity.AuthenticationException;
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.providers.AuthenticationProvider;
22 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.util.Assert;
29
30
31 /***
32 * Client-side object which queries a {@link RemoteAuthenticationManager} to
33 * validate an authentication request.
34 *
35 * <P>
36 * A new <code>Authentication</code> object is created by this class comprising
37 * the request <code>Authentication</code> object's <code>principal</code>,
38 * <code>credentials</code> and the <code>GrantedAuthority</code>[]s returned
39 * by the <code>RemoteAuthenticationManager</code>.
40 * </p>
41 *
42 * <P>
43 * The <code>RemoteAuthenticationManager</code> should not require any special
44 * username or password setting on the remoting client proxy factory to
45 * execute the call. Instead the entire authentication request must be
46 * encapsulated solely within the <code>Authentication</code> request object.
47 * In practical terms this means the <code>RemoteAuthenticationManager</code>
48 * will <B>not</B> be protected by BASIC or any other HTTP-level
49 * authentication.
50 * </p>
51 *
52 * <P>
53 * If authentication fails, a <code>RemoteAuthenticationException</code> will
54 * be thrown. This exception should be caught and displayed to the user,
55 * enabling them to retry with alternative credentials etc.
56 * </p>
57 *
58 * @author Ben Alex
59 * @version $Id: RemoteAuthenticationProvider.java,v 1.3 2005/11/17 00:55:51 benalex Exp $
60 */
61 public class RemoteAuthenticationProvider implements AuthenticationProvider,
62 InitializingBean {
63
64
65 private static final Log logger = LogFactory.getLog(RemoteAuthenticationProvider.class);
66
67
68
69 private RemoteAuthenticationManager remoteAuthenticationManager;
70
71
72
73 public void setRemoteAuthenticationManager(
74 RemoteAuthenticationManager remoteAuthenticationManager) {
75 this.remoteAuthenticationManager = remoteAuthenticationManager;
76 }
77
78 public RemoteAuthenticationManager getRemoteAuthenticationManager() {
79 return remoteAuthenticationManager;
80 }
81
82 public void afterPropertiesSet() throws Exception {
83 Assert.notNull(this.remoteAuthenticationManager, "remoteAuthenticationManager is mandatory");
84 }
85
86 public Authentication authenticate(Authentication authentication)
87 throws AuthenticationException {
88 String username = authentication.getPrincipal().toString();
89 String password = authentication.getCredentials().toString();
90 GrantedAuthority[] authorities = remoteAuthenticationManager
91 .attemptAuthentication(username, password);
92
93 return new UsernamePasswordAuthenticationToken(username, password,
94 authorities);
95 }
96
97 public boolean supports(Class authentication) {
98 return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
99 }
100 }