SpringBoot 사용자 정의 주석 권한 제어

Spring 사용자 정의 주해 와 실현, op 기반 구현, 권한 제어 사용자 정의 주해 실현
권한 제 어 를 실현 하면 shiro 를 선택 할 수 있 지만, shiro 가 무 거 워 서 사용 할 필요 가 없고, springBoot 의 op 방식 으로 사용자 정의 주 해 를 실현 할 수 있 습 니 다
1. 사용자 정의 주석 을 정의 합 니 다.
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PermissionNeed {
    String value() default "";
}

2. aop 의 사용
@Aspect
@Component
public class LovePiAspect {

    @Pointcut("@annotation(permissionNeed)")
    public  void annotationPointCut(PermissionNeed permissionNeed) {

    }

    @Before("annotationPointCut(permissionNeed)")
    public void before(JoinPoint joinPoint,PermissionNeed permissionNeed){
        MapperMethod.MethodSignature sign =  (MapperMethod.MethodSignature)joinPoint.getSignature();
        System.out.print("    :"+"     ");
        String permission = permissionNeed.value();
        //      
        //     
        Set userPrivilegeSet = OAuthUserContext.getUserPrivilegeSet();
        //      ,           
        if(!userPrivilegeSet.contains(permission)){
            throw new AppException(ApiResponseCode.PROMPT.get(),"           ");
        }
    }

}

userPrivailegeSet 는 요청 이나 로그 인 할 때마다 모든 권한 을 가 져 오고 threadLocal 을 스 레 드 로 저장 할 수 있 습 니 다.권한 이 만족 하지 않 아 사용자 정의 이상 을 던 집 니 다.
3. 사용자 정의 주석 사용
    @ResponseBody
    @RequestMapping(value = "/",method = RequestMethod.GET)
    @PermissionNeed(value = "sys:user:login")
    public String home() {
        return "Hello World!";
    }

좋은 웹페이지 즐겨찾기