1 20 21 package com.methodhead.auth; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.Cookie ; 26 27 import org.apache.struts.action.Action; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import com.methodhead.util.StrutsUtil; 32 33 42 public abstract class AuthAction 43 extends 44 Action { 45 46 48 50 52 54 public ActionForward execute( 55 ActionMapping mapping, 56 ActionForm form, 57 HttpServletRequest request, 58 HttpServletResponse response ) 59 throws 60 Exception { 61 62 AuthUser user = AuthUtil.getUser( request ); 66 67 if ( user == null ) { 68 69 Cookie [] cookies = request.getCookies(); 73 74 if ( cookies != null ) { 75 for ( int i = 0; i < cookies.length; i++ ) { 76 if ( cookies[ i ].getName().equals( "rememberme" ) ) { 77 String [] parts = cookies[ i ].getValue().split( ":" ); 78 79 AuthPolicy policy = ( AuthPolicy )StrutsUtil.getPolicy( mapping ); 83 84 user = policy.newUser(); 85 86 if ( user.loadForLogin( parts[ 0 ] ) ) { 87 88 if ( user.getPublicSecret().equals( parts[ 1 ] ) ) { 89 90 if ( !policy.autoLogin( user, request, form ) ) { 94 95 request.setAttribute( 99 AuthGlobals.URL_KEY, AuthUtil.getRelativeUrl( request ) ); 100 101 return StrutsUtil.findForward( mapping, "loginForm" ); 102 } 103 104 AuthUtil.setUser( request, user ); 108 109 return doExecute( mapping, form, request, response ); 113 } 114 } 115 } 116 } 117 } 118 119 StringBuffer url = request.getRequestURL(); 123 url.append( request.getQueryString() == null ? "" : "?" + request.getQueryString() ); 124 request.setAttribute( AuthGlobals.URL_KEY, url.toString() ); 125 126 return StrutsUtil.findForward( mapping, "loginForm" ); 130 } 131 132 return doExecute( mapping, form, request, response ); 136 } 137 138 141 protected abstract ActionForward doExecute( 142 ActionMapping mapping, 143 ActionForm form, 144 HttpServletRequest request, 145 HttpServletResponse response ) 146 throws 147 Exception ; 148 149 151 } 153 | Popular Tags |