¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang 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 dreamlu.net 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: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package org.springblade.core.secure.aspect; |
| | | |
| | | import org.aspectj.lang.ProceedingJoinPoint; |
| | | import org.aspectj.lang.annotation.Around; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.reflect.MethodSignature; |
| | | import org.springblade.core.secure.annotation.PreAuth; |
| | | import org.springblade.core.secure.auth.AuthFun; |
| | | import org.springblade.core.secure.exception.SecureException; |
| | | import org.springblade.core.tool.api.ResultCode; |
| | | import org.springblade.core.tool.utils.ClassUtil; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ApplicationContextAware; |
| | | import org.springframework.context.expression.BeanFactoryResolver; |
| | | import org.springframework.core.MethodParameter; |
| | | import org.springframework.expression.Expression; |
| | | import org.springframework.expression.ExpressionParser; |
| | | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| | | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| | | import org.springframework.lang.NonNull; |
| | | |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | | * AOP é´æ |
| | | * |
| | | * @author Chill |
| | | */ |
| | | @Aspect |
| | | public class AuthAspect implements ApplicationContextAware { |
| | | |
| | | /** |
| | | * 表达å¼å¤ç |
| | | */ |
| | | private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser(); |
| | | |
| | | /** |
| | | * å æ¹æ³ å ç±»ä¸ç @PreAuth 注解 |
| | | * |
| | | * @param point åç¹ |
| | | * @return Object |
| | | * @throws Throwable 没ææéçå¼å¸¸ |
| | | */ |
| | | @Around( |
| | | "@annotation(org.springblade.core.secure.annotation.PreAuth) || " + |
| | | "@within(org.springblade.core.secure.annotation.PreAuth)" |
| | | ) |
| | | public Object preAuth(ProceedingJoinPoint point) throws Throwable { |
| | | if (handleAuth(point)) { |
| | | return point.proceed(); |
| | | } |
| | | throw new SecureException(ResultCode.UN_AUTHORIZED); |
| | | } |
| | | |
| | | /** |
| | | * å¤çæé |
| | | * |
| | | * @param point åç¹ |
| | | */ |
| | | private boolean handleAuth(ProceedingJoinPoint point) { |
| | | MethodSignature ms = (MethodSignature) point.getSignature(); |
| | | Method method = ms.getMethod(); |
| | | // 读åæé注解ï¼ä¼å
æ¹æ³ä¸ï¼æ²¡æå读åç±» |
| | | PreAuth preAuth = ClassUtil.getAnnotation(method, PreAuth.class); |
| | | // å¤æè¡¨è¾¾å¼ |
| | | String condition = preAuth.value(); |
| | | if (StringUtil.isNotBlank(condition)) { |
| | | Expression expression = EXPRESSION_PARSER.parseExpression(condition); |
| | | // æ¹æ³åæ°å¼ |
| | | Object[] args = point.getArgs(); |
| | | StandardEvaluationContext context = getEvaluationContext(method, args); |
| | | return expression.getValue(context, Boolean.class); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * è·åæ¹æ³ä¸çåæ° |
| | | * |
| | | * @param method æ¹æ³ |
| | | * @param args åé |
| | | * @return {SimpleEvaluationContext} |
| | | */ |
| | | private StandardEvaluationContext getEvaluationContext(Method method, Object[] args) { |
| | | // åå§åSp el表达å¼ä¸ä¸æï¼å¹¶è®¾ç½® AuthFun |
| | | StandardEvaluationContext context = new StandardEvaluationContext(new AuthFun()); |
| | | // è®¾ç½®è¡¨è¾¾å¼æ¯æspring bean |
| | | context.setBeanResolver(new BeanFactoryResolver(applicationContext)); |
| | | for (int i = 0; i < args.length; i++) { |
| | | // è¯»åæ¹æ³åæ° |
| | | MethodParameter methodParam = ClassUtil.getMethodParameter(method, i); |
| | | // è®¾ç½®æ¹æ³ åæ°ååå¼ ä¸ºsp elåé |
| | | context.setVariable(methodParam.getParameterName(), args[i]); |
| | | } |
| | | return context; |
| | | } |
| | | |
| | | private ApplicationContext applicationContext; |
| | | |
| | | @Override |
| | | public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { |
| | | this.applicationContext = applicationContext; |
| | | } |
| | | |
| | | } |