java - Spring Security - 如果缺少角色,则阻止登录
问题描述
我有一个使用 LDAP 身份验证对用户进行身份验证的 spring 应用程序,将其与自定义权限填充器结合使用,我可以正确登录到我的应用程序。但是,如果用户没有特定角色,我想阻止他们完全登录。
我试图创建一个登录成功处理程序,它将从Authentication
对象中获取角色,setAuthenticated(false)
如果它们不满足要求,但这似乎为时已晚,仍然允许用户继续。
我可以创建一个自定义身份验证提供程序,但由于LdapAuthenticationProviderConfigurer
它非常有用,如果可能的话我不想这样做。
我目前的安全配置如下:
auth.ldapAuthentication()
.userSearchBase(ldapBase)
.userSearchFilter(ldapFilter)
.ldapAuthoritiesPopulator(new AuthorityPopulator(client))
.contextSource(contextSource());
我的成功处理程序:
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
String targetUrl = determineTargetUrl(request, response);
List<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
if(!roles.contains("admin")) {
authentication.setAuthenticated(false);
}
redirectStrategy.sendRedirect(request, response, targetUrl);
}
解决方案
推荐阅读
- javascript - 使用 WebRTC 一对多广播将流媒体的文本框内容流式传输到查看器的文本框
- ubuntu - awk 查找字符串完全匹配的行号
- python - 在单个事务中将多个 pandas DataFrames 保存到 SQLite
- amazon-web-services - AWS CloudWatch Events - 获取资源标签
- flutter - 无法构建颤振应用程序 IOS - mapbox_map.dart:14:5: 错误:“必需”不是类型。需要 this.initialCameraPosition
- html - 我应该怎么做才能点击 react-router 的链接标签内的按钮,而不导航到该链接?
- firebase - 在构建 Wrapper(dirty) 时引发了以下 ProviderNotFoundException:错误:找不到正确的提供程序
在此 Wrapper Widget 上方 - discord.js - 设置超时以删除嵌入 discord.js 上的消息
- r - 在 ggplot 如何使填充参数在 geom_dotplot() 中起作用
- image - PowerShell:Get-ChildItem 列出元数据中包含某些“标签”关键字的 .jpg 文件名 | 导出为 CSV