FileDocCategorySizeDatePackage
LogonAction.javaAPI DocExample3032Wed Nov 03 12:00:48 GMT 2004com.oreilly.strutsckbk.ch11

LogonAction

public final class LogonAction extends org.apache.struts.webapp.example.BaseAction

Fields Summary
Constructors Summary
Methods Summary
public org.apache.struts.action.ActionForwardexecute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)


        HttpSession session = request.getSession();

        ActionErrors errors = new ActionErrors();

        String username = (String) PropertyUtils.getSimpleProperty(form, "username");
        String password = (String) PropertyUtils.getSimpleProperty(form, "password");
        boolean rememberMe = ((Boolean) PropertyUtils.getSimpleProperty(form, "rememberMe")).booleanValue();

        Credentials credentials = new Credentials(username, password);

        SecurityService securityService = getSecurityService(request);
        User user = securityService.authenticate(credentials);

        if (rememberMe) {
            saveCookies(response, username, password);
        } else {
            removeCookies(response);
        }
        
        session.setAttribute(Constants.USER_KEY, user);

        return mapping.findForward(Constants.SUCCESS);
    
private voidremoveCookies(javax.servlet.http.HttpServletResponse response)

        // expire the username cookie by setting maxAge to zero
        // (actual cookie value is irrelevant)
        Cookie unameCookie = new Cookie("StrutsCookbookUsername", "expired");
        unameCookie.setMaxAge(0);
        response.addCookie(unameCookie);

        // expire the password cookie by setting maxAge to zero
        // (actual cookie value is irrelevant)
        Cookie pwdCookie = new Cookie("StrutsCookbookPassword", "expired");
        pwdCookie.setMaxAge(0);
        response.addCookie(pwdCookie);
    
private voidsaveCookies(javax.servlet.http.HttpServletResponse response, java.lang.String username, java.lang.String password)

        Cookie usernameCookie = new Cookie("StrutsCookbookUsername", Base64Encoder.encode(username));
        usernameCookie.setMaxAge(60 * 60 * 24 * 30); // 30 day expiration
        response.addCookie(usernameCookie);
        Cookie passwordCookie = new Cookie("StrutsCookbookPassword", Base64Encoder.encode(password));
        passwordCookie.setMaxAge(60 * 60 * 24 * 30); // 30 day expiration
        response.addCookie(passwordCookie);