【问题标题】:custom spring security form自定义弹簧安全表单
【发布时间】:2011-06-30 16:29:24
【问题描述】:

我想在 Spring Security 表单中添加验证码,我该如何实现? 我已经声明了我的自定义表单登录,例如:<form-login login-page="/login" /> 现在我需要覆盖身份验证过滤器来验证验证码,该怎么做?

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    我猜你可以重写attemptAuthentication方法如下(伪代码):

    1. get captcha response

    2. verify captcha response

    3. if invalid response, throw some kind of AuthenticationException (may be named as CaptchaFailedException) that you can check in your custom AuthenticationFailureHandler

    4. if valid response, call super.attemptAuthentication(request,response)

    【讨论】:

    • 默认的认证失败处理器(SimpleUrlAuthenticationFailureHandler),将session中的异常保存为session.setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, exception);如果您正在使用这个默认类(或扩展它),那么您可以在 JSP 中使用会话中的该异常并显示适当的消息。另请参阅 forum.springsource.org/showthread.php?t=91833
    【解决方案2】:

    @misha,看看这篇文章:Spring Security 3: Integrating reCAPTCHA Service

    这使用两个过滤器来使 reCAPTCHA 集成尽可能无缝且不显眼。这意味着您现有的 Spring Security 实现不会中断。无需触及现有的课程。

    无需覆盖您现有的实现。您可以将 CAPTCHA 添加到您现有的实施中。

    【讨论】:

    • 嗯,它是线程安全的吗?因为我既没有看到 ThreadLocal 存储,也没有看到作用域或其他类似请求的作用域。但无论如何,文章很棒。它向我展示了另一种实现验证码的方法。我做的略有不同。我已将 DaoAuthenticationProvider 子类化并将验证码放在那里。
    猜你喜欢
    • 2013-10-15
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2023-01-03
    • 2015-12-06
    • 2018-09-08
    • 1970-01-01
    相关资源
    最近更新 更多