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

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

Introduction

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

Prototype

public SimpleUrlAuthenticationSuccessHandler() 

Source Link

Usage

From source file:scratch.cucumber.example.SecurityConfiguration.java

@Override
protected void configure(HttpSecurity http) throws Exception {

    // The http.formLogin().defaultSuccessUrl("/path/") method is required when using stateless Spring Security
    // because the session cannot be used to redirect to the page that was requested while signed out. Unfortunately
    // using this configuration method will cause our custom success handler (below) to be overridden with the
    // default success handler. So to replicate the defaultSuccessUrl("/path/") configuration we will instead
    // correctly configure and delegate to the default success handler.
    final SimpleUrlAuthenticationSuccessHandler delegate = new SimpleUrlAuthenticationSuccessHandler();
    delegate.setDefaultTargetUrl("/spring/");

    // Make Spring Security stateless. This means no session will be created by Spring Security, nor will it use any
    // previously existing session.
    http.sessionManagement().sessionCreationPolicy(STATELESS);
    // Disable the CSRF prevention because it requires the session, which of course is not available in a
    // stateless application. It also greatly complicates the requirements for the sign in POST request.
    http.csrf().disable();//  w  ww.  jav  a  2s.  c  o m
    // Viewing any page requires authentication.
    http.authorizeRequests().anyRequest().authenticated();
    http.formLogin()
            // Viewing the sign in page does not require authentication.
            .loginPage("/spring/signIn").permitAll()
            // Override the sign in success handler with our stateless implementation. This will update the response
            // with any headers and cookies that are required for subsequent authenticated requests.
            .successHandler(new StatelessAuthenticationSuccessHandler(authenticationBinder, delegate));
    http.logout().logoutUrl("/spring/signOut").logoutSuccessUrl("/spring/");
    // Add our stateless authentication filter before the default sign in filter. The default sign in filter is
    // still used for the initial sign in, but if a user is authenticated we need to acknowledge this before it is
    // reached.
    http.addFilterBefore(new StatelessAuthenticationFilter(authenticationBinder, securityContextHolder),
            UsernamePasswordAuthenticationFilter.class);
}

From source file:architecture.user.spring.config.SecurityConfig.java

protected AuthenticationSuccessHandler authenticationSuccessHandler() {
    SimpleUrlAuthenticationSuccessHandler authenticationSuccessHandler = new SimpleUrlAuthenticationSuccessHandler() {
        @Override/*from  w  ww.  j a  v a  2s  .c o m*/
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                Authentication authentication) throws IOException, ServletException {
            OutputFormat output = getOutputFormat(request, response);
            if (output == OutputFormat.JSON) {
                // Token
                String referer = request.getHeader("Referer");
                Map model = new ModelMap();
                Map<String, String> item = new java.util.HashMap<String, String>();
                item.put("success", "true");
                if (StringUtils.isNotEmpty(referer))
                    item.put("referer", referer);
                /*
                 * model.put("item", item);
                 * request.setAttribute(WebApplicatioinConstants.
                 * MODEL_ATTRIBUTE, model); if(output == OutputFormat.JSON
                 * ){ JsonView view = new JsonView();
                 * view.setModelKey("item"); try { view.render(model,
                 * request, response); } catch (Exception e) { } return; }
                 */
            }
            super.onAuthenticationSuccess(request, response, authentication);
        }

        protected OutputFormat getOutputFormat(HttpServletRequest httpservletrequest,
                HttpServletResponse httpservletresponse) {
            String temp = httpservletrequest.getParameter("output");
            String formatString = StringUtils.defaultString(temp, "html");
            OutputFormat format = OutputFormat.stingToOutputFormat(formatString);
            return format;
        }
    };
    return authenticationSuccessHandler;
}

From source file:org.esupportail.publisher.config.SecurityConfiguration.java

@Bean
public SimpleUrlAuthenticationSuccessHandler authenticationSuccessHandler() {
    SimpleUrlAuthenticationSuccessHandler authenticationSuccessHandler = new SimpleUrlAuthenticationSuccessHandler();
    authenticationSuccessHandler.setDefaultTargetUrl("/");
    authenticationSuccessHandler.setTargetUrlParameter(getCasTargetUrlParameter());
    return authenticationSuccessHandler;
}