micronaut自定义securityrule检查用户是否经过身份验证?

q9rjltbz  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(463)

我有以下海关安全规则

@Singleton
public class AdminHandler implements SecurityRule {
    @Override
    public SecurityRuleResult check(HttpRequest<?> request, @Nullable RouteMatch<?> routeMatch, @Nullable Map<String, Object> claims) {
        if (routeMatch instanceof MethodBasedRouteMatch) {
            MethodBasedRouteMatch methodBasedRouteMatch = (MethodBasedRouteMatch) routeMatch;
            if (methodBasedRouteMatch.hasAnnotation(IAdminRequirement.class)) {
                AnnotationValue<IAdminRequirement> requiredPermissionAnnotation = methodBasedRouteMatch.getAnnotation(IAdminRequirement.class);
                // Get parameters from annotation on method
                Optional<String> resourceIdName = requiredPermissionAnnotation.stringValue("resourceIdName");
                Optional<String> permission = requiredPermissionAnnotation.stringValue("permission");
                if (permission.isPresent() && resourceIdName.isPresent() && claims != null) {
                    // Use name of parameter to get the value passed in as an argument to the method
                    String resourceId = methodBasedRouteMatch.getVariableValues().get(resourceIdName.get()).toString();
                    // Get claim from jwt using the resource ID
                    Object permissionForResource = ((Map) claims.get("https://your-domain.com/claims")).get(resourceId);
                    if (permissionForResource != null && permissionForResource.equals(permission.get())) {
                        // if the permission exists and it's equal, allow access
                        return SecurityRuleResult.ALLOWED;
                    }
                }
            }
        }
        return SecurityRuleResult.UNKNOWN;
    }
}

在httprequest中,我找不到检查用户是否经过身份验证的方法,声明是否存在?
如何检查用户是否经过身份验证?

@Post
    @IAdminRequirement(resourceIdName = "IdentityServer", permission = "read")
    Maybe<HttpResponse<?>> post(@Body ProductViewModel model);
g0czyy6m

g0czyy6m1#

如果声明不为空,则请求将通过身份验证

相关问题