1

http://code.google.com/p/kaptcha/を使用しようとしていますが、これは CAPTCHA を含める非常に簡単な方法のようです。私のデモアプリは JSFです。JSPの手順は簡単ですが、JSF での使用方法がわかりません。これを JSF で翻訳するにはどうすればよいですか?

送信アクションを管理するコードで:

String kaptchaExpected = (String)request.getSession() .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); String kaptchaReceived = request.getParameter("kaptcha");

if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) { setError("kaptcha", "Invalid validation code."); }

私はそれを私の中に入れてみました:

public String button1_action() {
    // TODO: Process the action. 
    return "success";
}

しかし、それはリクエストオブジェクトを理解していません:(

4

3 に答える 3

1

この同等の JSF アクションはそれを行う必要があります。

  // bind to <h:inputText value="#{thisbean.kaptchaReceived}" />
  private String kaptchaReceived;

  public String getKaptchaReceived() {
    return kaptchaReceived;
  }

  public void setKaptchaReceived(String kaptcha) {
    kaptchaReceived = kaptcha;
  }

  public String button1_action() {
    if (kaptchaReceived != null) {
      FacesContext context = FacesContext
          .getCurrentInstance();
      ExternalContext ext = context.getExternalContext();
      Map<String, Object> session = ext.getSessionMap();
      String kaptchaExpected = session
          .get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
      if (kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) {
        return "success";
      }
    }
    return "problem";
  }

これは、JSF ビューで HTML 要素の代わりにh:inputTexth:graphicImageを使用することを前提としています。

于 2009-04-27T10:53:35.537 に答える
1

バリデーターの実装は、カプチャを検証するもう 1 つの簡単な方法です。

<h:inputText id="kaptcha" autocomplete="off" required="true">
     <f:validator validatorId="kaptchaValidator" />
</h:inputText>
<h:message for="kaptcha" styleClass="errorMessage"/>

--- バリデータ ---

public class KaptchaValidator implements Validator {

@Override
public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {

HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);

        String kaptchaExpected = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

        String kaptchaReceived = (String) value;

        if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) {
            FacesMessage message = new FacesMessage();

            message.setDetail("Invalid Security Code.");
            message.setSummary("Invalid security code.");
            message.setSeverity(FacesMessage.SEVERITY_INFO);

            throw new ValidatorException(message);
        }
    }

于 2011-01-12T05:30:24.530 に答える
0

次のコードを使用して、FacesContext からアクセスできる JSF 外部コンテキストから要求オブジェクトを取得できます。

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();



編集( McDowellに感謝):

別の方法は、FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()メソッドを使用してリクエストパラメーターにアクセスすることです...

于 2009-04-27T06:15:44.033 に答える