1、獲取當前登錄人信息
2、登錄驗證的異常列表
3、登出操作
1、獲取當前登錄人信息
//獲取當前登錄人
User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
2、登錄驗證的異常列表
/**
* 登錄操作
*/
@RequestMapping("/login")
public String login(String loginAccount, String loginPassword)
{
//參數驗證
if (UtilHelper.isNullAndEmpty(loginAccount) || UtilHelper.isNullAndEmpty(loginPassword))
{
return "login.html";
}
//賬號密碼令牌
AuthenticationToken token = new UsernamePasswordToken(loginAccount, loginPassword);
//獲得當前用戶到登錄對象,現在狀態爲未認證
Subject subject = SecurityUtils.getSubject();
try
{
//將令牌傳到shiro提供的login方法驗證,需要自定義realm
subject.login(token);
//沒有異常表示驗證成功,進入首頁
return "redirect:/index";
}
catch (IncorrectCredentialsException ice)
{
request.setAttribute("message", "用戶名或密碼不正確!");
}
catch (UnknownAccountException uae)
{
request.setAttribute("message", "未知賬戶!");
}
catch (LockedAccountException lae)
{
request.setAttribute("message", "賬戶被鎖定!");
}
catch (DisabledAccountException dae)
{
request.setAttribute("message", "賬戶被禁用!");
}
catch (ExcessiveAttemptsException eae)
{
request.setAttribute("message", "用戶名或密碼錯誤次數太多!");
}
catch (AuthenticationException ae)
{
request.setAttribute("message", "驗證未通過!");
}
catch (Exception e)
{
request.setAttribute("message", "驗證未通過!");
}
return "login.html";
}
異常類型 | 異常說明 |
---|---|
DisabledAccountException | 禁用的帳號 |
LockedAccountException | 鎖定的帳號 |
UnknownAccountException | 錯誤的帳號 |
ExcessiveAttemptsException | 登錄失敗次數過多 |
IncorrectCredentialsException | 錯誤的憑證(用戶名或密碼錯誤) |
ExpiredCredentialsException | 過期的憑證 |
3、登出操作
/**
* 登出操作
*/
@RequestMapping("/logout")
public String logout()
{
//登出清除緩存
Subject subject = SecurityUtils.getSubject();
subject.logout();
return "redirect:/toLoginView";
}