Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.undertow.servlet.handlers.security;
  
 
 import java.util.Map;

Servlet handler for FORM authentication. Instead of using a redirect it serves up error and login pages immediately using a forward

Author(s):
Stuart Douglas
 
 
     private static final String SESSION_KEY = "io.undertow.servlet.form.auth.redirect.location";
 
     public static final Factory FACTORY = new Factory();
 
     @Deprecated
     public ServletFormAuthenticationMechanism(final String namefinal String loginPagefinal String errorPage) {
         super(nameloginPageerrorPage);
     }
 
     @Deprecated
     public ServletFormAuthenticationMechanism(final String namefinal String loginPagefinal String errorPagefinal String postLocation) {
         super(nameloginPageerrorPagepostLocation);
     }
 
     public ServletFormAuthenticationMechanism(FormParserFactory formParserFactoryString nameString loginPageString errorPageString postLocation) {
         super(formParserFactorynameloginPageerrorPagepostLocation);
     }
 
     public ServletFormAuthenticationMechanism(FormParserFactory formParserFactoryString nameString loginPageString errorPage) {
         super(formParserFactorynameloginPageerrorPage);
     }
 
     @Override
     protected Integer servePage(final HttpServerExchange exchangefinal String location) {
         final ServletRequestContext servletRequestContext = exchange.getAttachment(.);
         ServletRequest req = servletRequestContext.getServletRequest();
         ServletResponse resp = servletRequestContext.getServletResponse();
         RequestDispatcher disp = req.getRequestDispatcher(location);
         //make sure the login page is never cached
         exchange.getResponseHeaders().add(."no-cache, no-store, must-revalidate");
         exchange.getResponseHeaders().add(."no-cache");
         exchange.getResponseHeaders().add(."0");
 
 
         try {
             disp.forward(reqresp);
         } catch (ServletException e) {
             throw new RuntimeException(e);
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
         return null;
     }
 
     @Override
     protected void storeInitialLocation(final HttpServerExchange exchange) {
         final ServletRequestContext servletRequestContext = exchange.getAttachment(.);
         HttpSessionImpl httpSession = servletRequestContext.getCurrentServletContext().getSession(exchangetrue);
         Session session;
         if (System.getSecurityManager() == null) {
             session = httpSession.getSession();
         } else {
             session = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(httpSession));
         }
         session.setAttribute(, RedirectBuilder.redirect(exchangeexchange.getRelativePath()));
         SavedRequest.trySaveRequest(exchange);
     }
 
     @Override
     protected void handleRedirectBack(final HttpServerExchange exchange) {
         final ServletRequestContext servletRequestContext = exchange.getAttachment(.);
         HttpServletResponse resp = (HttpServletResponseservletRequestContext.getServletResponse();
         HttpSessionImpl httpSession = servletRequestContext.getCurrentServletContext().getSession(exchangefalse);
         if (httpSession != null) {
             Session session;
             if (System.getSecurityManager() == null) {
                 session = httpSession.getSession();
             } else {
                session = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(httpSession));
            }
            String path = (Stringsession.getAttribute();
            if (path != null) {
                try {
                    resp.sendRedirect(path);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public static class Factory implements AuthenticationMechanismFactory {
        @Override
        public AuthenticationMechanism create(String mechanismNameFormParserFactory formParserFactoryMap<StringStringproperties) {
            return new ServletFormAuthenticationMechanism(formParserFactorymechanismNameproperties.get(), properties.get());
        }
    }
New to GrepCode? Check out our FAQ X