javascript
java spring 登录验证_详解使用Spring3 实现用户登录以及权限认证
使用Spring3 實現用戶登錄以及權限認證
這里我就簡單介紹一下,我在實現的時候處理的一些主要的實現。
1.用戶登錄
- 用戶名
- 密 碼
以上是前臺頁面,后臺的就是一個簡單的邏輯實現:
@RequestMapping(value="loginAction.do", method=RequestMethod.POST)
public ModelAndView loginAction(@RequestParam(value="username") String username, @RequestParam(value="password") String password, HttpSession session, HttpServletResponse resp, @RequestParam(value="savetime", required=false) String savetime) {
session.removeAttribute(LogConstant.LOGIN_MESSAGE);
SystemUserDataBean user = userDao.getSystemUserByUserName(username);
ModelAndView view = null;
if(user == null) {
view = new ModelAndView(new RedirectView("login.html"));
session.setAttribute(LogConstant.LOGIN_MESSAGE, "用戶名不正確");
return view;
}
boolean isPasswordCorrect = EncryptionUtil.compareSHA(password, user.getPassword());
if(isPasswordCorrect){
session.setAttribute(LogConstant.CURRENT_USER, username);
} else{
view = new ModelAndView(new RedirectView("login.html"));
session.setAttribute(LogConstant.LOGIN_MESSAGE, "密碼不正確");
}
return view;
}
2.登錄信息
這里,在登錄頁面有一段JavaScript,來顯示密碼錯誤等信息:
var login_username_info = '';
var login_message_info = '';
if(login_message_info != null && login_message_info != ''){
alert(login_message_info);
}
3.攔截未登錄用戶的請求
這里,從頁面和后臺實現了雙重攔截:
頁面代碼如下:
if(session.getAttribute("currentUser")==null){
%>
window.parent.location='login.html';
}
%>
后臺是一個攔截器(servlet-config.xml):
攔截器的實現是
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class AccessStatisticsIntceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object obj) throws Exception {
String uri = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") +1);
if(!AuthorityController.isAuthorized(uri, request.getSession())) {
//校驗失敗
return false;
// throw new CustomException(LogConstant.USER_NOT_LOGIN);
}
return true;
}
具體如何校驗的,會根據用戶的權限,就不介紹了
4.返回未登錄前訪問的頁面
首先在頁面添加一段腳本,使用jQuery去訪問后臺
var page = "";
var loc = decodeURIComponent(window.parent.location);
var start = loc.indexOf("Log/") + 8;
var end = loc.indexOf(".html");
page = loc.substr(start, end-start);
if(page != null && page != '') {
alert(page);
$.ajax({
type : "get",
url : "setPreviousPageAction.do?previousPage=" + page + ".html",
success : function(msg){
}
});
}
然后,后臺有記錄這個頁面:
@RequestMapping(value="setPreviousPageAction.do")
public void setPreviousPageAction(@RequestParam(value="previousPage") String previousPage, HttpSession session){
session.setAttribute(LogConstant.PREVIOUS_PAGE, previousPage);
}
在登錄完成后,返回這個頁面即可。
5.保存用戶名密碼
登錄頁面提供一個保存下拉框:
不保存
保存一天
保存一月
保存一年
后臺在登錄時會操作,將信息保存在cookie中:
if(savetime != null) { //保存用戶在Cookie
int savetime_value = savetime != null ? Integer.valueOf(savetime) : 0;
int time = 0;
if(savetime_value == 1) { //記住一天
time = 60 * 60 * 24;
} else if(savetime_value == 2) { //記住一月
time = 60 * 60 * 24 * 30;
} else if(savetime_value == 2) { //記住一年
time = 60 * 60 * 24 * 365;
}
Cookie cid = new Cookie(LogConstant.LOG_USERNAME, username);
cid.setMaxAge(time);
Cookie cpwd = new Cookie(LogConstant.LOG_PASSWORD, password);
cpwd.setMaxAge(time);
resp.addCookie(cid);
resp.addCookie(cpwd);
}
前臺在發現用戶未登錄時,會取出cookie中的數據去登錄:
if(session.getAttribute("currentUser")==null){
Cookie[] cookies = request.getCookies();
String username = null;
String password = null;
for(Cookie cookie : cookies) {
if(cookie.getName().equals("log_username")) {
username = cookie.getValue();
} else if(cookie.getName().equals("log_password")) {
password = cookie.getValue();
}
}
if(username != null && password != null) {
%>
$.ajax({
type : "post",
url : "loginByCookieAction.do",
data:"username=" + ""+ "&password=" + "",
success : function(msg){
if(msg.status == 'success')
window.parent.location.reload();
else if(msg.status == 'failed')
gotoLoginPage();
}
});
} else {
%>
gotoLoginPage();
}
...
以上就列出了我在解決登錄相關問題的方法,代碼有點長,就沒有全部列出。
總結
以上是生活随笔為你收集整理的java spring 登录验证_详解使用Spring3 实现用户登录以及权限认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: aqs java_Java并发之AQS详
- 下一篇: gradle idea java ssm