spring boot中 使用AOP实现登录权限验证

0.基本概念

  • AOP(Aspect-oriented programming)

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification, such as "log all function calls when the function's name begins with 'set'". This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality. AOP forms a basis for aspect-oriented software development.
在计算中,面向切片的编程(AOP)是一种编程范例,旨在通过允许分离横切关注点来增加模块性。它通过向现有代码(建议)添加额外行为而不修改代码本身,而是通过“切入点”规范分别指定修改哪些代码,例如“当函数名称以'set'开头时记录所有函数调用”。这允许将不是业务逻辑核心的行为(如日志记录)添加到程序中,而不会混淆代码和功能的核心。 AOP构成了面向切片的软件开发的基础。

1.问题描述

在web开发中会涉及到这样一个基本问题,在用户发出一个http请求时,需要验证是否有权访问(在本例中 为是否登录)。若未登录则需要跳转到登录页面,否则响应用户请求。

2.解决方案

1.在 maven 配置文件中 添加AOP依赖

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
   </dependency>

2.编写验证器

  public class SignVerification {// 登录验证
    ServletRequestAttributes attributes =   (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    HttpSession session= request.getSession();
    public boolean Verification(){
        if (session.getAttribute("Admin") == null)
        {
            return false;
        }
        return true;
    }

   }

3.编写拦截器 VerificationAspect

@Aspect
@Component // 放入spring 容器
public class VerificationAspect {
private final static Logger logger =               LoggerFactory.getLogger(VerificationAspect.class);

//拦截条件
@Pointcut("execution(public * com.freeyun.demo.Controller.ShowInfoController.*(..))")
public void log() {}
@Around("log()")
public Object signVerification(ProceedingJoinPoint pjp) throws Throwable{
    SignVerification v = new SignVerification();
    if (v.Verification())//已经登录
    {
        return pjp.proceed();//继续执行被拦截的方法
    }
    else {//未登录
        //构建JSON 
        String response = "{\"signin\":0}";
        return response;
    }
}
  }

4.前端对收到的JOSN数据进行判断,若未登录则跳转到登录页面

 if(response.signin == 0)
        {

            window.location.href = "登录页面的路径";
        }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,692评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,826评论 0 23
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,781评论 3 20
  • 眼皮越来越沉…… 从一堆杂念中回归呼与吸时,惊觉刚才的呼吸急促…… 看见,被茂密的森林深处的树冠,切割成一块儿遥远...
    juan子阅读 288评论 0 0
  • 朴瑾惠被开除党籍,系韩国首位被强行退党的前总统。 看到这条新闻,深思良久,朴一生坎坷,艰辛时的温暖的慰藉,却在成功...
    May74阅读 368评论 0 0

友情链接更多精彩内容