私は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からアクセスすることができますので、あなたは、オブジェクトモデルにユーザ名を入れる必要はありません時間 サチン
コントローラクラスの直後にjspに行ってもよろしいですか?これは要求がjspに到達した瞬間に失われているようだから – Dani
はい、私はJSPには行かないが、それからデバッグすることができません。ログをチェックしても、間違いを追跡することはできません。私が間違っている場所を修正できますか?ありがとうございました – KillABug