2013-01-11 54 views
21

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

可以说我有一个安全的URL模式

/secure/link-profile 

可选,可以有附加网址PARAMATERS。

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

我该如何使这些url参数传递到登录页面?

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

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

在此先感谢

春季安全2.0.7.RELEASE Spring框架3.1.1.RELEASE

+0

你可以使用弹簧安全3.1吗?作为Maksym –

+0

我建议使用与您的spring-framework相同版本的spring-security。 – tom

+0

@MaksymDemidas是的,我可能会升级到弹簧安全3.1。 – kabal

回答

7

LoginUrlAuthenticationEntryPoint方法buildRedirectUrlToLoginPage(HttpServletRequest request, ...)

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

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

+0

的好主意。我已经实现了'AuthenticationProcessingFilterEntryPoint' – kabal

+0

已经提供了我的解决方案作为答案,信贷给@zagyi – kabal

3

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

显然是所谓的可以改进也使用一个建造者的种类,适应现有的查询字符串的url,但在这个时候我知道我的登录网址总是/login/,所以这对我的目的很好

+0

非常感谢!用'?'制作你的代码的一个小变种检测,如果已经存在,然后 - >返回url +“&”+ ... –

0

kabal -

我有非常类似的要求,我跟着你的和zagyi的和lion's后,但我似乎仍然松散/登录页面上的原始请求参数。

这是我有:

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是否会引发这种异常。

此外,我想保留着陆页上的查询参数,无论身份验证是否成功。

我确实添加了成功和失败处理程序,但没有人会采取行动。

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

我使用的弹簧引导1.1.6.RELEASE(又使用Spring框架4.0.7.RELEASE)

谢谢,弹簧安全3.2.4.RELEASE 圣

+0

您是否添加了对'.authenticationEntryPoint(yourEntryPoint)'的调用?我的实现是与一个非常老的应用程序,这是使用XML配置,并弹簧安全2.0.7.RELEASE – kabal

+0

嘿@kabal是的我尝试了以下方式 - httpSecurity。 ())和formLogin().... authenticationEntryPoint(new AuthenticationProcessingFilterEntryPoint()) – San

+0

stackover-ers我刚刚发布了[解决方案](http://stackoverflow.com/a/27559666/3713971)为我工作春季安全3.2.4.RELEASE春季4.0.7 – San

2

不同的article说明覆盖如何不起作用。我需要覆盖commence。这可能是Spring安全更高版本中引入的新事物。

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")); 
    } 
} 
 
public class AuthenticationProcessingFilterEntryPoint extends LoginUrlAuthenticationEntryPoint { 
    public AuthenticationProcessingFilterEntryPoint(String loginFormUrl) { 
     super(loginFormUrl); 
    } 

    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { 
     RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
     redirectStrategy.sendRedirect(request, response, getLoginFormUrl() + "?" + request.getQueryString()); 
    } 
} 

Rizier123,为您的指针再次感谢。

+0

@All - 有人请指导在https://stackoverflow.com/questions/47079993/unable-to-redirect- back-in-spring-boot-jsp-from-the-salesforce-site? – Prateek

相关问题