2012-05-09 16 views
0

私はspring-extjsアプリケーションでログインしたユーザーを取得する際に問題があります。私は春のセキュリティ2.0.4を使用しています。スプリングセキュリティを使用して現在ログインしているユーザーを取得する

コントローラクラス:

@RequestMapping(value="/StaffingApplication/index.action", method = RequestMethod.GET) 
public String printUser(ModelMap model) { 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    String name = auth.getName(); //get logged in username  
    System.out.println(name); 
    model.addAttribute("username", name); 
    return "index.jsp"; 
} 

login.jsは、私はこのようにアクセス私のJSPページで

var login = new Ext.FormPanel({ 
    labelWidth:80, 
    url:'j_spring_security_check', 
    frame:true, 
    title:'Please Login', 
    defaultType:'textfield', 
    width:300, 
    height:130, 
    monitorValid:true, 
    // Specific attributes for the text fields for username/password. 
    // The "name" attribute defines the name of variables sent to the server. 

    items:[{ 
     fieldLabel:'Username', 
     name:'j_username', 
     allowBlank:false 
    },{ 
     fieldLabel:'Password', 
     name:'j_password', 
     inputType:'password', 
     allowBlank:false 
    }], 

    // All the magic happens after the user clicks the button 
    buttons:[{ 
     text:'Login', 
     formBind: true, 
     // Function that fires when user clicks the button 
     handler:function(){ 
     login.getForm().submit({ 

      method:'POST', 

      // Functions that fire (success or failure) when the server responds. 
      // The server would actually respond with valid JSON, 
      // something like: response.write "{ success: true}" or 

      // response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
      // depending on the logic contained within your server script. 
      // If a success occurs, the user is notified with an alert messagebox, 

      // and when they click "OK", they are redirected to whatever page 
      // you define as redirect. 

      success:function(){ 
      Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){ 
       if (btn == 'ok'){ 
         window.location = 'index.action'; 
       } 
      }); 

     }, 

     // Failure function, see comment above re: success and failure. 
     // You can see here, if login fails, it throws a messagebox 
     // at the user telling him/her as much. 


     failure:function(form, action){ 
      if(action.failureType == 'server'){ 
       obj = Ext.util.JSON.decode(action.response.responseText); 

       Ext.Msg.alert('Login Failed!', obj.errors.reason); 
      }else{ 
       Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText); 
       //window.location='loginFailure.html'+action.response.responseText; 
      } 
      login.getForm().reset(); 
     } 

     }); 
    } 
    }] 
}); 

を提出します。

<div id="header" class="header">Options Staffing Application 
    <div style="" id="user-status"> 
     <a href='<c:url value="j_spring_security_logout"/>'>Logout</a> 
     <h3>Username : ${username}</h3> 
    </div> 
</div> 

ただし、ユーザー名の代わりに空白が表示されます。 コントローラーで印刷しようとすると値が表示されますが、JSPページには表示されません。他のスレッドは助けてくれませんでしたが、助けてくれませんでした。

ありがとう

<h3>Username <%=SecurityContextHolder.getContext().getAuthentication().getName(); =></h3> 

あるいはさらに良い:あなたはちょうどあなたがコントローラクラスで行う方法のように、JSPからアクセスすることができますので、あなたは、オブジェクトモデルにユーザ名を入れる必要はありません時間 サチン

+0

コントローラクラスの直後にjspに行ってもよろしいですか?これは要求がjspに到達した瞬間に失われているようだから – Dani

+0

はい、私はJSPには行かないが、それからデバッグすることができません。ログをチェックしても、間違いを追跡することはできません。私が間違っている場所を修正できますか?ありがとうございました – KillABug

答えて

2

<h3>Username <sec:authentication property="name" /></h3> 

しかし、Spring Security 2でtaglibを使用できるかどうかわかりません

+0

コメントをいただきありがとうございました。ModelAndViewを使用して回答を得ました。つまり、@RequestMapping(value = "/ index.action"、method = RequestMethod.GET)を使用しました。 public ModelAndView main(HttpServletRequest request、HttpServletResponse response、ModelMapモデル)は例外をスローする{ \t認証auth = SecurityContextHolder.getContext()。getAuthentication(); 文字列名= auth.getName(); //ログインしたユーザー名を取得する model.addAttribute( "username"、name); \t \t System.out.println(name); \t新しいモデルとビュー( "インデックス")を返します。 } 'しかし、両方の実装がどのように違うのか説明できますか? – KillABug

+0

また、ユーザー名をモデル属性として保存する必要はありません。これは、SecurityContextHolder経由でどこにでもアクセスできるためです。つまり、あなたはSpring Securityがすでにあなたのために行っている仕事に取り組んでいます。 – Dani

+0

あなたのポイントを得ました、フレームワークはそれを実装しており、私たちはそれを使用する必要があります。助けてくれてありがとう。私は別の問題を投稿しました(私はより論理的なものです)http://stackoverflow.com/questions/10545369/issue-with-mapping-email-ids-specific-users可能ならば確認して助けてください – KillABug

関連する問題