ip也打印出来
This commit is contained in:
1 parent
606c50139c
commit
65a793efcc
3 files changed
+32
-2
No files matched your search
@@ -29,13 +29,15 @@ public class AdminAccessConfiguration {
|
|||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||||
FilterChain chain) throws ServletException, IOException {
|
FilterChain chain) throws ServletException, IOException {
|
||||||
// 仅使用 TCP 对端地址,绝不直接信任客户端可伪造的 X-Forwarded-For/X-Real-IP。
|
// 仅使用 TCP 对端地址,绝不直接信任客户端可伪造的 X-Forwarded-For/X-Real-IP。
|
||||||
if (!enabled || whitelist.allows(paths.getPathWithinApplication(request), request.getRemoteAddr())) {
|
String remoteAddress = request.getRemoteAddr();
|
||||||
|
if (!enabled || whitelist.allows(paths.getPathWithinApplication(request), remoteAddress)) {
|
||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||||
response.setContentType("application/json;charset=UTF-8");
|
response.setContentType("application/json;charset=UTF-8");
|
||||||
response.getWriter().write("{\"code\":403,\"msg\":\"来源 IP 不在接口访问白名单中\"}");
|
response.getWriter().write("{\"code\":403,\"msg\":\"来源 IP "
|
||||||
|
+ remoteAddress + " 不在接口访问白名单中\"}");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
FilterRegistrationBean<OncePerRequestFilter> registration = new FilterRegistrationBean<>(filter);
|
FilterRegistrationBean<OncePerRequestFilter> registration = new FilterRegistrationBean<>(filter);
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ public class AdminAccessWhitelistTest {
|
|||||||
new AdminAccessConfiguration().adminAccessFilter(whitelist, true).getFilter()
|
new AdminAccessConfiguration().adminAccessFilter(whitelist, true).getFilter()
|
||||||
.doFilter(request, response, chain);
|
.doFilter(request, response, chain);
|
||||||
assertEquals(403, response.getStatus());
|
assertEquals(403, response.getStatus());
|
||||||
|
assertTrue(response.getContentAsString().contains("来源 IP 203.0.113.9 不在接口访问白名单中"));
|
||||||
|
assertFalse(response.getContentAsString().contains("127.0.0.1"));
|
||||||
verify(whitelist).allows("/system/user/list", "203.0.113.9");
|
verify(whitelist).allows("/system/user/list", "203.0.113.9");
|
||||||
verifyZeroInteractions(chain);
|
verifyZeroInteractions(chain);
|
||||||
new AdminAccessConfiguration().adminAccessFilter(whitelist, false).getFilter()
|
new AdminAccessConfiguration().adminAccessFilter(whitelist, false).getFilter()
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
-- 将全局接口白名单页面加入系统管理;重复执行不会创建重复菜单。
|
||||||
|
INSERT INTO sys_menu
|
||||||
|
(menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type,
|
||||||
|
visible, status, perms, icon, create_by, create_time, remark)
|
||||||
|
SELECT '接口白名单', menu_id, 8, 'accessWhitelist', 'system/accessWhitelist/index', 1, 0, 'C',
|
||||||
|
'0', '0', 'system:config:list', 'lock', 'admin', NOW(), '管理后台全局接口 IP 访问白名单'
|
||||||
|
FROM sys_menu parent
|
||||||
|
WHERE parent.menu_name = '系统管理' AND parent.menu_type = 'M'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM sys_menu existing
|
||||||
|
WHERE existing.component = 'system/accessWhitelist/index'
|
||||||
|
)
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
-- 已拥有参数设置查询权限的角色可看到页面;新增、编辑、删除继续沿用该角色已有的 config 权限。
|
||||||
|
INSERT INTO sys_role_menu (role_id, menu_id)
|
||||||
|
SELECT DISTINCT permission.role_id, whitelist.menu_id
|
||||||
|
FROM sys_role_menu permission
|
||||||
|
JOIN sys_menu config_menu ON config_menu.menu_id = permission.menu_id
|
||||||
|
JOIN sys_menu whitelist ON whitelist.component = 'system/accessWhitelist/index'
|
||||||
|
WHERE config_menu.perms = 'system:config:list'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM sys_role_menu current_permission
|
||||||
|
WHERE current_permission.role_id = permission.role_id
|
||||||
|
AND current_permission.menu_id = whitelist.menu_id
|
||||||
|
);
|
||||||
Reference in new issue
Block a user