SpringAOP實現(xiàn)功能權(quán)限校驗功能的示例代碼-創(chuàng)新互聯(lián)

實現(xiàn)功能權(quán)限校驗的功能有多種方法,其一使用攔截器攔截請求,其二是使用AOP拋異常。

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、成都網(wǎng)站建設(shè)與策劃設(shè)計,桑植網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務涵蓋:桑植等地區(qū)。桑植做網(wǎng)站價格咨詢:18980820575

首先用攔截器實現(xiàn)未登錄時跳轉(zhuǎn)到登錄界面的功能。注意這里沒有使用AOP切入,而是用攔截器攔截,因為AOP一般切入的是service層方法,而攔截器是攔截控制器層的請求,它本身也是一個處理器,可以直接中斷請求的傳遞并返回視圖,而AOP則不可以。

1.使用攔截器實現(xiàn)未登錄時跳轉(zhuǎn)到登錄界面的功能

1.1 攔截器SecurityInterceptor

package com.jykj.demo.filter;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.jykj.demo.util.Helper;
import com.jykj.demo.util.Result;

public class SecurityInterceptor implements HandlerInterceptor{
 @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
   throws Exception {
  System.out.println("SecurityInterceptor:"+request.getContextPath()+","+request.getRequestURI()+","+request.getMethod());
  HttpSession session = request.getSession();
  if (session.getAttribute(Helper.SESSION_USER) == null) {
   System.out.println("AuthorizationException:未登錄!"+request.getMethod());
   if("POST".equalsIgnoreCase(request.getMethod())){
    response.setContentType("text/html; charset=utf-8"); 
    PrintWriter out = response.getWriter(); 
    out.write(JSON.toJSONString(new Result(false,"未登錄!")));
    out.flush();
    out.close();
   }else{
    response.sendRedirect(request.getContextPath()+"/login"); 
   }
   return false;
  } else {
   return true;
  }
 }

 @Override
 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
   ModelAndView modelAndView) throws Exception {
  // TODO Auto-generated method stub
 }

 @Override
 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
   throws Exception {
  // TODO Auto-generated method stub
 }
}

分享標題:SpringAOP實現(xiàn)功能權(quán)限校驗功能的示例代碼-創(chuàng)新互聯(lián)
URL地址:http://www.muchs.cn/article4/dhjoie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、關(guān)鍵詞優(yōu)化網(wǎng)站排名、網(wǎng)站建設(shè)、響應式網(wǎng)站品牌網(wǎng)站制作

廣告

聲明:本網(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)

搜索引擎優(yōu)化