實(shí)現(xiàn)功能權(quán)限校驗(yàn)的功能有多種方法,其一使用攔截器攔截請(qǐng)求,其二是使用AOP拋異常。
首先用攔截器實(shí)現(xiàn)未登錄時(shí)跳轉(zhuǎn)到登錄界面的功能。注意這里沒有使用AOP切入,而是用攔截器攔截,因?yàn)锳OP一般切入的是service層方法,而攔截器是攔截控制器層的請(qǐng)求,它本身也是一個(gè)處理器,可以直接中斷請(qǐng)求的傳遞并返回視圖,而AOP則不可以。
1.使用攔截器實(shí)現(xiàn)未登錄時(shí)跳轉(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 } }
分享標(biāo)題:SpringAOP實(shí)現(xiàn)功能權(quán)限校驗(yàn)功能的示例代碼-創(chuàng)新互聯(lián)
URL地址:http://chinadenli.net/article4/dhjoie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、關(guān)鍵詞優(yōu)化、網(wǎng)站排名、網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容