Example usage for org.springframework.security.web RedirectStrategy sendRedirect

List of usage examples for org.springframework.security.web RedirectStrategy sendRedirect

Introduction

In this page you can find the example usage for org.springframework.security.web RedirectStrategy sendRedirect.

Prototype

void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException;

Source Link

Document

Performs a redirect to the supplied URL

Usage

From source file:com.mycompany.apps.web.security.LogoutHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    logger.info("onLogoutSuccess");

    RedirectStrategy redirectStrategy = getRedirectStrategy();
    redirectStrategy.sendRedirect(request, response, "/login");
}

From source file:com.mycompany.apps.web.security.FailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {
    logger.info("onAuthenticationFailure");

    String errorId = "unknown";
    if (exception instanceof BadCredentialsException) {
        //errorId = LoginViewController.BAD_CREDENTIAL;
    }//www .j ava  2  s.  com

    RedirectStrategy redirectStrategy = getRedirectStrategy();
    redirectStrategy.sendRedirect(request, response, "/login/" + errorId);
}

From source file:net.triptech.buildulator.service.OpenIdAuthenticationFailureHandler.java

/**
 * Called when an authentication attempt fails.
 *
 * @param request - the request during which the authentication attempt occurred.
 * @param response - the response./*from  w  w  w  . j a v a 2s.  c  om*/
 * @param exception - the exception which was thrown to reject the authentication
 * request.
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authenticationException) throws IOException, ServletException {

    if (authenticationException instanceof DisabledException) {
        RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
        redirectStrategy.sendRedirect(request, response, "/accountDisabled");
    }

    if (isFailedDueToUserNotRegistered(authenticationException)) {

        OpenIDAuthenticationToken token = (OpenIDAuthenticationToken) authenticationException
                .getAuthentication();

        Person person = Person.findByOpenIdIdentifier(token.getIdentityUrl());

        if (person == null) {

            // The person does not exist, create
            person = createPerson(token);

            // Recreate OpenIDAuthentication token, transfer values from existing
            // token, and assign roles from retrieved user. Since grantedAuthorities
            // is unmodifiable list and no way to update the pre created token.

            OpenIDAuthenticationToken newToken = new OpenIDAuthenticationToken(person, person.getAuthorities(),
                    token.getIdentityUrl(), token.getAttributes());
            newToken.setAuthenticated(true);

            token.setDetails(person);
            SecurityContextHolder.getContext().setAuthentication(newToken);

            // Transfer any previous projects to the new user
            transferProjects(request, person);

            RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
            redirectStrategy.sendRedirect(request, response, "/user");
        }
    }
}

From source file:sequrity.SecurityConfig.java

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/faces/javax.faces.resource/**").permitAll().anyRequest()
            .authenticated().and().formLogin().loginPage("/faces/prijava.xhtml").permitAll()
            .failureUrl("/faces/prijava.xhtml?error").loginProcessingUrl("/perform_login")
            .successHandler(new AuthenticationSuccessHandler() {

                @Override//from w w  w.  ja v  a2s. c  o  m
                public void onAuthenticationSuccess(HttpServletRequest hsr, HttpServletResponse hsr1,
                        Authentication a) throws IOException, ServletException {
                    RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
                    if (a.getAuthorities().contains(new SimpleGrantedAuthority(LoginService.ROLE_PROFESOR))) {
                        redirectStrategy.sendRedirect(hsr, hsr1, "/faces/schedule_profesor.xhtml");
                    } else {
                        redirectStrategy.sendRedirect(hsr, hsr1, "/faces/schedule.xhtml");
                    }
                }
            })
            //                .defaultSuccessUrl("/faces/schedule.xhtml", true)
            .usernameParameter("username").passwordParameter("password").and().logout()
            .logoutSuccessUrl("/faces/prijava.xhtml");

}

From source file:net.triptech.metahive.service.OpenIdAuthenticationFailureHandler.java

/**
 * Called when an authentication attempt fails.
 *
 * @param request - the request during which the authentication attempt occurred.
 * @param response - the response.//  w ww  . j  a  va  2  s .  com
 * @param exception - the exception which was thrown to reject the authentication
 * request.
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authenticationException) throws IOException, ServletException {

    if (authenticationException instanceof DisabledException) {
        RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
        redirectStrategy.sendRedirect(request, response, "/accountDisabled");
    }

    if (isFailedDueToUserNotRegistered(authenticationException)) {

        OpenIDAuthenticationToken token = (OpenIDAuthenticationToken) authenticationException
                .getAuthentication();

        String id = token.getIdentityUrl();

        List<Person> people = Person.findPeopleByOpenIdIdentifier(id).getResultList();

        Person person = people.size() == 0 ? null : people.get(0);

        if (person == null) {

            // The person does not exist, create
            person = createPerson(token);

            // Recreate OpenIDAuthentication token, transfer values from existing
            // token, and assign roles from retrieved user. Since grantedAuthorities
            // is unmodifiable list and no way to update the pre created token.

            OpenIDAuthenticationToken newToken = new OpenIDAuthenticationToken(person, person.getAuthorities(),
                    token.getIdentityUrl(), token.getAttributes());
            newToken.setAuthenticated(true);

            token.setDetails(person);
            SecurityContextHolder.getContext().setAuthentication(newToken);

            RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
            redirectStrategy.sendRedirect(request, response, "/user");
        }
    }
}

From source file:cn.edu.zjnu.acm.judge.config.SecurityConfiguration.java

private AuthenticationFailureHandler failureHandler() {
    String defaultFailureUrl = "/login?error";
    RedirectStrategy redirectStrategy = new FailureRedirectStrategy();
    return (request, response, exception) -> {
        saveLoginLog(request, false);//from  w  ww . j  a va  2s.com
        redirectStrategy.sendRedirect(request, response, defaultFailureUrl);
    };
}