0

我在我的 spring-extjs 应用程序中获取登录用户时遇到问题。我使用的是 spring security 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 文件

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 页面上。通过其他线程但没有帮助。任何帮助将不胜感激

感谢萨钦的时间

4

1 回答 1

2

您不需要将用户名放入对象模型中,因为您可以像在控制器类中那样从 jsp 访问它:

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

甚至更好:

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

但我不确定您是否可以在 Spring Security 2 中使用该标记库

于 2012-05-09T19:47:21.160 回答