*IT SpringAOP:足跡第十八步瞭解SpringAOP(實現許可權認證、日誌)
方案1:
1.1)準備切面,實現許可權認證、日誌
@Aspect
@Component
public class LogAspect {
Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Value("${spring.application.name}")
private String serverName;
@Pointcut("@annotation(com.xxx.annotation.LogAnnotation)")
public void beforePointCut () {}
@Before("beforePointCut()")
public void before (JoinPoint joinPoint) {
if (joinPoint == null) {
return;
}
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] methodArgs = joinPoint.getArgs();
Method method = ((MethodSignature)joinPoint.getSignature()).getMethod();
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
String logType = logAnnotation.type();
String logValue = logAnnotation.value();
String userName = SecurityUtils.getCurrentUserLogin();
if (StringUtils.isEmpty(userName)) {
userName = "使用者未登入";
}
String ip = getIpAddress(request);
StringBuilder args = new StringBuilder();
for (Object o : methodArgs) {
args.append(o);
args.append("&");
}
if (args.length() > 0) {
args = args.delete(args.length()-1,args.length());
}
logger.info(userName+"|"+className+"|"+methodName+"|"+ip+"|"+logType+"|"+args.toString()+"|"+serverName+"|"+logValue);
}
public static String getIpAddress(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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
1.2)生成切點註解
package com.xxx.annotation;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAnnotation {
String value() default "";
String type() default "";
}
1.3)在介面Action下add方法處呼叫切面
@RestController
@RequestMapping("/api")
public class AdminLogController {
@Autowired
private AdminLogService adminLogService;
@LogAnnotation(type = Constants.LOG_TYPE_ADD,value = "admin的add日誌")
@PostMapping(value = "/setAdminLog")
@ApiOperation(value="admin的add日誌", notes="新增資料")
public ResponseEntity<ResultStatus> addUserLog(@RequestBody AdminLogEntity adminLogEntity){
ResultStatus resultStatus = adminLogService.save(adminLogEntity);
return new ResponseEntity<ResultStatus>(resultStatus, HttpStatus.OK);
}
}
方案2:
2.1)準備切面(@Aspect)與切點@Pointcut(介面Action);
2.2)在介面Action下add方法處呼叫切面(AOP核心是攔截);
3)DTO
3.1)Result
3.2)介面中調取統一結果集Result
3.3)json規範
相關文章
- SpringAop實現許可權校驗與日誌列印Spring
- Hadoop 許可權認證Hadoop
- springAOP的三種實現方式Spring
- SpringAOP_構造注入實現Spring
- SpringAOP_設定注入實現Spring
- springAOPSpring
- 超實用的SpringAOP實戰之日誌記錄Spring
- SpringBoot整合Spring security JWT實現介面許可權認證Spring BootJWT
- springboot + shiro 實現登入認證和許可權控制Spring Boot
- spring security許可權認證Spring
- artisan日誌 root 許可權解決辦法
- SpringAOP分析Spring
- springAop原理Spring
- 許可權控制及AOP日誌
- spring boot 利用註解實現許可權驗證Spring Boot
- Ocelot閘道器+IdentityServer4實現API許可權認證IDEServerAPI
- Laravel Daily 日誌許可權問題LaravelAI
- SpringAOP的使用Spring
- GoFrame 框架使用 casbin 許可權認證GoFrame框架
- DRF-認證許可權頻率
- SpringSecurity許可權管理系統實戰—二、日誌、介面文件等實現SpringGse
- 代理模式——結合SpringAOP講解模式Spring
- Django實戰1-許可權管理功能實現-03:使用者認證Django
- Laravel 框架的日誌許可權問題Laravel框架
- Laravel 日誌檔案許可權問題Laravel
- Laravel 日誌有時候有許可權有時候沒有許可權?Laravel
- ThinkJS入門+例項(實現認證許可權等基本功能)JS
- 更深入地瞭解 Linux 許可權Linux
- 如何用 Vue 實現前端許可權控制(路由許可權 + 檢視許可權 + 請求許可權)Vue前端路由
- Spring boot 入門(四):整合 Shiro 實現登陸認證和許可權管理Spring Boot
- 記一次 Laravel日誌許可權許可權問題(定時器導致)Laravel定時器
- SpringAOP的原始碼解析Spring原始碼
- 如何優雅的使用切面和註解實現許可權驗證
- 深入Django:使用者認證與許可權控制實戰指南Django
- ThinkPHP6 寫入日誌許可權報錯PHP
- Laravel實現許可權控制Laravel
- Hyperf 完整專案-1-jwt 許可權認證JWT
- SpringAop--Java面試題SpringJava面試題