spring aop 攔截業(yè)務(wù)方法,實(shí)現(xiàn)權(quán)限控制示例
難點(diǎn):aop類是普通的java類,session是無法注入的,那么在有狀態(tài)的系統(tǒng)中如何獲取用戶相關(guān)信息呢,session是必經(jīng)之路啊,獲取session就變的很重要。思索很久沒有辦法,后來在網(wǎng)上看到了解決辦法。
思路是:
i. SysContext 成員變量 request,session,response
ii. Filter 目的是給 SysContext 中的成員賦值
iii.然后在AOP中使用這個(gè)SysContext的值
要用好,需要理解 ThreadLocal和 和Filter 執(zhí)行順序
1.aop獲取request,response,session等
public class SysContext {
private static ThreadLocal<HttpServletRequest> requestLocal=new ThreadLocal<HttpServletRequest>();
private static ThreadLocal<HttpServletResponse> responseLocal=new ThreadLocal<HttpServletResponse>();
public static HttpServletRequest getRequest(){
return requestLocalget();
}
public static void setRequest(HttpServletRequest request){
requestLocalset(request);
}
public static HttpServletResponse getResponse(){
return responseLocalget();
}
public static void setResponse(HttpServletResponse response){
responseLocalset(response);
}
public static HttpSession getSession(){
return (HttpSession)(getRequest())getSession();
}
}
2.添加過濾器
public class GetContextFilter implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
SysContextsetRequest((HttpServletRequest)request);
SysContextsetResponse((HttpServletResponse)response);
chaindoFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
}
}
3.配置web.xml
將這部分放置在最前面,這樣可以過濾到所有的請(qǐng)求
<filter> <filter-name>sessionFilter</filter-name> <filter-class>comuneifilterGetContextFilter</filter-class> </filter> <filter-mapping> <filter-name>sessionFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping>
4.spring aop before
從session中取出用戶名,如果不存在,拋出異常跳轉(zhuǎn),將錯(cuò)誤信息放到request中
@Aspect
public class AdminAspect {
ActionContext context = ActionContextgetContext();
HttpServletRequest request;
HttpServletResponse response;
@Before("execution(* comuneiActionAdminActiongetPrivileges())")
public void adminPrivilegeCheck()
throws Throwable {
HttpSession session = SysContextgetSession();
request = SysContextgetRequest();
response = SysContextgetResponse();
String userName = "";
try {
userName = sessiongetAttribute("userName")toString();
if(userName==null||userNameequals(""))
throw new Exception("no privilege");
} catch (Exception ex) {
requestsetAttribute("msg", "{\"res\":\"" + "無權(quán)限" + "\"}");
try {
requestgetRequestDispatcher("/jsp/jsonjsp")forward(
request, response);
} catch (ServletException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
5.applicationContext.xml
<bean id="adminAspect" class="comuneiaopAdminAspect"></bean>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot時(shí)間格式化的方法小結(jié)
SpringBoot中的時(shí)間格式化通常指的是將Java中的日期時(shí)間類型轉(zhuǎn)換為指定格式的字符串,或者將字符串類型的時(shí)間解析為Java中的日期時(shí)間類型,本文小編將給大家詳細(xì)總結(jié)了SpringBoot時(shí)間格式化的方法,剛興趣的小伙伴跟著小編一起來看看吧2023-10-10
springboot?vue測(cè)試平臺(tái)接口定義及發(fā)送請(qǐng)求功能實(shí)現(xiàn)
這篇文章主要為大家介紹了springboot+vue測(cè)試平臺(tái)接口定義及發(fā)送請(qǐng)求功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
五種單件模式之Singleton的實(shí)現(xiàn)方法詳解
本篇文章是對(duì)Singleton的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁面
這篇文章主要介紹了jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁面,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
javaweb servlet中使用請(qǐng)求轉(zhuǎn)發(fā)亂碼的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猨avaweb servlet中使用請(qǐng)求轉(zhuǎn)發(fā)亂碼的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08

