当前位置:首页>正文

springmvc拦截器不生效 SpringMVC中配aop拦截不生效,咋回事

2023-04-11 14:31:04 互联网 未知

SpringMVC中配aop拦截不生效,咋回事

首先我的aop切面类文件路径有问题,后来移到了logic下的util包中;
放在spring-mvc.xml中即可。
附上拦截controller的类:

package com.ts.bg.logic.util

import javax.servlet.http.HttpServletRequest

import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.stereotype.Component

@Component
@Aspect
public class IndexAopUtil {

//设置切入点,匹配所有controller的toIndex方法
public static final String INDEX_LIST_INIT="execution(* com.ts.bg.controller.*.toIndex(..))"

@Around(INDEX_LIST_INIT)
public Object index_list_init(ProceedingJoinPoint joinPoint) throws Throwable{
Object object =null
Object[] args = joinPoint.getArgs()
if(args!=null){
HttpServletRequest request=(HttpServletRequest) args[0]
object=joinPoint.proceed()
//设置菜单编号
request.setAttribute("menuCode", request.getParameter("menuCode"))
}
return object
}
}

该类的意义:拦截所有controller目录下的controller中的toIndex方法。
网上有说controller类拦截不到,用其他方式,我试过了,不行。

可能和spring版本有关系,我的是spring4.x。

spring拦截器不起作用

public class CheckSessionInterceptor implements HandlerInterceptor {

    /**
     * 生成视图时执行,可以用来处理异常,并记录在日志中
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception exception){

    }

    /** -
     * 生成视图之前执行,可以修改ModelAndView
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception{

    }

    /**
     * 进入拦截器后首先进入的方法
     * 返回false则不再继续执行
     * 返回true则继续执行
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getContextPath()
        if(request.getRequestURI().equals(path) || request.getRequestURI().equals(path   "/")) {
            return true
        }
        HandlerMethod  handlerMethod = null
        try {
            handlerMethod = (HandlerMethod)handler
        } catch(ClassCastException ex) {
            return true
        }
        NoCheckSession noCheckSession = handlerMethod.getMethodAnnotation(NoCheckSession.class)
        ResponseBody jsonMethod = handlerMethod.getMethodAnnotation(ResponseBody.class)

        if(noCheckSession == null) {
            if(request.getSession().getAttribute(BaseController.LOGIN_USER) == null) {
                if(jsonMethod != null) {
                    response.reset()
                    response.setContentType("application/json charset=UTF-8")
                    PrintWriter pw = response.getWriter()
                    pw.write("{"success" : false, "exceptionflag":"no_session"}")
                    pw.flush()
                    pw.close()
                    return false
                } else {
                    request.getRequestDispatcher("/sys/logout").forward(request, response)
                    return false
                }
            }
        }
        return true
    }
}

requestmapping-spring MVC 的拦截器怎么拦截不了

spring mvc 拦截器怎么拦截jsp页面 你这个 是拦截带 /jsp 的 .do请求 解决方案 用spring 的拦截器 去拦截 所有的 .do 请求, 然后写一个 过滤器去拦截 所有的.jsp 的请求 这样才能防止循环过滤 这种会把所有jsp请求过滤不推荐。 loginFilter

spring AOP 拦截器实现问题

创建拦截类:
@Aspect
public class MyAspect{

     /** 执行前拦截 */
@Before("execution(* t.t..service.*Service.*(..))")
public void before(JoinPoint point) throws Throwable {
System.out.println("执行方法:"   point.getSignature().getDeclaringTypeName()   "."   point.getSignature().getName())
}

/** 执行后拦截 */
@After("execution(* t.t..service.*Service.*(..))")
public void after(JoinPoint point) throws Throwable {
System.out.println("执行完成:"   point.getSignature().getDeclaringTypeName()   "."   point.getSignature().getName())
}

/** 执行前后拦截(环绕) */
@Around("execution(* cn.cydl.dlj..service.*Service.*(..))")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("执行前...")
// 这里相当于@Before
Object obj = point.proceed()// 调用方法具体执行过程,如果不调用,这原来的方法就不会执行了
// obj问原来的方法返回值,如果不返回obj,则原来的方法即时有return也不会返回任何值
// 这里相当于@After
System.out.println("执行后...")
return obj
}

applicationContext.xml中
引入AOP支持(头部,请自行补完整,这里不能写html地址):
xmlns:aop="...ingframework.org/schema/aop" 

xsi:schemaLocation="...
...ingframework.org/schema/aop
...ingframework.org/schema/aop/spring-aop-3.2.xsd
"

具体AOP配置:



 
需要Spring核心、SpringAOP和aspectjrt、aspectjweaver等包支持。

spring mvc拦截器异常类怎么写

nodejs 可以 直接 require JSON文件的....跟require JS文件一样

我想把数据库相关的地址端口,账号密码等等写到一个json文件里,然后node从json里读取

我用的mongoskin,我现在是在代码里写死的,比如:

var db = require(mongoskin).db(root:123@localhost:27017/xxxx)

如果要读json的话,要用fs模块,然后

var db
fs.readFile(json , function(err,data)) {
db = ......
}

转载,仅供参考。