Example usage for org.springframework.security.web.authentication SavedRequestAwareAuthenticationSuccessHandler SavedRequestAwareAuthenticationSuccessHandler

List of usage examples for org.springframework.security.web.authentication SavedRequestAwareAuthenticationSuccessHandler SavedRequestAwareAuthenticationSuccessHandler

Introduction

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

Prototype

SavedRequestAwareAuthenticationSuccessHandler

Source Link

Usage

From source file:com.traffitruck.WebSecurityConfig.java

@Bean
public AuthenticationSuccessHandler successHandler() {
    SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler() {
        @Override/*  ww  w .j  av a2s  .  co  m*/
        protected void handle(HttpServletRequest request, HttpServletResponse response,
                Authentication authentication) throws IOException, ServletException {
            if (resetPasswordFlow(authentication.getAuthorities())) {
                try {
                    getRedirectStrategy().sendRedirect(request, response, "/resetPassword");
                    return;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            String url = Role.valueOf(authentication.getAuthorities().iterator().next().getAuthority())
                    .getLandingUrl();
            getRedirectStrategy().sendRedirect(request, response, url);
        }

        private boolean resetPasswordFlow(Collection<? extends GrantedAuthority> authorities) {
            for (GrantedAuthority grantedAuthority : authorities) {
                if (grantedAuthority.getAuthority().startsWith("resetPassword-"))
                    return true;
            }
            return false;
        }
    };
    return handler;
}

From source file:org.osiam.configuration.WebApplicationSecurity.java

@Override
protected void configure(HttpSecurity http) throws Exception {
    LoginDecisionFilter loginDecisionFilter = new LoginDecisionFilter();
    loginDecisionFilter.setAuthenticationManager(authenticationManagerBean());
    SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successHandler.setAlwaysUseDefaultTargetUrl(false);
    loginDecisionFilter.setAuthenticationSuccessHandler(successHandler);
    loginDecisionFilter//  www.java2  s  .  c om
            .setAuthenticationFailureHandler(new OsiamCachingAuthenticationFailureHandler("/login/error"));

    // @formatter:off
    http.requestMatchers().antMatchers("/login/**", "/error", "/oauth/**").and().authorizeRequests()
            .antMatchers("/login", "/login/error", "/error").permitAll().anyRequest().authenticated().and()
            .csrf()
            // TODO: This is a bad idea! We need CSRF at least for the `/oauth/authorize` endpoint
            // see also: https://github.com/spring-projects/spring-security-oauth/blob/2.0.8.RELEASE/samples/oauth2/sparklr/src/main/java/org/springframework/security/oauth/examples/sparklr/config/SecurityConfiguration.java#L48
            .disable().exceptionHandling().accessDeniedPage("/login/error").and().sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and().formLogin()
            .loginProcessingUrl("/login/check").failureUrl("/login/error").loginPage("/login").and()
            .addFilterBefore(loginDecisionFilter, UsernamePasswordAuthenticationFilter.class);
    // @formatter:on
}

From source file:de.terrestris.shogun.security.ShogunAuthProcessingFilter.java

/**
 * On successful authentication by an Authentication Manager of Spring Security
 * we intercept with this method  and change the respone to include the ROLES of
 * the logged in user./*from   ww w  .  j  a va 2  s  .c  o m*/
 * This way we can react on the ROLES and redirect accordingly within the requesting login form (here login.js)
 *
 * @see WebContent/client/login.js
 */
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
        Authentication authResult) throws IOException, ServletException {
    SecurityContextHolder.getContext().setAuthentication(authResult);

    SavedRequestAwareAuthenticationSuccessHandler srh = new SavedRequestAwareAuthenticationSuccessHandler();
    this.setAuthenticationSuccessHandler(srh);
    srh.setRedirectStrategy(new RedirectStrategy() {
        @Override
        public void sendRedirect(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                String s) throws IOException {
            //do nothing, no redirect
        }
    });
    super.successfulAuthentication(request, response, authResult);

    // build a comma separated string of the ROLES
    String authorityText = StringUtils.join(authResult.getAuthorities(), ",");

    // write the servlet return object
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    Writer out = responseWrapper.getWriter();
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(out);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeBooleanField("success", true);
    jsonGenerator.writeStringField("name", authResult.getName());
    jsonGenerator.writeStringField("role", authorityText);
    jsonGenerator.writeEndObject();
    jsonGenerator.close();
}

From source file:org.socialsignin.springsocial.security.signin.SpringSocialSecurityAuthenticationFilter.java

@PostConstruct
public void init() {
    if (defaultAuthenticationSuccessUrl != null && !defaultAuthenticationSuccessUrl.isEmpty()) {
        SavedRequestAwareAuthenticationSuccessHandler savedRequestAwareAuthenticationSuccessHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        savedRequestAwareAuthenticationSuccessHandler.setDefaultTargetUrl(defaultAuthenticationSuccessUrl);
        setAuthenticationSuccessHandler(savedRequestAwareAuthenticationSuccessHandler);
    }/*from   ww  w.j a v a2 s  .co m*/
}

From source file:com.iservport.auth.SecurityWebConfig.java

@Bean
public SavedRequestAwareAuthenticationSuccessHandler savedRequestAwareAuthenticationSuccessHandler() {

    SavedRequestAwareAuthenticationSuccessHandler auth = new SavedRequestAwareAuthenticationSuccessHandler();
    auth.setTargetUrlParameter("targetUrl");
    return auth;/*from w ww  . j a  v a  2  s  . c om*/
}

From source file:org.lightadmin.core.config.context.LightAdminSecurityConfiguration.java

@Bean
@Autowired/*from www  .  j a va  2  s.  co m*/
public Filter authenticationFilter(AuthenticationManager authenticationManager, RequestCache requestCache) {
    UsernamePasswordAuthenticationFilter authenticationFilter = new UsernamePasswordAuthenticationFilter();
    authenticationFilter.setFilterProcessesUrl(applicationUrl("/j_spring_security_check"));
    authenticationFilter.setAuthenticationManager(authenticationManager);
    SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successHandler.setRequestCache(requestCache);
    authenticationFilter.setAuthenticationSuccessHandler(successHandler);
    authenticationFilter.setAuthenticationFailureHandler(
            new SimpleUrlAuthenticationFailureHandler(applicationUrl("/login?login_error=1")));
    return authenticationFilter;
}

From source file:jp.pigumer.sso.WebSecurityConfig.java

@Bean
public SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler() {
    SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successRedirectHandler.setDefaultTargetUrl("/hello");
    return successRedirectHandler;
}

From source file:nu.localhost.tapestry5.springsecurity.services.SecurityModule.java

@Marker(SpringSecurityServices.class)
public static UsernamePasswordAuthenticationFilter buildRealAuthenticationProcessingFilter(
        @SpringSecurityServices final AuthenticationManager manager,
        @SpringSecurityServices final RememberMeServices rememberMeServices,
        @Inject @Value("${spring-security.check.url}") final String authUrl,
        @Inject @Value("${spring-security.target.url}") final String targetUrl,
        @Inject @Value("${spring-security.failure.url}") final String failureUrl,
        @Inject @Value("${spring-security.always.use.target.url}") final String alwaysUseTargetUrl)
        throws Exception {

    UsernamePasswordAuthenticationFilter filter = new UsernamePasswordAuthenticationFilter();
    filter.setAuthenticationManager(manager);

    filter.setPostOnly(false);/*ww  w .j a v a  2  s .  c  o m*/

    filter.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler(failureUrl));

    SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successHandler.setDefaultTargetUrl(targetUrl);
    successHandler.setAlwaysUseDefaultTargetUrl(Boolean.parseBoolean(alwaysUseTargetUrl));
    filter.setAuthenticationSuccessHandler(successHandler);
    filter.setFilterProcessesUrl(targetUrl);
    filter.setFilterProcessesUrl(authUrl);
    filter.setRememberMeServices(rememberMeServices);

    filter.afterPropertiesSet();
    return filter;
}

