欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

spring aop 拦截业务方法,实现权限控制示例

时间:2020-10-03 10:16:09|栏目:JAVA代码|点击:

难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要。思索很久没有办法,后来在网上看到了解决办法。

思路是:

i. SysContext  成员变量 request,session,response 

ii. Filter 目的是给 SysContext 中的成员赋值 

iii.然后在AOP中使用这个SysContext的值

要用好,需要理解  ThreadLocal和  和Filter 执行顺序

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 

将这部分放置在最前面,这样可以过滤到所有的请求

<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中取出用户名,如果不存在,抛出异常跳转,将错误信息放到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\":\"" + "无权限" + "\"}"); 
    try { 
     requestgetRequestDispatcher("/jsp/jsonjsp")forward( 
       request, response); 
    } catch (ServletException e) { 
     eprintStackTrace(); 
    } catch (IOException e) { 
     eprintStackTrace(); 
    } 
   } 
  } 
 } 

5.applicationContext.xml

<bean id="adminAspect" class="comuneiaopAdminAspect"></bean> 

上一篇:在IntelliJ IDEA中创建和运行java/scala/spark程序的方法

栏    目:JAVA代码

下一篇:Java――对象初始化顺序使用详解

本文标题:spring aop 拦截业务方法,实现权限控制示例

本文地址:http://www.codeinn.net/misctech/6464.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有