spring AOP 对请求接口做登录校验拦截

 AOP配置类:

package com.xty.conf;

import com.xty.model.service.UserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;


/**
 * Created by xuyling on 2019/9/10.
 */
@Aspect
@Component
public class AopConfig {

    @Autowired
    private UserService userService;

    @Pointcut("execution(* com.xty.model.service.StudentService.*(..))")//切入面在StudentService类下的所有方法
    public void match(){}

    @Around("match()")
    public Object before(ProceedingJoinPoint pjp) throws Throwable {

        Object result = null;
        System.out.println("前置通知");
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();//获取当前请求
        HttpSession session=attr.getRequest().getSession(true);
        boolean isLogin = userService.isLogin(session);
        
        if(!isLogin){
            //未登录
            ArrayList list = new ArrayList();
            list.add("用户未登录");
            return list;
        }else{
           result = pjp.proceed(pjp.getArgs());//执行后面的业务代码
            System.out.println("后置通知");
           return result;
        }

    }

   /* @After("match()")
    public void after(){
        System.out.println("后置通知");
    }*/

}

登录类编写,这里只是做测试,写的很粗糙

package com.xty.model.service;

import com.xty.model.entity.UserEntity;
import com.xty.model.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by xuyling on 2019/8/5.
 */
@Service
public class UserService {

        @Autowired
        UserMapper userMapper;

        public UserEntity Sel(int id){
            return userMapper.Sel(id);
        }
    

        //判断用户是否登录
        public boolean isLogin(HttpSession session){

            UserEntity currentUser = (UserEntity) session.getAttribute("current_user");
            if(currentUser!=null){
               return true;
            }
            return false;
        }

        //登录
        public UserEntity login(UserEntity userEntity,HttpSession session){
            if(StringUtils.isNotBlank(userEntity.getUserName())&&StringUtils.isNotBlank(userEntity.getPassWord())){
                UserEntity user = userMapper.getUserByUsernameAndPassword(userEntity.getUserName(), userEntity.getPassWord());
                if(user!=null){
                    session.setAttribute("current_user",user);
                    return user;
                }
            }

            return null;

        }
        //退出
        public boolean outLogin(HttpSession session){
            session.removeAttribute("current_user");
            return true;

        }


}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!感谢您提出问题。 针对您的问题,SpringAOP实现业务错误信息校验的具体步骤如下: 1.定义注解 首先,需要定义一个注解,用于表示需要进行校验的方法或参数。例如,我们可以定义一个@CheckParam注解,表示需要对方法的参数进行校验。 2.编写校验逻辑 接下来,需要编写具体的校验逻辑。在实现业务错误信息校验时,一般需要判断参数是否符合要求,如果不符合,则需要抛出异常并返回错误信息。 3.编写切面 现在,我们可以编写一个切面,在方法执行之前和之后进行拦截并进行校验操作。在方法执行前,我们可以通过反射获取注解信息并进行校验,如果不符合要求,则抛出异常并返回错误信息。在方法执行后,我们可以记录日志等操作。 4.配置切面 最后,需要将切面配置到Spring容器中,这可以通过在配置文件中添加aop:aspectj-autoproxy节点来实现。 具体实现可以参考以下代码示例: ``` //定义注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface CheckParam { } //编写校验逻辑 public void checkParam(Object obj) { //对obj进行校验操作,例如判断是否为空、是否符合格式等等 if (obj == null) { throw new IllegalArgumentException("参数不能为空"); } } //编写切面 @Component @Aspect public class CheckParamAspect { @Before("@annotation(com.example.CheckParam)") public void checkParamBefore(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); for (Object arg : args) { checkParam(arg);//调用校验逻辑 } } @AfterReturning(returning = "result", pointcut = "@annotation(com.example.CheckParam)") public void checkParamAfter(Object result) { //记录日志等操作 } @AfterThrowing(throwing = "ex", pointcut = "@annotation(com.example.CheckParam)") public void checkParamThrowing(Exception ex) { //处理异常并返回错误信息 } } //配置切面 <aop:aspectj-autoproxy/> <bean id="checkParamAspect" class="com.example.CheckParamAspect"> ``` 以上是通过SpringAOP实现业务错误信息校验的具体步骤和代码示例。希望能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值