惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
H
Help Net Security
B
Blog RSS Feed
G
Google Developers Blog
博客园 - 司徒正美
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
P
Proofpoint News Feed
小众软件
小众软件
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
V
V2EX
月光博客
月光博客
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
Arctic Wolf
Help Net Security
Help Net Security
Schneier on Security
Schneier on Security
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Palo Alto Networks Blog
T
Tenable Blog
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Y
Y Combinator Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
NISL@THU
NISL@THU
GbyAI
GbyAI
博客园 - Franky
S
Secure Thoughts
有赞技术团队
有赞技术团队
PCI Perspectives
PCI Perspectives
U
Unit 42

博客园 - 007少侠

使用Recaf编辑汇编代码直接修改java的编译代码class Centos Linux 更换源,原官方源已经不再提供服务 如何在Docker容器中的Linux系统(Ubuntu + Centos Linux)里面使用systemctl Centos Linux 7 搭建邮件服务器(postfix + dovecot) Ubuntu Linux 搭建邮件服务器(postfix + dovecot) 【WSL2】在Windows通过自定义域名访问wsl2中的服务 Python pip pip3 源设置成国内源,阿里云源,清华大学源 利用云服务器实现内网穿透(frp),开启个人电脑(windows)可远程桌面访问 使用docker安装centos7并挂载主机目录 Rust交叉编译Mac编译Linux/Windows平台 Linux下安装dart sdk并配置环境变量 Linux下安装JDK-openj9并配置环境变量 centos7 安装 bbr加速 linux环境常用命令和java/jvm常用命令 mybatis plus + druid多数据源(使用dynamic实现) docker将容器打包成镜像并传输至其他服务器部署(可用于容器重新run) .gitignore == git添加忽略不生效解决方案 jenkins自动构建项目源码git pull时出现冲突的终极解决方案(git远程覆盖本地分支) mysql查询,根据时间查询:几天前、几天内的数据
spring boot 使用aop实现拦截器
007少侠 · 2020-03-31 · via 博客园 - 007少侠

实现功能:对某个目录下所有请求接口进行拦截判断,如检测登录状态等。

需要pom.xml引入aop:

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

具体实现:

package com.tuijie.gainguest.common.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.tuijie.gainguest.common.enums.JsonCodeEnum;
//import com.tuijie.gainguest.common.result.JsonResp;
//import com.tuijie.gainguest.controller.Constants;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;

/**
 * @Author: admin
 * @Description: 切面AOP
 * @Date Created in 9:39 AM 2020/3/31
 * @Modified By:
 */
@Aspect
@Component
@Slf4j //log日志,可选
public class SysPointcut {
    // 放行接口
    private final static String[] excludePathPatterns = {
            "/api/sysUserAdmin/login",
            "/api/sysUserAdmin/logout",
    };
    private static final SerializerFeature[] serializerFeatures;
    static {
        serializerFeatures = new SerializerFeature[] {
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullListAsEmpty
        };
    }

    /**
     * 后台管理切面AOP
     * 定义切入点,切入点为com.tuijie.gainguest.controller.admin包下所有类-接口
     * 通过@Pointcut注解声明频繁使用的切点表达式
     */
    @Pointcut("execution(* com.tuijie.gainguest.controller.admin..*.*(..)))")
    public void BrokerAspect() {}

    @Around("BrokerAspect()")
    public Object beforeExec(ProceedingJoinPoint pjp) throws Throwable {
        //获取request信息
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        assert sra != null;
        HttpServletRequest request = sra.getRequest();
        HttpSession session = request.getSession();

        String uri = request.getRequestURI();
        if (!Arrays.asList(excludePathPatterns).contains(uri)) {
            Object admin = session.getAttribute(/*Constants.ACCOUNT_SESSION*/"test");
//            JsonResp jsonResp;
            if (admin == null) {
                log.warn("未登录: [{}] [{}]", session.getId(), uri);
//                jsonResp = JsonResp.fail(JsonCodeEnum.OVERTIME.getMessage(), JsonCodeEnum.OVERTIME.getCode());
                HttpServletResponse response = sra.getResponse();
                assert response != null;
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json; charset=UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write(JSON.toJSONString(/*jsonResp*/new HashMap<String, Object>(){{put("id", 1);}}, serializerFeatures));
                writer.flush();
                writer.close();
                return null;
            }
        }

        return pjp.proceed();
    }

}