2016-08-15 6 views
0

Grails GSPでBootStrapを使用しています。コードの下にログインモーダルがあります。私はspring security pluginを使用してログインを処理しています。このモーダルに戻ってフラッシュメッセージを表示する方法入力されたユーザー名とパスワードが間違っています。BootStrapとGrails

ログインモーダルコード:

<sec:ifNotLoggedIn> 
    <li> <a href="#" data-toggle="modal" data-target="#login-modal" class="btn navbar-btn btn-white"><i class="fa fa-sign-in"></i>Log in</a> <%--<g:link controller="login" action="auth">Login</g:link></li>--%> 
     </sec:ifNotLoggedIn> 
<!-- *** LOGIN MODAL ***_________________________________________________________ 
--> 
<div id="login-modal" tabindex="-1" role="dialog" aria-labelledby="Login" aria-hidden="true" class="modal fade"> 
    <div class="modal-dialog modal-sm"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" data-dismiss="modal" aria-hidden="true" class="close">×</button> 
     <h4 id="Login" class="modal-title">Customer login</h4> 
     </div> 
     <div class="modal-body"> 
     <g:if test='${flash.message}'> 
     <div class="message" role="status"> ${flash.message}</div></g:if> 
     <form action='./j_spring_security_check' method='POST' id='loginForm' class='cssform' autocomplete='off'> 
      <div class="form-group"> 
      <%--<input id="email_modal" type="text" placeholder="email" class="form-control"> 
      --%><input type='text' class='form-control' name='j_username' id='username' placeholder="${message(code:'springSecurity.login.username.label') }"/> 
      </div> 
      <div class="form-group"> 
      <%--<input id="password_modal" type="password" placeholder="password" class="form-control"> 
      --%><input type='password' class='form-control' name='j_password' id='password' placeholder="${message(code:'springSecurity.login.password.label') }"/> 
      </div> 
      <p class="text-center"> 
      <button type="submit" id="submit" class="btn btn-primary"><i class="fa fa-sign-in"></i> ${message(code: 'springSecurity.login.button')}</button> 
      </p> 
     </form> 
     <p class="text-center text-muted"><g:link controller="users" action="create"><g:message code="spring.security.ui.login.register" default="New User"/></g:link></p> 
     <p class="text-center text-muted"><g:link controller="register" action="forgotPassword"><g:message code="spring.security.ui.login.forgotPassword" default="Forgot Password"/></g:link></p> 
     </div> 
    </div> 
    </div> 
</div> 
<!-- *** LOGIN MODAL END ***--> 

ここでは、春のコアでログインコントローラです:

def auth() { 

    def config = SpringSecurityUtils.securityConfig 

    if (springSecurityService.isLoggedIn()) { 
     redirect uri: config.successHandler.defaultTargetUrl 
     return 
    } 

    String view = 'auth' 
    String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}" 
    render view: view, model: [postUrl: postUrl, 
           rememberMeParameter: config.rememberMe.parameter] 
} 
+0

ログインアクションを処理するコントローラの現在のコードを表示します。 –

+0

@Michal_Szulc質問を編集しました – Sherif

+0

あなたのモーダルは "auth.gsp"やその他のカスタムログインビューにありますか? –

答えて

0

あなたはLoginControllerをオーバーライドし、あなたが望むすべてのアクションを変更することができます。これを考慮に入れて...

アクションからあなたのgspビューにパラメータを送信して、そのパラメータが存在する場合、そのモーダルを開くことができます。

<g:if test="${openModal}"> 
<script> 
    $(document).ready(function(){ 
     openModal(); 
    }); 
<script> 
</g:if> 

これ以外の方法ですべてをAjaxで処理しています。

前の例のように、LoginControllerをオーバーライドしてプロジェクトのコピーを作成する必要があります。

その後、結果を期待するすべてのアクションを変更し、Ajaxレスポンスに対するレスポンスを変更する必要があります。

render status: 401, text: 'Unauthorized' 

もちろん、フォームの動作を変更して、ログインコントローラへのajax呼び出しを行う必要があります。お勧めしませんが、g:remoteFormの代わりにそのフォームを置き換えることができます。または独自のAjax関数を作成します。