2012-05-09 79 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