我正在使用Spring 2.5.6,Spring Security 2.0.4和Hibernate 3.2.6開發應用程序。在我的應用程序中,用戶可以同時使用相同的憑據從不同的瀏覽器登錄。有一個選項可以更改用戶的電子郵件ID。控制器,該控制器處理這個請求如下: -使用Hibernate和Spring 2.5的併發會話問題
@RequestMapping(value="/changeEmail", method= RequestMethod.POST)
public void changeEmail(HttpServletRequest request, HttpServletResponse response, Principal principal) throws IOException
{
System.out.println("We got the Request");
boolean hasError = false;
String currentEmail = request.getParameter("currentEmail");
String newEmail = request.getParameter("newEmail");
String confirmEmail = request.getParameter("confirmEmail");
String password = request.getParameter("password");
System.out.println("Old Email = " + currentEmail + ", New Email = " + newEmail + ", Confirm Email = " + confirmEmail + ", Password = " + password);
Map<String, String> errorsMap = new HashMap<String,String>();
UserFormResponse callBackResponse = new UserFormResponse("",errorsMap);
if(currentEmail == null || currentEmail.equals(""))
{
hasError = true;
errorsMap.put("currentEmail", "Enter Your Current Email");
}
if(newEmail == null || newEmail.equals(""))
{
hasError = true;
errorsMap.put("newEmail", "Enter Your New Email");
}
if(confirmEmail == null || confirmEmail.equals("") || !confirmEmail.equals(newEmail))
{
hasError = true;
errorsMap.put("confirmEmail", "Confirm Your Email");
}
if(password == null || password.equals(""))
{
hasError = true;
errorsMap.put("password", "Enter Your Password");
}
if(!hasError)
{
System.out.print("Should be printed : " + callBackResponse.getStatus());
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Object myUser = (auth != null) ? auth.getPrincipal() : null;
if (myUser instanceof User) {
User user = (User) myUser;
if(!user.getPassword().equals(password))
{
callBackResponse.setStatus("Wrong Password");
errorsMap.put("password", "Enter Correct Password");
}
else if(!user.getUserEmail().equals(currentEmail))
{
callBackResponse.setStatus("Wrong Email");
errorsMap.put("currentEmail", "Enter Your Current Email");
}
else
{
user.setUserEmail(newEmail);
getHibernateTemplate().update(user);
callBackResponse.setStatus("Success");
}
}
else
{
callBackResponse.setStatus("You must be logged in");
}
}
else
{
callBackResponse.setStatus("Kindly fill all fields");
}
callBackResponse.setErrorsMap(errorsMap);
System.out.println(callBackResponse.getStatus());
System.out.println(callBackResponse.getErrorsMap());
objectMapper.writeValue(response.getOutputStream(),callBackResponse);
}
電子郵件被完全改變了,當我檢查從後端數據庫。但主要問題是,如果用戶有兩個併發會話,則在一個會話中更改的電子郵件不會反映在其他會話中,直到其他會話不會失效。有沒有解決方案,以便一個會話期間的更改可以反映到另一個併發會話?
注意:請不要建議更改spring和hibernate的版本,因爲在2.5.6春天開發這個應用程序是必須的。
我不知道你是如何使用它,但你可以使所有現有的會話與消息無效 - emailid已更改,請重新登錄。 – Keshav
@Keshav:謝謝你的回覆。我在網上搜索了所有仍然不知道如何在Spring security 2.0.4中使用戶的所有會話失效。你能告訴我嗎? – vishal