zoukankan      html  css  js  c++  java
  • Spring AOP实现拦截转发控制

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
     
    @Aspect
    public class demoInterceptor {
     
    private demoInterceptor() {
     
    };
     
    @Pointcut("execution(* com.demo.controller.login(..))")
    private void interceptor() {
    };
     
    @Around("interceptor()")
    public Object aroundLogin(ProceedingJoinPoint point) throws Throwable {
    HttpServletRequest request = null;
    HttpServletResponse response = null;
    Object[] args = point.getArgs();
    for (int i = 0; i < args.length; i++) {
    if (args[i] instanceof HttpServletRequest) {
    request = (HttpServletRequest) args[i];
    }
    if (args[i] instanceof HttpServletResponse) {
    response = (HttpServletResponse) args[i];
    }
     
    }
    if (request.getParameter("j_username").equals("demo")) {
     
    return point.proceed();
    } else {
    response.sendRedirect("/login.do");
    return null;
    }
    }
    }

    aop-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <aop:aspectj-autoproxy/>
    <bean class="demoInterceptor"/>
    </beans>

    转自:http://www.iprotoss.com/?p=130

  • 相关阅读:
    关于H5中的Canvas API的探索
    leetcode297 Serialize and Deserialize Binary Tree
    CF1187E Tree Painting
    CF1187D Subarray Sorting
    CF1183E/H Subsequences
    kickstart2019 round_C B. Circuit Board
    leetcode85 Maximal Rectangle
    leetcode84 Largest Rectangle in Histogram
    kickstart2019 round_A B. Parcels
    蓝桥杯 正则问题
  • 原文地址:https://www.cnblogs.com/tv151579/p/3482469.html
Copyright © 2011-2022 走看看