1 28 29 package com.caucho.server.security; 30 31 import com.caucho.log.Log; 32 33 import javax.annotation.PostConstruct; 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.servlet.ServletContext ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import java.io.IOException ; 41 import java.security.Principal ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 45 81 public abstract class AbstractLogin { 82 protected final static Logger log = Log.open(AbstractLogin.class); 83 84 89 protected ServletAuthenticator _auth; 90 91 94 public void setAuthenticator(ServletAuthenticator auth) 95 { 96 _auth = auth; 97 } 98 99 102 public ServletAuthenticator getAuthenticator() 103 { 104 if (_auth == null) { 105 try { 106 Context ic = new InitialContext (); 107 _auth = (ServletAuthenticator) ic.lookup("java:comp/env/caucho/auth"); 108 } catch (Exception e) { 109 log.log(Level.FINEST, e.toString(), e); 110 } 111 112 if (_auth == null) 113 _auth = new NullAuthenticator(); 114 } 115 116 return _auth; 117 } 118 119 123 @PostConstruct 124 public void init() 125 throws ServletException 126 { 127 } 128 129 133 public String getAuthType() 134 { 135 return "none"; 136 } 137 138 149 public Principal authenticate(HttpServletRequest request, 150 HttpServletResponse response, 151 ServletContext application) 152 throws ServletException , IOException 153 { 154 Principal user = getUserPrincipal(request, response, application); 157 158 if (user == null) 159 response.sendError(HttpServletResponse.SC_FORBIDDEN); 160 161 return user; 162 } 163 164 177 public Principal getUserPrincipal(HttpServletRequest request, 178 HttpServletResponse response, 179 ServletContext application) 180 throws ServletException 181 { 182 return getAuthenticator().getUserPrincipal(request, response, application); 183 } 184 185 195 public boolean isUserInRole(HttpServletRequest request, 196 HttpServletResponse response, 197 ServletContext application, 198 Principal user, String role) 199 throws ServletException 200 { 201 return getAuthenticator().isUserInRole(request, response, 202 application, user, role); 203 } 204 205 212 public void logout(HttpServletRequest request, 213 HttpServletResponse response, 214 ServletContext application) 215 throws ServletException 216 { 217 Principal principal = getUserPrincipal(request, response, application); 218 219 if (principal != null) 220 getAuthenticator().logout(application, 221 null, 222 request.getRequestedSessionId(), 223 principal); 224 } 225 } 226 | Popular Tags |