How to redirect from servlet with PrettyFaces ?

Splash Forums PrettyFaces Users How to redirect from servlet with PrettyFaces ?

Tagged: ,

This topic contains 2 replies, has 2 voices, and was last updated by  mika 5 years, 10 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #18106

    mika
    Participant

    i was wondering how to redirect from a Servlet (filter) with PrettyFaces, i need a sample code please ?

    #21754

    Make sure the Filter is ordered after PrettyFaces. Then use the HttpServletResponse.sendRedirect(...) method.

    #21755

    mika
    Participant

    hi Lincoln, it’s the spring security filter, and it can’t be after prettyFaces AFAIK, here’s my configuration for it:

    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
    org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
    </filter>

    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <filter>
    <filter-name>Pretty Filter</filter-name>
    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
    <init-param>
    <param-name>logLevel</param-name>
    <param-value>ERROR</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <filter-name>Pretty Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    the loginSuccessHandler class which the user enter after successful login:

    @Service("loginSuccessHandler")
    public class LoginSuccessHandler extends
    SavedRequestAwareAuthenticationSuccessHandler {

    Log log = LogFactory.getLog(getClass());

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private MessageSource messageSource;

    private final RequestCache requestCache = new HttpSessionRequestCache();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
    HttpServletResponse response,
    org.springframework.security.core.Authentication authentication)
    throws IOException, ServletException {

    String userName = authentication.getName();
    Employee user = null;
    try {
    user = employeeService.findEmployeeByEmail(userName);
    } catch (Exception e) {
    e.printStackTrace();
    }

    String currentUserAttr = messageSource.getMessage(
    Constants.currentUserAttrKey, null, null);

    request.getSession().setAttribute(currentUserAttr, user);

    log.info("################### In LoginSuccessHandler");

    String targetUrl = getDefaultTargetUrl();

    log.info("################### defaultTargetUrl: " + targetUrl);

    SavedRequest savedRequest = requestCache.getRequest(request, response);

    log.info("################### contextPath: " + request.getContextPath());
    // if there's a saved request,let's check on that saved request
    // to see where that user is heading for and if he's authorized or not
    if (savedRequest != null) {
    targetUrl = savedRequest.getRedirectUrl();
    log.info("################### savedRequest: " + targetUrl);
    response.sendRedirect(targetUrl);
    return;
    } else {
    log.info("################### Redirecting to defaultTargetUrl");
    response.sendRedirect(request.getContextPath() + targetUrl);
    return;
    }

    }

    }

    if there’s a savedRequest, sometimes (not always) it redirects to the correct url, put displays a plain page ? any ideas ?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.