✨ 添加新特性。@Inner 内部调用校验
This commit is contained in:
1 parent
d32090bf51
commit
444ccb4bd1
17 files changed
+169
-40
No files matched your search
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/11/26
|
||||
* <p>
|
||||
* 服务调用不鉴权注解
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Inner {
|
||||
|
||||
/**
|
||||
* 是否AOP统一处理
|
||||
*
|
||||
* @return false, true
|
||||
*/
|
||||
boolean value() default true;
|
||||
|
||||
/**
|
||||
* 需要特殊判空的字段(预留)
|
||||
*
|
||||
* @return {}
|
||||
*/
|
||||
String[] field() default {};
|
||||
}
|
||||
+11
-5
@@ -19,10 +19,10 @@ package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.core.SpringSecurityMessageSource;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
@@ -51,7 +51,10 @@ public class GlobalExceptionHandlerResolver {
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public R handleGlobalException(Exception e) {
|
||||
log.error("全局异常信息 ex={}", e.getMessage(), e);
|
||||
return new R<>(e);
|
||||
return R.builder()
|
||||
.msg(e.getLocalizedMessage())
|
||||
.code(CommonConstant.FAIL)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,9 +66,12 @@ public class GlobalExceptionHandlerResolver {
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public R handleAccessDeniedException(AccessDeniedException e) {
|
||||
log.error("拒绝授权异常信息 ex={}",e.getLocalizedMessage(),e);
|
||||
String msg = SpringSecurityMessageSource.getAccessor()
|
||||
.getMessage("AbstractAccessDecisionManager.accessDenied"
|
||||
, e.getMessage());
|
||||
log.error("拒绝授权异常信息 ex={}", msg, e);
|
||||
return R.builder()
|
||||
.msg(e.getLocalizedMessage())
|
||||
.msg(msg)
|
||||
.code(CommonConstant.FAIL)
|
||||
.build();
|
||||
}
|
||||
@@ -80,7 +86,7 @@ public class GlobalExceptionHandlerResolver {
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public R handleBodyValidException(MethodArgumentNotValidException exception) {
|
||||
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
|
||||
log.error("参数绑定异常,ex = {}",fieldErrors.get(0).getDefaultMessage());
|
||||
log.error("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
|
||||
return R.builder()
|
||||
.msg(fieldErrors.get(0).getDefaultMessage())
|
||||
.code(CommonConstant.FAIL)
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.security.annotation.Inner;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/11/26
|
||||
* <p>
|
||||
* 服务间接口不鉴权处理逻辑
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class PigxSecurityInnerAspect {
|
||||
private final HttpServletRequest request;
|
||||
|
||||
@Around("@annotation(inner)")
|
||||
public Object around(ProceedingJoinPoint point, Inner inner) throws Throwable {
|
||||
String header = request.getHeader(SecurityConstants.FROM);
|
||||
if (inner.value() && !StrUtil.equals(SecurityConstants.FROM_IN, header)) {
|
||||
log.warn("访问接口 {} 没有权限", point.getSignature().getName());
|
||||
throw new AccessDeniedException("Access is denied");
|
||||
}
|
||||
return point.proceed();
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -72,7 +72,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
*/
|
||||
@Override
|
||||
public UserDetails loadUserBySocial(String inStr) throws UsernameNotFoundException {
|
||||
return getUserDetails(remoteUserService.social(inStr));
|
||||
return getUserDetails(remoteUserService.social(inStr, SecurityConstants.FROM_IN));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user