Spring Security - 在重定向到登录时保留URL参数

kab*_*bal 22 java spring spring-mvc spring-security

好.

假设我有一个安全的网址模式

/secure/link-profile
Run Code Online (Sandbox Code Playgroud)

可选地,可以附加url参数.

/secure/link-profile?firstName=Bob&secondName=Smith&membershipNumber=1234
Run Code Online (Sandbox Code Playgroud)

我怎样才能将这些url params转移到登录页面?

/login?firstName=Bob&secondName=Smith&membershipNumber=1234
Run Code Online (Sandbox Code Playgroud)

基本前提是我们提供与第三方的奖励整合,第三方会将用户发送给我们.他们将被带到一个页面,将他们的第三方帐户/个人资料与他们/我们的网站用户链接.但是,如果他们没有我们的现有帐户,那么在登录页面上,他们将进入注册页面,然后我们希望预先填充第三方传递给我们的一些细节.

提前致谢

spring security 2.0.7.RELEASE spring framework 3.1.1.RELEASE

zag*_*gyi 8

请参阅法buildRedirectUrlToLoginPage(HttpServletRequest request, ...)LoginUrlAuthenticationEntryPoint.

如果我正确理解了你想要实现的目标,我认为应该足以在子类中重写此方法,复制原始方法,但 urlBuilder.setQuery(request.getQueryString())在构建url时另外调用.

然后,您只需要ExceptionTranslationFilter使用此自定义入口点进行配置.


kab*_*bal 5

根据@zagyi 的回复,我只是在我现有的扩展中覆盖了一个方法 AuthenticationProcessingFilterEntryPoint

要覆盖的方法protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)是由调用buildRedirectUrlToLoginPage(..

@Override
protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) {
    String url = super.determineUrlToUseForThisRequest(request, response, exception);
    return url + "?" + request.getQueryString();
}
Run Code Online (Sandbox Code Playgroud)

显然,这可以改进为也使用各种构建器,以满足 url 上现有的查询字符串,但此时我知道我的登录 url 总是/login/,所以这对我的目的来说很好


San*_*San 5

一篇文章解释了覆盖如何不起作用。我需要覆盖开始。这可能是spring security以后版本引入的新东西。

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    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().
            exceptionHandling().authenticationEntryPoint(new AuthenticationProcessingFilterEntryPoint("/signIn"));
    }
}
Run Code Online (Sandbox Code Playgroud)
公共类 AuthenticationProcessingFilterEntryPoint 扩展 LoginUrlAuthenticationEntryPoint {
    公共 AuthenticationProcessingFilterEntryPoint(String loginFormUrl) {
        超级(登录表格网址);
    }

    @覆盖
    公共无效开始(HttpServletRequest 请求,HttpServletResponse 响应,AuthenticationException authException)抛出 IOException,ServletException {
        RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
        redirectStrategy.sendRedirect(request, response, getLoginFormUrl() + "?" + request.getQueryString());
    }
}

Rizier123,再次感谢您的指点。