我是靠谱客的博主 热情河马,最近开发中收集的这篇文章主要介绍SpringSecurity的OAuth2.0的登录日志的处理(使用AOP),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原理:

用AOP的方式将

@RequestMapping(value = "/oauth/token", method=RequestMethod.POST)

org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken

用到的maven(获取请求useragent的信息的包):

 <!--获取请求主机的useragent的信息的包-->
        <dependency>
            <groupId>eu.bitwalker</groupId>
            <artifactId>UserAgentUtils</artifactId>
            <version>1.21</version>
        </dependency>

LoginHttpRequestAop : 

package application.aop;

import eu.bitwalker.useragentutils.BrowserType;
import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
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 java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.TimeZone;

/**
 * @author: wtl
 * @License: (C) Copyright 2021, wtl Corporation Limited.
 * @Contact: 1050100468@qq.com
 * @Date: 2021/8/1 7:08
 * @Version: 1.0
 * @Description:
 */
@Component
@Aspect
public class LoginHttpRequestAop {

    @Pointcut("execution(* org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))")
    public void pointCut(){
    }

    @After("pointCut()")
    public void loginAop(JoinPoint joinPoint){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        HttpServletResponse httpServletResponse = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        
        String userAgentString = httpServletRequest.getHeader("User-Agent");
        UserAgent userAgent = UserAgent.parseUserAgentString(userAgentString);
        BrowserType browserType = userAgent.getBrowser().getBrowserType(); //浏览器类型

        OperatingSystem operatingSystem = userAgent.getOperatingSystem(); // 操作系统信息
        DeviceType deviceType = operatingSystem.getDeviceType(); // 设备类
        String role = httpServletRequest.getRemoteUser();
        String remoteRequestIp = getRemoteRequestIp(httpServletRequest);
        
        System.out.println(httpServletResponse.getStatus());
        System.out.println(remoteRequestIp);
        System.out.println(browserType);
        System.out.println(operatingSystem);
        System.out.println(deviceType);
        System.out.println(role);
        System.out.println(simpleDateFormat.format(System.currentTimeMillis()));
    }


    @AfterThrowing(value = "pointCut()",throwing = "throwable")
    public void loginAfterThrowingAop(JoinPoint joinPoint,Throwable throwable){
        System.out.println(throwable);
    }


    //获取请求IP
    private String getRemoteRequestIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;

    }

}

最后

以上就是热情河马为你收集整理的SpringSecurity的OAuth2.0的登录日志的处理(使用AOP)的全部内容,希望文章能够帮你解决SpringSecurity的OAuth2.0的登录日志的处理(使用AOP)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部