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

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

Introduction

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

Prototype

RedirectStrategy

Source Link

Usage

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.//  w  w  w. j a  v  a2 s  . co 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.terasoluna.gfw.security.web.redirect.RedirectAuthenticationHandlerTest.java

@Test
public void testOnAuthenticationSuccess_SetCustomeRedirectToRedirectStrategy() throws Exception {
    RedirectAuthenticationHandler redireHandler = new RedirectAuthenticationHandler();
    redireHandler.setRedirectToRedirectStrategy(new RedirectStrategy() {
        @Override//from  ww w  . j ava2  s . c  om
        public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url)
                throws IOException {
            response.sendRedirect("http://google.com");
        }
    });
    redireHandler.afterPropertiesSet();

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

    // expected data
    String expectedRedirectURL = "http://google.com";

    // run
    redireHandler.onAuthenticationSuccess(request, response, auth);

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