gpt4 book ai didi

java - Swagger + Spring安全性-基于角色的隐藏方法

转载 作者:行者123 更新时间:2023-12-03 21:00:01 25 4
gpt4 key购买 nike

我有一个具有不同使用者的api。我希望他们根据他们在Spring Security中的角色获得相关的文档。

例如

Api操作A仅限角色A和角色B

Api操作B仅限角色B

api操作C向所有人开放

我正在使用springfox,spring 4,spring rest,security

我知道有一个称为@ApiIgnore的注释,也许可以利用。

这是可能吗?

最佳答案

经过一番搜索,我发现网络上没有办法解决此问题。所以我用自己的解决方案解决了。

我编写了一个过滤器,用于修改响应并删除用户无权访问的api。

过滤器是这样的:

 @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String url = httpServletRequest.getRequestURI();
if (url.contains("v2/api-docs")) {
CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);
chain.doFilter(httpServletRequest, wrapper);
refineApiBaseOnACL(wrapper);
return;
}
chain.doFilter(httpServletRequest, response);
}

要修改响应,您应该遵循 this link

然后,我们需要优化生成的api:
private List<String> httpCommands = List.of("get", "head", "post", "put", "delete", "options", "patch");

public void refineApiBaseOnACL(CharResponseWrapper wrapper) {
try {
byte[] bytes = wrapper.getByteArray();

if (wrapper.getContentType().contains("application/json")) {
String out = refineContentBaseOnACL(new String(bytes));
wrapper.getResponse().getOutputStream().write(out.getBytes());
} else {
wrapper.getResponse().getOutputStream().write(bytes);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private String refineContentBaseOnACL(String originalContent) {
JSONObject object = new JSONObject(originalContent);
JSONObject paths = object.getJSONObject("paths");
JSONArray tags = object.getJSONArray("tags");

Iterator keys = paths.keys();
Set<String> toRemovePath = new HashSet<>();
Set<Integer> toRemoveTags = new HashSet<>();
Set<String> tagSet = new HashSet<>();
while (keys.hasNext()) {
String key = (String) keys.next();
String[] split = key.split("/");
if (!getAccessHandler().checkAccessRest(split[1], split[2]))
toRemovePath.add(key);
else {
for (String httpCommand : httpCommands)
if (paths.getJSONObject(key).has(httpCommand)) {
JSONObject command = paths.getJSONObject(key).getJSONObject(httpCommand);
JSONArray tagsArray = command.getJSONArray("tags");
for (int i = 0; i < tagsArray.length(); i++)
tagSet.add(tagsArray.getString(i));
}
}
}

for (String key : toRemovePath)
paths.remove(key);

for (int i = 0; i < tags.length(); i++)
if (!tagSet.contains(tags.getJSONObject(i).getString("name")))
toRemoveTags.add(i);

List<Integer> sortedTags = new ArrayList<>(toRemoveTags);
sortedTags.sort(Collections.reverseOrder());
for (Integer key : sortedTags)
tags.remove(key);


Pattern modelPattern = Pattern.compile("\"#/definitions/(.*?)\"");
Set<String> modelSet = new HashSet<>();
Matcher matcher = modelPattern.matcher(object.toString());
while (matcher.find())
modelSet.add(matcher.group(1));

JSONObject definitions = object.getJSONObject("definitions");
Set<String> toRemoveModel = new HashSet<>();
Iterator definitionModel = definitions.keys();
while (definitionModel.hasNext()) {
String definition = (String) definitionModel.next();
boolean found = false;
for (String model : modelSet)
if (definition.equals(model)) {
found = true;
break;
}
if (!found)
toRemoveModel.add(definition);
}

for (String model : toRemoveModel) {
definitions.remove(model);
}

return object.toString();
}

在我的情况下,我有一个 AccessHandler,它使用URL处理访问控制。您应该在逻辑上写本节。
对于spring安全角色,您可以使用如下代码:
request.isUserInRole("Role_A");

关于java - Swagger + Spring安全性-基于角色的隐藏方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41445222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com