Spring Security - Retaining URL parameters on redirect to login

后端 未结 4 835
攒了一身酷
攒了一身酷 2020-12-08 20:23

ok.

lets say I have a secure url pattern

/secure/link-profile

optionally, there can be url paramaters appended.

/se         


        
4条回答
  •  独厮守ぢ
    2020-12-08 21:17

    Hi kabal -

    I have very similar requirements, and I followed yours and zagyi's and lion's post, but I still seem to loose the original request parameter on /login page.

    Here's what I have:

    public class AuthenticationProcessingFilterEntryPoint extends LoginUrlAuthenticationEntryPoint {
        @Override
        protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
            String url = super.determineUrlToUseForThisRequest(request, response, exception);
            return url + "?" + request.getQueryString();
        }
    }
    
    
    
    protected void configure(final HttpSecurity httpSecurity) throws Exception {
        httpSecurity.
        formLogin().loginPage("/signIn").permitAll().
            and().
                authorizeRequests().
                antMatchers(managementContextPath + "/**").permitAll().
                anyRequest().authenticated().withObjectPostProcessor(objectPostProcessor).
            and().
                csrf().disable().
                contentTypeOptions().
                xssProtection().
                cacheControl().
                httpStrictTransportSecurity().
            and().
                requestCache().requestCache(new RedisRequestCache(savedRequestRedisTemplate())).
            and().
                sessionManagement().sessionAuthenticationStrategy(sessionAuthenticationStrategy).
            and().
                addFilter(new ExceptionTranslationFilter(new AuthenticationProcessingFilterEntryPoint()));
    }
    

    I can see that AuthenticationProcessingFilterEntryPoint is deployed, but it does not hit a breakpoint there.

    Based on the documentation, it appears that this will kick in only when there is AuthenticationException or AccessDeniedException. In the configuration above, I am not sure if the spring internally throws such exception when such happens.

    Additionally, I'd like to to preserve the query parameter on the landing page regardless authentication succeeds or not.

    I did add success and failure handler, but none would kick into action.

    protected void configure(final HttpSecurity httpSecurity) throws Exception {
        httpSecurity.
        formLogin().
            successHandler(new PropogateQueryStringAuthenticationSuccessHandlerImpl()).
            failureHandler(new SimpleUrlAuthenticationFailureHandlerImpl(new QueryStringPropagateRedirectStrategy())).
        and().
            authorizeRequests().
            antMatchers(managementContextPath + "/**").permitAll().
            anyRequest().authenticated().withObjectPostProcessor(objectPostProcessor).
        and().
            csrf().disable().
            contentTypeOptions().
            xssProtection().
            cacheControl().
            httpStrictTransportSecurity().
        and().
            requestCache().requestCache(new RedisRequestCache(savedRequestRedisTemplate())).
         and().
            sessionManagement().sessionAuthenticationStrategy(sessionAuthenticationStrategy);
    }
    

    I am using spring-security 3.2.4.RELEASE on spring boot 1.1.6.RELEASE(which in turn uses Spring framework 4.0.7.RELEASE)

    Thanks, San

提交回复
热议问题