亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解使用Spring3 實現(xiàn)用戶登錄以及權(quán)限認(rèn)證

 更新時間:2017年03月17日 11:07:39   作者:huimingBall  
這篇文章主要介紹了詳解使用Spring3 實現(xiàn)用戶登錄以及權(quán)限認(rèn)證,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。

使用Spring3 實現(xiàn)用戶登錄以及權(quán)限認(rèn)證

這里我就簡單介紹一下,我在實現(xiàn)的時候處理的一些主要的實現(xiàn)。

1.用戶登錄

 <form action="loginAction.do" method="post"> 
  <div class="header"> 
  <h2 class="logo png"></h2> 
  </div> 
  <ul> 
        <li><label>用戶名</label><input name="username" type="text" class="text"/></li> 
        <li/> 
        <li><label>密 碼</label><input name="password" type="password" class="text" /></li>  
        <li/> 
        <li class="submits"> 
          <input class="submit" type="submit" value="登錄" /> 
        </li> 
  </ul> 
  <div class="copyright">© 2013 - 2014 |</div> 
</form> 

以上是前臺頁面,后臺的就是一個簡單的邏輯實現(xiàn):

    @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,來顯示密碼錯誤等信息:

<script type="text/javascript"> 
var login_username_info = '<%=request.getSession().getAttribute("currentUser") == null ? "" : request.getSession().getAttribute("currentUser")%>'; 
var login_message_info = '<%=request.getSession().getAttribute("login_message") == null ? "" : request.getSession().getAttribute("login_message")%>'; 
if(login_message_info != null && login_message_info != ''){ 
  alert(login_message_info); 
} 
 
</script> 

3.攔截未登錄用戶的請求

這里,從頁面和后臺實現(xiàn)了雙重攔截:

頁面代碼如下:

<% 
if(session.getAttribute("currentUser")==null){ 
%> 
window.parent.location='login.html'; 
<% 
} 
%> 

后臺是一個攔截器(servlet-config.xml):

<!-- 攔截器 -->  
  <mvc:interceptors>  
    <mvc:interceptor>  
      <mvc:mapping path="/*.do" />  
      <bean class="com..log.report.interceptor.AccessStatisticsIntceptor" />  
    </mvc:interceptor>  
  </mvc:interceptors>  

攔截器的實現(xiàn)是

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; 
 } 

具體如何校驗的,會根據(jù)用戶的權(quán)限,就不介紹了

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.保存用戶名密碼

登錄頁面提供一個保存下拉框:

<select class="save_login" id="savetime" name="savetime"> 
  <option selected value="0">不保存</option> 
  <option value="1">保存一天</option> 
  <option value="2">保存一月</option> 
  <option value="3">保存一年</option> 
</select> 

后臺在登錄時會操作,將信息保存在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); 
}  

前臺在發(fā)現(xiàn)用戶未登錄時,會取出cookie中的數(shù)據(jù)去登錄:

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=" + "<%=username%>"+ "&password=" + "<%=password%>", 
      success : function(msg){   
        if(msg.status == 'success') 
          window.parent.location.reload(); 
        else if(msg.status == 'failed') 
          gotoLoginPage(); 
      } 
    }); 
    <% 
  } else { 
    %> 
    gotoLoginPage(); 
    <% 
  } 
   
  ... 

以上就列出了我在解決登錄相關(guān)問題的方法,代碼有點長,就沒有全部列出。

相關(guān)文章

  • JavaWeb實現(xiàn)注冊用戶名檢測

    JavaWeb實現(xiàn)注冊用戶名檢測

    這篇文章主要為大家詳細(xì)介紹了JavaWeb實現(xiàn)注冊用戶名檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • springboot+thymeleaf+mybatis實現(xiàn)甘特圖的詳細(xì)過程

    springboot+thymeleaf+mybatis實現(xiàn)甘特圖的詳細(xì)過程

    這篇文章主要介紹了springboot+thymeleaf+mybatis實現(xiàn)甘特圖的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • Java實現(xiàn)等待所有子線程結(jié)束后再執(zhí)行一段代碼的方法

    Java實現(xiàn)等待所有子線程結(jié)束后再執(zhí)行一段代碼的方法

    這篇文章主要介紹了Java實現(xiàn)等待所有子線程結(jié)束后再執(zhí)行一段代碼的方法,涉及java多線程的線程等待與執(zhí)行等相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • springAOP的三種實現(xiàn)方式示例代碼

    springAOP的三種實現(xiàn)方式示例代碼

    這篇文章主要介紹了springAOP的三種實現(xiàn)方式,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 在Spring Boot中處理文件上傳功能實現(xiàn)

    在Spring Boot中處理文件上傳功能實現(xiàn)

    這篇文章主要介紹了如何在Spring Boot中處理文件上傳,通過配置文件上傳屬性、創(chuàng)建控制器來處理上傳的文件,并通過異常處理器來管理錯誤情況,可以快速實現(xiàn)文件上傳功能,需要的朋友可以參考下
    2024-06-06
  • Java String不可變性實現(xiàn)原理解析

    Java String不可變性實現(xiàn)原理解析

    這篇文章主要介紹了Java String不可變性實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Spring bean生命周期配置過程解析

    Spring bean生命周期配置過程解析

    這篇文章主要介紹了Spring bean生命周期配置過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • 深入淺出JAVA MyBatis-快速入門

    深入淺出JAVA MyBatis-快速入門

    這篇文章主要介紹了在今天這篇博文中,我將要介紹一下mybatis的框架原理,以及mybatis的入門程序,實現(xiàn)用戶的增刪改查,她有什么優(yōu)缺點以及mybatis和hibernate之間存在著怎么樣的關(guān)系,大家這些問題一起通過本文學(xué)習(xí)吧
    2021-06-06
  • SpringBoot整合Docker實現(xiàn)一次構(gòu)建到處運行的操作方法

    SpringBoot整合Docker實現(xiàn)一次構(gòu)建到處運行的操作方法

    本文講解的是 SpringBoot 引入容器化技術(shù) Docker 實現(xiàn)一次構(gòu)建到處運行,包括鏡像構(gòu)建、Docker倉庫搭建使用、Docker倉庫可視化UI等內(nèi)容,需要的朋友可以參考下
    2022-10-10
  • 關(guān)于Java中Comparable 和 Comparator的用法

    關(guān)于Java中Comparable 和 Comparator的用法

    這篇文章主要介紹了關(guān)于Java中Comparable 和 Comparator的用法,Comparable 和 Comparator 是關(guān)于排序的兩個接口,用來實現(xiàn) Java 集合中的的排序功能,需要的朋友可以參考下
    2023-04-04

最新評論