提交 | 用户 | 时间
|
71e81e
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.base.auth.aop; |
|
17 |
|
|
18 |
import cn.stylefeng.guns.base.auth.annotion.Permission; |
|
19 |
import cn.stylefeng.guns.base.auth.exception.PermissionException; |
|
20 |
import cn.stylefeng.guns.base.auth.service.AuthService; |
|
21 |
import org.aspectj.lang.ProceedingJoinPoint; |
|
22 |
import org.aspectj.lang.annotation.Around; |
|
23 |
import org.aspectj.lang.annotation.Aspect; |
|
24 |
import org.aspectj.lang.annotation.Pointcut; |
|
25 |
import org.aspectj.lang.reflect.MethodSignature; |
|
26 |
import org.springframework.beans.factory.annotation.Autowired; |
|
27 |
import org.springframework.core.annotation.Order; |
|
28 |
|
|
29 |
import java.lang.reflect.Method; |
|
30 |
|
|
31 |
/** |
|
32 |
* 权限检查的aop |
|
33 |
* |
|
34 |
* @author fengshuonan |
|
35 |
* @date 2017-07-13 21:05 |
|
36 |
*/ |
|
37 |
@Aspect |
|
38 |
@Order(200) |
|
39 |
public class PermissionAop { |
|
40 |
|
|
41 |
@Autowired |
|
42 |
private AuthService authService; |
|
43 |
|
|
44 |
@Pointcut(value = "@annotation(cn.stylefeng.guns.base.auth.annotion.Permission)") |
|
45 |
private void cutPermission() { |
|
46 |
|
|
47 |
} |
|
48 |
|
|
49 |
@Around("cutPermission()") |
|
50 |
public Object doPermission(ProceedingJoinPoint point) throws Throwable { |
|
51 |
MethodSignature ms = (MethodSignature) point.getSignature(); |
|
52 |
Method method = ms.getMethod(); |
|
53 |
Permission permission = method.getAnnotation(Permission.class); |
|
54 |
String[] permissions = permission.value(); |
|
55 |
if (permissions.length == 0) { |
|
56 |
|
|
57 |
//检查全体角色 |
|
58 |
boolean result = authService.checkAll(); |
|
59 |
if (result) { |
|
60 |
return point.proceed(); |
|
61 |
} else { |
|
62 |
throw new PermissionException(); |
|
63 |
} |
|
64 |
|
|
65 |
} else { |
|
66 |
|
|
67 |
//检查指定角色 |
|
68 |
boolean result = authService.check(permissions); |
|
69 |
if (result) { |
|
70 |
return point.proceed(); |
|
71 |
} else { |
|
72 |
throw new PermissionException(); |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
} |