首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Security捕获"CommunicationException“

从Security捕获"CommunicationException“
EN

Stack Overflow用户
提问于 2015-09-10 12:25:06
回答 1查看 1.1K关注 0票数 1

下午好,

我目前正在制作一个the应用程序,它必须是用户可以访问的,既可以在网络上访问,也可以从网络上访问。但在这两种情况下都需要身份验证。

问题是,当我试图在网络上进行身份验证时,我不知道如何捕获由spring安全性引发的以下异常:

(javax.naming.CommunicationException)

代码语言:javascript
复制
org.springframework.security.authentication.InternalAuthenticationServiceException: companyname.com:636; nested exception is javax.naming.CommunicationException: companyname.com:636 [Root exception is java.net.UnknownHostException: com.companyname]
at org.springframework.security.ldap.authentication.LdapAuthenticationProvider.doAuthentication(LdapAuthenticationProvider.java:191)
at org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider.authenticate(AbstractLdapAuthenticationProvider.java:80)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:92)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

这是我的"GlobalAuthenticationConfigurerAdapter“适配器:

代码语言:javascript
复制
@Configuration
@Order( SecurityProperties.ACCESS_OVERRIDE_ORDER )
protected static class LdapConnection extends GlobalAuthenticationConfigurerAdapter {
@Autowired
private ActiveDirectoryUserMapper mapper;

@Override
public void init( final AuthenticationManagerBuilder auth ) throws Exception {
    auth.ldapAuthentication().userSearchFilter( "(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))" ).userSearchBase( "DC=companyname,DC=com" ).contextSource( this.contextSource() )
            .userDetailsContextMapper( this.mapper ).ldapAuthoritiesPopulator( this.ldapAuthoritiesPopulator() ).rolePrefix( "" );
}

@Bean
public DefaultSpringSecurityContextSource contextSource() throws Exception {
    final DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource( "ldaps://companyname.com:636" );

    contextSource.setUserDn( "UserDn@companyname" );
    contextSource.setPassword( "password" );
    contextSource.setReferral( "ignore" );
    contextSource.afterPropertiesSet();

    return contextSource;
}

@Bean
public LdapAuthoritiesPopulator ldapAuthoritiesPopulator() throws Exception {
    final DefaultLdapAuthoritiesPopulator populator = new DefaultLdapAuthoritiesPopulator( this.contextSource(), "OU=Groupes,DC=companyname,DC=com" );

    populator.setSearchSubtree( true );
    populator.setGroupRoleAttribute( "cn" );
    populator.setGroupSearchFilter( "member={0}" );
    populator.setIgnorePartialResultException( true );
    populator.setRolePrefix( "" );

    return populator;
}
}

我的目标只是在ldap服务器不可用时实现一种“后备模式”。

你知道如何抓住这个例外,甚至防止它吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-10 13:14:56

InternalAuthenticationServiceException扩展了AuthenticationServiceException,而后者又扩展了AuthenticationException。您可以创建扩展SimpleUrlAuthenticationFailureHandler和重写的自定义类。

代码语言:javascript
复制
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)

当身份验证流失败时,Spring将调用此方法。

因此,在您的示例中,如果异常是InternalAuthenticationServiceException的一个实例,则可以依赖您的“回退模式”。但是,此时您的登录阶段已经失败,用户被认为已注销,因此您可能需要重新创建“手动”一致的“状态”,其中用户将正确登录(例如,向Spring添加一个适当的UserDetails实例)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32501853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档