From source file:de.chludwig.websec.saml2sp.springconfig.SamlSpringSecurityConfig.java

@Bean
public SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler() {
    SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successRedirectHandler.setDefaultTargetUrl(START_PAGE_PATH);
    return successRedirectHandler;
}

From source file:ch.astina.hesperid.web.services.SecurityModule.java

/**
 * Checks credentials entered using a form.
 *//* w  w w .j av a 2s . co m*/
@Marker(SpringSecurityServices.class)
public static UsernamePasswordAuthenticationFilter buildRealAuthenticationProcessingFilter(
        @SpringSecurityServices final AuthenticationManager manager,
        @SpringSecurityServices final RememberMeServices rememberMeServices,
        @Inject @Value("${spring-security.check.url}") final String authUrl,
        @Inject @Value("${spring-security.target.url}") final String targetUrl,
        @Inject @Value("${spring-security.failure.url}") final String failureUrl,
        @Inject @Value("${spring-security.always.use.target.url}") final String alwaysUseTargetUrl)
        throws Exception {

    UsernamePasswordAuthenticationFilter filter = new UsernamePasswordAuthenticationFilter();
    filter.setAuthenticationManager(manager);

    filter.setPostOnly(false);

    filter.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler(failureUrl));

    SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successHandler.setDefaultTargetUrl(targetUrl);
    successHandler.setAlwaysUseDefaultTargetUrl(Boolean.parseBoolean(alwaysUseTargetUrl));
    filter.setAuthenticationSuccessHandler(successHandler);
    filter.setFilterProcessesUrl(targetUrl);
    filter.setFilterProcessesUrl(authUrl);
    filter.setRememberMeServices(rememberMeServices);

    filter.afterPropertiesSet();
    return filter;
}