1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.security.authentication;
15
16 import javax.servlet.ServletRequest;
17 import javax.servlet.ServletResponse;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpSession;
20
21 import org.eclipse.jetty.security.Authentication;
22 import org.eclipse.jetty.security.Authenticator;
23 import org.eclipse.jetty.security.DefaultAuthentication;
24 import org.eclipse.jetty.security.ServerAuthException;
25
26
27
28
29 public class SessionCachingAuthenticator extends DelegateAuthenticator
30 {
31 public final static String __J_AUTHENTICATED = "org.eclipse.jetty.server.Auth";
32
33
34 public SessionCachingAuthenticator(Authenticator delegate)
35 {
36 super(delegate);
37 }
38
39 public Authentication validateRequest(ServletRequest request, ServletResponse response, boolean mandatory) throws ServerAuthException
40 {
41 HttpSession session = ((HttpServletRequest)request).getSession(mandatory);
42
43 if (session == null)
44 return DefaultAuthentication.SUCCESS_UNAUTH_RESULTS;
45
46 Authentication authentication = (Authentication) session.getAttribute(__J_AUTHENTICATED);
47 if (authentication != null)
48 return authentication;
49
50 authentication = _delegate.validateRequest(request, response, mandatory);
51 if (authentication != null && authentication.getUserIdentity().getSubject() != null)
52 {
53 Authentication next=new DefaultAuthentication(Authentication.Status.SUCCESS,authentication.getAuthMethod(),authentication.getUserIdentity());
54 session.setAttribute(__J_AUTHENTICATED, next);
55 }
56 return authentication;
57 }
58
59 }