在我的Play
应用程序中,我为Swagger定义了AuthorizationFilter
,如下所示:
class AuthorizationFilter extends SwaggerSpecFilter {
def isOperationAllowed(
operation: Operation,
api: ApiDescription,
params: java.util.Map[String, java.util.List[String]],
cookies: java.util.Map[String, String],
headers: java.util.Map[String, java.util.List[String]]): Boolean = {
checkKey(params, headers) match {
case true => true
case false => {
Logger("swagger").debug(s"authenticated: false - method: ${operation.method} - path: ${api.path}")
if (operation.method == "GET" && api.path.indexOf("/admin") != -1) true
else if (operation.method == "GET" && api.path.indexOf("/auth/users") != -1) true
else false
}
}
}
def isParamAllowed(
parameter: Parameter,
operation: Operation,
api: ApiDescription,
params: java.util.Map[String, java.util.List[String]],
cookies: java.util.Map[String, String],
headers: java.util.Map[String, java.util.List[String]]): Boolean = {
val isAuthorized = checkKey(params, headers)
if (parameter.paramAccess == Some("internal") && !isAuthorized) false
else true
}
def checkKey(
params: java.util.Map[String, java.util.List[String]],
headers: java.util.Map[String, java.util.List[String]]): Boolean = {
val apiKey = params.containsKey("api_key") match {
case true => Some(params.get("api_key").get(0))
case _ => {
headers.containsKey("api_key") match {
case true => Some(headers.get("api_key").get(0))
case _ => None
}
}
}
apiKey match {
case Some(key) if (key == "special-key") => true
case _ => false
}
}
}
然后,我在conf\application.conf
中添加了以下配置:
api.version = "0.1"
swagger {
api {
basepath = "http://localhost:9000"
}
security {
filter = "security.apidocs.AuthorizationFilter"
}
}
...但AuthorizationFilter
永远不会被调用。我该如何启用它?
答案 0 :(得分:1)
确保您的AuthorizationFilter首先在您的文件中定义了包security.apidocs。