成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

使用Spring3如何實現(xiàn)一個用戶權(quán)限認證功能

今天就跟大家聊聊有關(guān)使用Spring3 如何實現(xiàn)一個用戶權(quán)限認證功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

十年的通榆網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整通榆建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“通榆網(wǎng)站設(shè)計”,“通榆網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

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

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

1.用戶登錄

 <form action="loginAction.do" method="post"> 
  <div class="header"> 
  <h3 class="logo png"></h3> 
  </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">&copy; 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 &#63; "" : request.getSession().getAttribute("currentUser")%>'; 
var login_message_info = '<%=request.getSession().getAttribute("login_message") == null &#63; "" : 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&#63;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 &#63; 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(); 
    <% 
  } 
   
  ... 

看完上述內(nèi)容,你們對使用Spring3 如何實現(xiàn)一個用戶權(quán)限認證功能有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁題目:使用Spring3如何實現(xiàn)一個用戶權(quán)限認證功能
本文URL:http://jinyejixie.com/article26/ppjjcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、App設(shè)計、搜索引擎優(yōu)化、面包屑導(dǎo)航、做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
获嘉县| 盐源县| 宣恩县| 江都市| 海兴县| 罗定市| 河曲县| 石台县| 新乡市| 长沙县| 石台县| 大庆市| 平乡县| 建水县| 白河县| 莱芜市| 东乌珠穆沁旗| 大庆市| 平乡县| 建水县| 资溪县| 华池县| 思南县| 汉寿县| 酒泉市| 鹤岗市| 普定县| 威宁| 旌德县| 青神县| 环江| 平和县| 莱州市| 洛阳市| 宣恩县| 隆子县| 罗田县| 湖南省| 余江县| 宝清县| 乐都县|