当前位置: 首页 > 知识库问答 >
问题:

Spring Security 3.1.3请求查询字符串剥离

贺懿轩
2023-03-14

我正在使用Spring Security 3.1.3保护我的应用程序,并且我要求允许用户通过第三方应用程序中的链接登录。

但是,第三方应用程序中的链接将重定向到特定资源,而不是登录页面,用户希望访问的资源将被定义为查询字符串参数。因此,例如,链接的形式为:
//server.com/app/build/panel.jsp?resourceid='blah'

当用户单击此链接时,他们应该被带到我的Spring Security配置中定义的登录页面,如果经过身份验证,则应该被重定向到包含querystring参数的原始链接。querystring参数对用户应该如何进行身份验证没有影响,它只是一个资源ID。

现在,除了querystring之外,这一切都可以正常工作,它在进入请求处理流之前被Spring Security剥离。

这显示在Spring Security的调试输出中;

org.springframework.security.web.savedrequest.添加到会话的DefaultSavedRequest[http://server.com:8080/app/build/panel.jsp]

即,查询字符串未保存,并且已删除。

注意,我目前正在使用Ant匹配。我没有必要真的与querystring比赛。

在Spring Security的早期版本中,您似乎可以通过使用BeanPostProcessor来影响这种行为,正如本文所述,Spring Security-忽略请求参数规则的Url。但是方法DefaultFilterInvocationSecurityMetadataSource。setStripQueryStringFromUrls()已从Spring Security 3.1.3中删除。

如何配置Spring Security性,使其不从原始请求中删除querystring?这样,当用户在登录到原始URL后被重定向时,querystring参数将被保留?

非常感谢Howard

共有3个答案

袁宜
2023-03-14

有关类似问题的其他信息,请参阅链接:https://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html

提取:当应用程序部署在没有从这些值中剥离路径参数的容器中时,攻击者可能会将它们添加到请求的URL中,以导致模式匹配意外成功或失败。

然而,这种剥离旨在牢固地保护登录的模式匹配。这并不意味着查询参数不能从HTTP请求中获得,它们应该是。

淳于知
2023-03-14

基本上是成功处理程序

你可以看看这个例子:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
      .antMatchers("/login*")
      .permitAll()
      .anyRequest()
      .authenticated()
      .and()
      .formLogin()
      .successHandler(new RefererAuthenticationSuccessHandler());
}

更多信息:http://www.baeldung.com/spring-security-redirect-login

鲍向笛
2023-03-14

你可以从SuccessHandler获得它

SecurityConfiguration类

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
SuccessHandler getSuccessHandler;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()

    .antMatchers("/dashboard/**",               
            "/feedback/**"
            ).access("hasRole('ROLE_SYSTEM_ADMIN') or hasRole('ROLE_COMPANY_ADMIN')")

    .and().formLogin().loginPage("/login").successHandler(getSuccessHandler)
    .loginProcessingUrl("/login").usernameParameter("ssoId").passwordParameter("password")      
    .and().csrf()
    .and().exceptionHandling().accessDeniedPage("/Access_Denied")
    .and()
    .sessionManagement().invalidSessionUrl("/login").maximumSessions(1).expiredUrl("/login").and().sessionAuthenticationErrorUrl("/login").sessionFixation().migrateSession()
    .sessionCreationPolicy(SessionCreationPolicy.ALWAYS); //always, IF_REQUIRED,never ,stateless    

    http.logout()
    .logoutUrl("/logout")
    .logoutSuccessUrl("/login")
    .invalidateHttpSession(true)
    .permitAll();
}

 @Override
  public void configure(WebSecurity web) throws Exception {
    web
    .ignoring()     
    .antMatchers("/static/**")
    .antMatchers("/images/**");       
     }
}

SuccessHandler类

@Component
public class SuccessHandler implements AuthenticationSuccessHandler {


@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
    Authentication authentication) throws IOException, ServletException {

    HttpSession session = request.getSession();
    response.sendRedirect(request.getContextPath() + "/dashboard/index");
}
}
 类似资料:
  • 我对REST和谷歌云endpoint非常陌生。我遵循了App Engine上的Endpoints框架入门教程,并成功执行了教程中所述的API查询: curl--head"Content-Type: apps/json"--request POST--data'{"message":"hello world"}'http://localhost:8080/_ah/api/echo/v1/echo 但

  • 我有一个查询字符串,可以是: 编辑:参数值的长度可以大于2 有人知道在String.ReplaceAll([regex],[replace])中使用什么好的regex表达式吗?

  • 问题内容: 我有一个HTTP客户端(目前)的Node.js应用程序。所以我在做: 这似乎是完成此任务的一种好方法。但是,我有些沮丧,我必须执行此步骤。这应该由一个公共库封装,但是我还没有看到它存在于node的库中,而且我不确定哪个标准的npm包可以完成它。有没有一种合理使用的更好的方法? url.format方法节省了构建自己的URL的工作。但理想情况下,请求的级别也应高于此级别。 问题答案: 检

  • 问题内容: 进行AJAX GET请求时查询字符串的最大长度是多少?更具体地说,我正在使用图像进行跨域AJAX: 这里的查询字符串有什么限制? 问题答案: 在任何规范中都没有定义的限制。的 有效 或规定的极限是2048个字符。某些浏览器和Web服务器的使用时间会更长。

  • 问题内容: 是否有任何JavaScript库可以根据查询字符串,样式生成字典? 可以像这样使用的东西: “查询字符串”是否在.NET领域之外称为其他名称?为什么不分解为键/值集合? 编辑:我已经编写了自己的函数,但是任何主要的库都可以这样做吗? 问题答案: 是否有任何JavaScript库可以根据查询字符串,样式生成字典? 可以像这样使用的东西: “查询字符串”是否在.NET领域之外称为其他名称?

  • 我的目标是所有下面的URI都应该工作 https://rest/xyz?sort=name https://rest/xyz?排序=名称 https://rest/xyz?过滤器=名称=值 https://rest/xyz?Filter=name=value 为了实现这一点,我创建了自定义过滤器,该过滤器覆盖了传递给过滤器链的HttpServlet请求。以下是此方法的链接: http://foru