首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Security -在重定向到登录时保留URL参数

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

Stack Overflow用户
提问于 2013-01-12 00:35:53
回答 3查看 18.2K关注 0票数 22

好的。

假设我有一个安全的url模式。

/secure/link-profile

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

/secure/link-profile?firstName=Bob&secondName=Smith&membershipNumber=1234

我如何才能使这些url参数被带到登录页面?

/login?firstName=Bob&secondName=Smith&membershipNumber=1234

基本前提是我们与第三方提供奖励集成,第三方会将他们的用户发送给我们。他们将被带到一个页面,将他们的第三方帐户/配置文件与他们/我们的网站用户链接起来。但是,如果他们没有我们的现有帐户,那么在登录页面上,他们将转到注册页面,然后我们希望预先填充第三方传递给我们的一些他们的详细信息。

提前感谢

spring security 2.0.7.RELEASE spring框架3.1.1.RELEASE

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-12 02:49:38

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

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

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

票数 9
EN

Stack Overflow用户

发布于 2013-01-12 03:50:25

根据@zagyi的响应,我只是重写了我现有的AuthenticationProcessingFilterEntryPoint扩展中的一个方法

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

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

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

票数 5
EN

Stack Overflow用户

发布于 2014-12-17 13:52:05

嗨,kabal -

我有非常相似的需求,我关注了你和zagyi的帖子和lion's的帖子,但我似乎仍然遗漏了/login页面上的原始请求参数。

这就是我所拥有的:

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()));
}

我可以看到部署了AuthenticationProcessingFilterEntryPoint,但它并没有命中断点。

基于documentation,这似乎只有在有AuthenticationException或AccessDeniedException时才会生效。在上面的配置中,我不确定当发生这种情况时,spring内部是否会抛出这样的异常。

此外,无论身份验证成功与否,我都希望保留登录页面上的查询参数。

我添加了success和failure处理程序,但它们都不起作用。

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);
}

我在spring boot 1.1.6.RELEASE上使用spring-security 3.2.4.RELEASE (它使用Spring框架4.0.7.RELEASE)

谢谢你,三

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14282569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档