1

Wildfly 8.2 と PostgreSQL を使用する JBoss セキュリティ レルムと組み合わせて、PrimeFaces 5.2 とフォーム ベースのログインを使用する JSF 2.2 アプリケーションがあります。

これは正常に機能しており、完全に期待どおりです。問題は、PrimeFaces Mobileで上記のセットアップを使用することです。ユーザー名とパスワードを入力してログイン ボタンをクリックした後、要求された元のページまたはエラー ページにリダイレクトされる代わりに、ログイン ビューに再度リダイレクトされるため、何も起こりません。

私のモバイルログインフォームから始めましょう:

<ui:define name="content">
    <pm:content styleClass="content">
        <h:form id="loginForm" method="POST" prependId="false"
                onsubmit="document.getElementById('loginForm').action = 'j_security_check';">
                <p:focus for="j_username"/>
            <pm:field>
            <p:outputLabel value="Benutzername"></p:outputLabel>
           <h:inputText id="j_username" name="j_username" required="true" />
        </pm:field>
        <pm:field>
            <p:outputLabel value="Passwort"></p:outputLabel>
            <p:password id="j_password" name="j_password" redisplay="false" required="true" />
        </pm:field>
        <pm:field>
            <p:commandButton id="login" value="Login" ajax="false" />
        </pm:field>
        </h:form>
    </pm:content>
</ui:define>

前述のように、フォーム定義はモバイル以外のバージョンでも機能します。モバイル バージョンの場合は、タグpm_contentpm:field.

生成された DOM を Chrome で調べると、レンダリングされたモバイル バージョンのページには、非モバイル バージョンとは異なるフォームおよび入力要素の ID があり、onsubmit欠落していることがわかります。

<form id="j_idt6:loginForm" name="j_idt6:loginForm" method="post" action="/MyApp/login.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt6:loginForm" value="j_idt6:loginForm">
<span id="j_idt6:j_idt32"></span><script type="text/javascript">$(function(){PrimeFaces.focus('j_idt6:username');});</script><div class="ui-field-contain"><label id="j_idt6:j_idt16" class="ui-outputlabel ui-widget">Benutzername</label><div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset"><input id="j_idt6:username" type="text" name="j_idt6:username"></div></div><div class="ui-field-contain"><label id="j_idt6:j_idt18" class="ui-outputlabel ui-widget">Passwort</label><div id="j_idt6:password" class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset ui-input-has-clear"><input data-role="none" id="j_idt6:password" name="j_idt6:password" type="password"><a href="#" class="ui-input-clear ui-btn ui-icon-delete ui-btn-icon-notext ui-corner-all ui-input-clear-hidden"></a></div></div><div class="ui-field-contain"><button id="j_idt6:login" name="j_idt6:login" class="ui-btn ui-shadow ui-corner-all" onclick="" type="submit">Login</button></div><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-5673897088131963149:-3654042330506594383" autocomplete="off">
</form>

モバイル以外のログインから生成された出力:

<form id="loginForm" name="loginForm" method="post" action="/MyApp/login.xhtml" enctype="application/x-www-form-urlencoded" onsubmit="document.getElementById('loginForm').action = 'j_security_check';">
<input type="hidden" name="loginForm" value="loginForm">
<span id="j_idt11"></span><script type="text/javascript">$(function(){PrimeFaces.focus('j_username');});</script><input id="j_username" type="text" name="j_username"><input id="j_password" type="password" name="j_password" value=""><input id="login" type="submit" name="login" value="Login"><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="5464337132357101375:3961658655950415709" autocomplete="off">
</form>

私が正しく理解すれば、モバイル版で ID を先頭に追加することに問題があるようです。

j_security_checkこれを修正して PrimeFaces Mobile 5.2 にPOST を実装するにはどうすればよいですか?

4

1 に答える 1

0

@BalusC のヒントを使用して実用的なソリューションをいじることができたので、フォームは次のようになります。

<ui:define name="content">
        <pm:content styleClass="content">
            <h:form>
                <pm:field>
                    <h:outputLabel for="username" value="Username" />
                    <h:inputText id="username" value="#{authenticationBean.username}"
                                 required="true" />
                    <h:message for="username" />
                </pm:field>
                <pm:field>
                    <h:outputLabel for="password" value="Password" />
                    <h:inputSecret id="password" value="#{authenticationBean.password}"
                                   required="true" />
                    <h:message for="password" />
                </pm:field>
                <pm:field>
                    <h:commandButton value="Login" action="#{authenticationBean.login()}" />
                </pm:field>
            </h:form>
        </pm:content>
</ui:define>

@ViewScopedログインは、ログイン メソッド内のマネージド Bean AuthenticationBean 内でプログラム化されました。

public void login() {
        log.info("Login attempt");

        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

        try {
            request.login(username, password);
            log.info("Login successful");
            externalContext.redirect(originalURL);
        } catch (ServletException e) {
            // Handle unknown username/password in request.login().
            context.addMessage(null, new FacesMessage("Unknown login"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
于 2015-08-28T13:12:17.257 に答える