最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

這里我就簡(jiǎn)單介紹一下,我在實(shí)現(xiàn)的時(shí)候處理的一些主要的實(shí)現(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> 

以上是前臺(tái)頁(yè)面,后臺(tái)的就是一個(gè)簡(jiǎn)單的邏輯實(shí)現(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.登錄信息

這里,在登錄頁(yè)面有一段JavaScript,來(lái)顯示密碼錯(cuò)誤等信息:

<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.攔截未登錄用戶的請(qǐng)求

這里,從頁(yè)面和后臺(tái)實(shí)現(xiàn)了雙重?cái)r截:

頁(yè)面代碼如下:

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

后臺(tái)是一個(gè)攔截器(servlet-config.xml):

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

攔截器的實(shí)現(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())) { 
      //校驗(yàn)失敗 
      return false; 
//     throw new CustomException(LogConstant.USER_NOT_LOGIN); 
    } 
      return true; 
 } 

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

4.返回未登錄前訪問(wèn)的頁(yè)面

首先在頁(yè)面添加一段腳本,使用jQuery去訪問(wèn)后臺(tái)

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

然后,后臺(tái)有記錄這個(gè)頁(yè)面:

@RequestMapping(value="setPreviousPageAction.do") 
public void setPreviousPageAction(@RequestParam(value="previousPage") String previousPage, HttpSession session){ 
  session.setAttribute(LogConstant.PREVIOUS_PAGE, previousPage); 
} 

在登錄完成后,返回這個(gè)頁(yè)面即可。

5.保存用戶名密碼

登錄頁(yè)面提供一個(gè)保存下拉框:

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

后臺(tái)在登錄時(shí)會(huì)操作,將信息保存在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); 
}  

前臺(tái)在發(fā)現(xiàn)用戶未登錄時(shí),會(huì)取出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)問(wèn)題的方法,代碼有點(diǎn)長(zhǎng),就沒(méi)有全部列出。

相關(guān)文章

  • Java中如何獲取文件的上級(jí)目錄

    Java中如何獲取文件的上級(jí)目錄

    這篇文章主要介紹了Java中如何獲取文件的上級(jí)目錄問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Springboot返回的json屏蔽某些屬性的操作

    Springboot返回的json屏蔽某些屬性的操作

    這篇文章主要介紹了Springboot返回的json屏蔽某些屬性的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法

    Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法

    eclipse工程前面出現(xiàn)紅色嘆號(hào)都是由于eclipse項(xiàng)目、eclipse工程中,缺少了一些jar包等文件引起的,這篇文章主要給大家介紹了關(guān)于Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法,需要的朋友可以參考下
    2023-11-11
  • java理論基礎(chǔ)函數(shù)式接口特點(diǎn)示例解析

    java理論基礎(chǔ)函數(shù)式接口特點(diǎn)示例解析

    這篇文章主要為大家介紹了java理論基礎(chǔ)函數(shù)式接口特點(diǎn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Spring的@Value注入復(fù)雜類型(通過(guò)@value注入自定義類型)

    Spring的@Value注入復(fù)雜類型(通過(guò)@value注入自定義類型)

    Spring的@Value可以注入復(fù)雜類型嗎?今天教你通過(guò)@value注入自定義類型。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java十分鐘精通異常處理機(jī)制

    Java十分鐘精通異常處理機(jī)制

    異常就是不正常,比如當(dāng)我們身體出現(xiàn)了異常我們會(huì)根據(jù)身體情況選擇喝開(kāi)水、吃藥、看病、等?異常處理方法。?java異常處理機(jī)制是我們java語(yǔ)言使用異常處理機(jī)制為程序提供了錯(cuò)誤處理的能力,程序出現(xiàn)的錯(cuò)誤,程序可以安全的退出,以保證程序正常的運(yùn)行等
    2022-03-03
  • MyBatisPlus超詳細(xì)分析條件查詢

    MyBatisPlus超詳細(xì)分析條件查詢

    這篇文章主要介紹了MyBatisPlus條件查詢的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • SpringBoot2.3整合redis緩存自定義序列化的實(shí)現(xiàn)

    SpringBoot2.3整合redis緩存自定義序列化的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot2.3整合redis緩存自定義序列化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Spring MVC請(qǐng)求參數(shù)的獲取教程指南

    Spring MVC請(qǐng)求參數(shù)的獲取教程指南

    本文介紹了SpringMVC中如何獲取各種類型的請(qǐng)求參數(shù),包括基本類型、POJO、數(shù)組、集合以及RESTful風(fēng)格的參數(shù),還討論了請(qǐng)求參數(shù)中文亂碼的解決方案,參數(shù)綁定的注解如@RequestParam,以及自定義類型轉(zhuǎn)換器的實(shí)現(xiàn),需要的朋友可以參考下
    2024-10-10
  • Mac安裝多個(gè)JDK并實(shí)現(xiàn)動(dòng)態(tài)切換

    Mac安裝多個(gè)JDK并實(shí)現(xiàn)動(dòng)態(tài)切換

    有時(shí)候我們有多個(gè)項(xiàng)目需要使用多個(gè)版本JDK,本文主要介紹了Mac安裝多個(gè)JDK并實(shí)現(xiàn)動(dòng)態(tài)切換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評(píng)論

广德县| 砚山县| 莒南县| 兴义市| 雷波县| 东丰县| 西贡区| 浦县| 台中县| 台安县| 安丘市| 蒙自县| 达孜县| 玛沁县| 积石山| 连云港市| 嫩江县| 青冈县| 忻州市| 张家川| 宁国市| 吉木乃县| 江山市| 玉环县| 旬阳县| 温宿县| 嘉禾县| 邵武市| 绥化市| 永春县| 锡林浩特市| 衡山县| 资中县| 宁南县| 华池县| 龙游县| 平遥县| 辉县市| 班戈县| 西乡县| 凉城县|