Example usage for org.springframework.security.web DefaultRedirectStrategy DefaultRedirectStrategy

List of usage examples for org.springframework.security.web DefaultRedirectStrategy DefaultRedirectStrategy

Introduction

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

Prototype

DefaultRedirectStrategy

Source Link

Usage

From source file:it.scoppelletti.programmerpower.web.security.RedirectAccessDeniedHandler.java

/**
 * Costruttore.
 */
public RedirectAccessDeniedHandler() {
    myRedirectStrategy = new DefaultRedirectStrategy();
}

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  . j av  a 2s  .  co 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.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   ww 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: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.co m
 * @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

@Override
protected void configure(HttpSecurity http) throws Exception {
    SimpleUrlAuthenticationSuccessHandler simpleUrlAuthenticationSuccessHandler = new JudgeAuthenticationSuccessHandler(
            "/");
    simpleUrlAuthenticationSuccessHandler.setUseReferer(false);
    simpleUrlAuthenticationSuccessHandler.setTargetUrlParameter("url");
    DefaultRedirectStrategy defaultRedirectStrategy = new DefaultRedirectStrategy();

    simpleUrlAuthenticationSuccessHandler.setRedirectStrategy(defaultRedirectStrategy);

    SimpleUrlLogoutSuccessHandler simpleUrlLogoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
    simpleUrlLogoutSuccessHandler.setUseReferer(true);

    // @formatter:off
    http.authorizeRequests().antMatchers(ckfinder.getServlet().getPath()).hasAnyRole("ADMIN").and().csrf()
            .disable().exceptionHandling().authenticationEntryPoint(authenticationEntryPoint()).and()
            .formLogin().loginPage("/login").usernameParameter("user_id1").passwordParameter("password1")
            .successHandler(simpleUrlAuthenticationSuccessHandler).failureHandler(failureHandler()).permitAll()
            .and().headers().cacheControl().disable().httpStrictTransportSecurity().disable().frameOptions()
            .sameOrigin().and().logout().logoutUrl("/logout")
            .logoutSuccessHandler(simpleUrlLogoutSuccessHandler).permitAll().and().rememberMe()
            .rememberMeParameter("rememberMe").tokenRepository(persistentTokenRepository).and().requestCache()
            .requestCache(new NullRequestCache()).and().servletApi();
    // @formatter:on
}

From source file:org.terasoluna.gfw.security.web.redirect.RedirectAuthenticationHandlerTest.java

@Test
public void testOnAuthenticationSuccess_SetNullRedirectToRedirectStrategyAndSetContextRelative()
        throws Exception {
    RedirectAuthenticationHandler redireHandler = new RedirectAuthenticationHandler();
    DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    redirectStrategy.setContextRelative(true);
    redireHandler.setRedirectStrategy(redirectStrategy);

    // set Blank URI
    String redirectURI = "http://localhost/foo/bar";
    request.setParameter("redirectTo", redirectURI);

    // expected data
    String expectedRedirectURL = "bar";

    // run/*from   ww w  . j av  a2 s  .com*/
    redireHandler.onAuthenticationSuccess(request, response, auth);

    // assert
    assertThat(response.getRedirectedUrl(), is(expectedRedirectURL));
}

From source file:org.kmnet.com.fw.web.security.redirect.RedirectAuthenticationHandler.java

/**
 * If {@link RedirectStrategy} is not externally set, {@link DefaultRedirectStrategy} is used.
 * <p>//w w w .j av  a  2 s .co m
 * {@code contextRelative} property of DefaultRedirectStrategy is set to true.<br>
 * In order to set it to {@code false}, {@code targetUrlParameterRedirectStrategy} property <br>
 * must be set in the bean definition <br>
 * 
 * </p>
 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
 */
@Override
public void afterPropertiesSet() {
    if (targetUrlParameterRedirectStrategy == null) {
        // set contextRelative=true as default RedirectStrategy
        targetUrlParameterRedirectStrategy = new DefaultRedirectStrategy();
        ((DefaultRedirectStrategy) targetUrlParameterRedirectStrategy).setContextRelative(true);
    }
}

From source file:org.apache.rave.portal.web.controller.handler.OpenIDAuthenticationFailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {
    if (exception instanceof UsernameNotFoundException
            && exception.getAuthentication() instanceof OpenIDAuthenticationToken
            && ((OpenIDAuthenticationToken) exception.getAuthentication()).getStatus()
                    .equals(OpenIDAuthenticationStatus.SUCCESS)) {

        OpenIDAuthenticationToken token = (OpenIDAuthenticationToken) exception.getAuthentication();
        String url = token.getIdentityUrl();
        User user = createTemporaryUser(token, url);
        request.getSession(true).setAttribute(ModelKeys.NEW_USER, user);

        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
        log.info("Redirecting to new user account creation page");
        super.setRedirectStrategy(redirectStrategy);
        redirectStrategy.sendRedirect(request, response, "/" + ViewNames.CREATE_ACCOUNT_PAGE);
        return;//  ww  w .j  a  v a2 s.c  o  m
    } else {
        super.onAuthenticationFailure(request, response, exception);
    }
}

From source file:org.springframework.security.oauth.provider.filter.UserAuthorizationSuccessfulAuthenticationHandler.java

public UserAuthorizationSuccessfulAuthenticationHandler(String s) {
    super(s);
    setRedirectStrategy(new DefaultRedirectStrategy());
}

From source file:org.springframework.security.web.session.SimpleRedirectSessionInformationExpiredStrategy.java

public SimpleRedirectSessionInformationExpiredStrategy(String invalidSessionUrl) {
    this(invalidSessionUrl, new DefaultRedirectStrategy());
}