Java Spring安全性-如果缺少角色,则阻止登录
我有一个spring应用程序,它使用LDAP身份验证对用户进行身份验证,结合自定义权限填充器,我可以正确登录到我的应用程序。但是,如果用户没有特定的角色,我想阻止他们完全登录 我试图创建一个登录成功处理程序,该处理程序将从Java Spring安全性-如果缺少角色,则阻止登录,java,spring,authentication,spring-security,spring-security-ldap,Java,Spring,Authentication,Spring Security,Spring Security Ldap,我有一个spring应用程序,它使用LDAP身份验证对用户进行身份验证,结合自定义权限填充器,我可以正确登录到我的应用程序。但是,如果用户没有特定的角色,我想阻止他们完全登录 我试图创建一个登录成功处理程序,该处理程序将从Authentication对象和setAuthenticated(false)获取角色,如果它们不满足要求,但这似乎为时已晚,仍然允许用户继续 我可以创建自定义身份验证提供程序,但由于ldapaauthenticationproviderconfigurer非常有用,如果可能
Authentication
对象和setAuthenticated(false)
获取角色,如果它们不满足要求,但这似乎为时已晚,仍然允许用户继续
我可以创建自定义身份验证提供程序,但由于ldapaauthenticationproviderconfigurer
非常有用,如果可能的话,我不想这样做
我目前已使用以下配置了安全性:
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);
}
受保护的无效句柄(HttpServletRequest请求、HttpServletResponse响应、身份验证)引发IOException{
字符串targetUrl=DeterminiteTargetUrl(请求、响应);
列表角色=authentication.getAuthories().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
如果(!roles.contains(“admin”)){
authentication.setAuthenticated(false);
}
redirectStrategy.sendRedirect(请求、响应、目标URL);
}
您是否创建了AccessDeniedHandler?安全配置也可以使用此方法:“accessDeniedHandler(new accessDeniedHandler());”