spring - Spring Security中如何获取GET参数?

标签 spring authentication parameters get spring-security

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

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

如何从 URL 获取 redir 参数?

我尝试了几件事,包括重写 SimpleUrlAuthenticationSuccessHandler 和调用 request.getParameter("redir") 但它返回 null。我还尝试实现自己的过滤器 UsernamePasswordAuthenticationFilter 并在 attemptsAuthentication 上调用 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>

谢谢

最佳答案

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

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

关于spring - Spring Security中如何获取GET参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11854771/

相关文章:

java - 如何在 Spring-Boot 的生产过程中覆盖 application.properties?

java - Guice:请求实例,而不是类型

authentication - Laravel 5 db :seed the username and password, 但未在登录时进行身份验证

ruby-on-rails - Rails 中可能出现 HTTP 基本自定义错误?

function - 在 golang 中,使用 net/http 时如何调用带和不带尾随括号的函数

java - Spring Web App - 简单的 JDBC 模板建议?

使用 '*' 通配符的 Spring CORS 映射不起作用

c# - ASP.NET IIS 用户角色认证

php - Wordpress:如何将 url GET 参数添加到我的主菜单项

regex - JMeter:如何知道为什么我在 JMeter 中的正则表达式提取器没有提取数据