def authorize(power: str, log: bool = False): def decorator(func): @login_required def wrapper(request, *args, **kwargs): if not power in request.session.get('permissions'): if log: exec_log(request=request, is_access=False, desc='没有权限') if request.method == 'GET': return res_josn_data.fail_api(msg="权限不足!") if log and request.method == "POST": exec_log(request=request, is_access=True, desc=str(dict(request.POST))) return func(request, *args, **kwargs) return wrapper return decorator
时间: 2023-11-26 16:04:21 浏览: 32
这是一个装饰器函数,用于检查用户权限的装饰器。它接受一个权限字符串和一个布尔型参数 log,如果 log 为 True,则会记录用户权限检查的结果。它返回一个装饰器函数 decorator,decorator 接受一个函数 func 作为参数,并返回一个新的函数 wrapper,wrapper 实现了对用户权限的检查和记录,并调用原始的函数 func。
具体来说,当一个请求到达被装饰的函数时,wrapper 首先使用@login_required 装饰器检查用户是否已经登录,如果没有登录则返回登录页面。接着,wrapper 检查用户是否有指定的权限,如果没有则返回一个错误信息。如果 log 开启,则会记录检查结果,以及 POST 请求的数据(如果请求是 POST 请求)。最后,wrapper 调用原始的函数并返回其结果。
这个装饰器函数可以用于 Django 框架中的视图函数或者其他需要权限控制的函数。
相关问题
<security:authorize access="hasPermission 中,hasPermission 怎么传参数
在 Spring Security 中,`hasPermission` 表达式需要传入两个参数:目标对象和权限表达式。常见的用法是在方法或类上使用注解方式授权,例如:
```java
@PreAuthorize("hasPermission(#book, 'read')")
public void read(Book book) {
// ...
}
```
其中,`#book` 表示方法参数中的 `Book` 对象,`'read'` 是权限表达式。
如果你需要在 XML 配置中使用 `hasPermission` 表达式,可以按照下面的方式传入参数:
```xml
<security:authorize access="hasPermission(#book, 'read')">
<!-- ... -->
</security:authorize>
```
这里的 `#book` 同样表示目标对象,`'read'` 是权限表达式。注意,如果你在 XML 中使用 `hasPermission` 表达式,需要在 Spring 配置文件中开启 Spring Security 的表达式语言支持,例如:
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<security:global-method-security pre-post-annotations="enabled"/>
<!-- ... -->
</beans>
```
这里的 `pre-post-annotations="enabled"` 表示开启方法级别的注解授权支持。
jsp 页面<security:authorize access="@myHasPermission()"> myHasPermission无法跳转到具体的方法
`@myHasPermission()` 是一个 Spring Security 中的 SpEL 表达式,它用于检查当前用户是否有特定权限来访问页面或资源。在这个表达式中,`myHasPermission` 是一个自定义的方法名,用于检查当前用户是否有特定权限。
要使 `@myHasPermission()` 能够正确地跳转到具体的方法,需要在 Spring Security 配置文件中对其进行定义。具体来说,需要定义一个 `MethodSecurityExpressionHandler` Bean,并将其与 `GlobalMethodSecurityConfiguration` 配置类关联起来,如下所示:
```java
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new CustomPermissionEvaluator());
return expressionHandler;
}
}
```
在上面的配置中,`DefaultMethodSecurityExpressionHandler` 是 Spring Security 默认的表达式处理器,`CustomPermissionEvaluator` 是自定义的权限检查器,用于实现 `myHasPermission` 方法的具体逻辑。在 `CustomPermissionEvaluator` 中,需要实现 `hasPermission` 方法,用于检查当前用户是否有特定权限。例如:
```java
public class CustomPermissionEvaluator implements PermissionEvaluator {
@Autowired
private UserService userService;
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
User user = userService.getCurrentUser();
String permissionStr = permission.toString();
// 根据 permissionStr 判断用户是否有特定权限
return user.hasPermission(permissionStr);
}
@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
// 这个方法一般用于检查某个实体对象是否有特定权限
return false;
}
}
```
在 `hasPermission` 方法中,首先获取当前用户,然后根据传入的 `permission` 参数进行判断,判断用户是否有特定的权限。如果有,则返回 `true`,否则返回 `false`。
在上面的配置完成后,`@myHasPermission()` 应该就能够正确地跳转到具体的方法了。