【问题标题】:How to get GET parameter in Spring Security?如何在 Spring Security 中获取 GET 参数?
【发布时间】:2012-08-04 23:42:20
【问题描述】:

我正在使用 Spring Security 3.1,我想在用户登录之前获取来自 URL 的参数。所以我希望访问我的登录页面的用户向我发送一个 redir 参数(包含他在通过身份验证后想要访问的 URL)。当我请求页面时,而不是在我尝试提交表单以登录时。

例如:localhost/myApp?redir=my.app.com/custom

如何从 URL 获取 redir 参数?

我已经尝试了几件事,包括覆盖 SimpleUrlAuthenticationSuccessHandler 和调用 request.getParameter("redir") 但它返回 null。我还尝试实现自己的过滤器 UsernamePasswordAuthenticationFilter 并在尝试身份验证时调用 getParameter(),但返回 null。

更新以获取更多信息: 我的登录页面非常简单,基本上是:一个通过 POST 方法调用 /j_spring_security_check 的表单(带有 j_username 和 j_password 参数)。

我的身份验证提供程序实现了 AuthenticationManager,因此在用户通过身份验证后,将返回 Authentication 对象以及授权列表、用户、密码。

我还实现了 SimpleUrlAuthenticationSuccessHandler 并且 onAuthenticationSuccess() 方法将检查他的权限,如果经过身份验证和许可,用户将被重定向到他在 URL 上通知的页面(现在,当我尝试在此调用 request.getParemeter("redir")方法,我得到 null。

public class GetURLFilter extends UsernamePasswordAuthenticationFilter{
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException {

    String paginaRedirecionar = request.getParameter("redir");
    request.getSession().setAttribute("redirecionamento", paginaRedirecionar);

    return super.attemptAuthentication(request, response);
}
}

<bean id="authenticationFilter"
    class="com.uolinc.adm.security.GetURLFilter"
    p:authenticationManager-ref="radiusAuthenticationManager"
    p:authenticationFailureHandler-ref="radiusAuthenticationFailureHandler"
    p:authenticationSuccessHandler-ref="radiusAuthenticationSuccessHandler" />

<security:http auto-config="false"
    use-expressions="true"
    access-denied-page="/auth/denied" entry-point-ref="authenticationEntryPoint"
    disable-url-rewriting="true"
    access-decision-manager-ref="accessDecisionManager">

    <security:logout logout-url="/auth/logout" logout-success-url="/auth/login" />

    <security:custom-filter position="FORM_LOGIN_FILTER" ref="authenticationFilter" />
</security:http>

谢谢

【问题讨论】:

  • 您所描述的是正确的方法,所以无论您尝试什么,您都做错了。如果您在问题中添加一些信息(例如您的 spring 安全配置和登录页面/控制器),也许我可以帮助您解决问题。
  • 参数是在请求登录页面时发送的,还是在提交时发送的?
  • 感谢爸爸的回答,我已经添加了一些信息(很多,所以我尝试了最重要的)
  • 卢克,它是在登录页面(GET)请求期间发送的,当时我点击了该页面的输入,所以在我尝试进行身份验证之前。但我只想要请求中参数的值,如果有任何其他方法可以获得它。

标签: spring login parameters get spring-security


【解决方案1】:

UsernamePasswordAuthenticationFilterSimpleUrlAuthenticationSuccessHandler 在登录表单的提交 期间被调用,这是与呈现表单的请求不同的请求。因此,在您提交表单时,不会出现在请求表单期间出现的参数。您需要在会话中缓存该参数或将其作为隐藏参数添加到登录表单中,以便使用正常登录参数重新提交(以便您的AuthenticationSuccessHandler 可以使用它)。

请注意,以这种方式使用客户端提供的数据进行重定向是有风险的,除非您进行了某种验证(例如,检查 URL 是否在您的应用程序中)。否则,攻击者可以提供链接到恶意站点的 URL,并可能在用户不知情的情况下劫持经过身份验证的会话或在安全站点上执行操作。

【讨论】:

  • 我要做的是实现 UsernamePasswordAuthenticationFilter 并将获取请求参数的逻辑放在 doFilter() 方法中,而不是 attemptAuthentication() 中。 doFilter() 在身份验证之前和之后都被调用,而从顾名思义,attemptAuthentication 只在之后调用。这成功了,我得到了参数并插入到会话中。谢谢大家!
猜你喜欢
  • 2018-12-16
  • 2016-04-14
  • 2012-04-30
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多