2012-05-09 123 views
0

當前的loggedIn用戶我在獲得登錄的用戶在我的彈簧ExtJS的application.I正在使用彈簧安全2.0.4.Here一個問題是什麼,我都試過的細節。抓取使用彈簧安全

控制器類:

@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文件

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(); 
     } 

     }); 
    } 
    }] 
}); 

在我的jsp頁面訪問它這樣。

<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頁面上。通過其他線程,但沒有幫助。任何幫助,將不勝感激

感謝您的時間 薩欽

+0

你確定你要在控制器類之後立即訪問jsp嗎?因爲這似乎是請求由你到達JSP – Dani

+0

的那一刻失去了是的,我不打算JSP,但後來我不能夠調試it.Checked日誌,但不能跟蹤的錯誤。你能糾正我錯誤的地方嗎?謝謝 – KillABug

回答

2

你並不需要把用戶名到一個對象模型,因爲你可以從一個jsp就像你在控制器類的方式做訪問:

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

甚至更​​好:

<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(); String name = auth.getName(); //獲取登錄用戶名 model.addAttribute(「username」,name); \t \t的System.out.println(名稱); \t返回新的ModelAndView(「index」); '但你能解釋兩者在執行上有何不同嗎? – KillABug

+0

同樣,你不需要用戶名存儲爲一個模型屬性,因爲你可以訪問它,無論你是通過SecurityContextHolder中。我的意思是,你正在從事Spring Security已經爲你做的一份工作。 – Dani

+0

明白了你的觀點,框架已經實現了它,我們只需要使用它。謝謝你的幫助。我已經發布了另一個我遇到的問題(更多的邏輯)在這裏http://stackoverflow.com/questions/10545369/issue-with-mapping-email-ids-to-specific-users請儘可能檢查並提供幫助 – KillABug