Java 在预验证提供程序中处理UserNotFoundException
我有两个身份验证提供者。一个检查数据库,另一个是SAML断言的预认证提供者 用户信息及其各自的角色保存在数据库中。因此,当用户使用SAML访问应用程序时,我们在数据库中插入一行并分配一个默认角色,然后将用户重定向到注册页面,在该页面中我们捕获一些特定于用户的信息 使用PreAuthenticatedProvider,我正在捕获UserNotFoundException,问题是如何将用户从我的authenticate方法重定向到注册页面Java 在预验证提供程序中处理UserNotFoundException,java,spring,spring-security,Java,Spring,Spring Security,我有两个身份验证提供者。一个检查数据库,另一个是SAML断言的预认证提供者 用户信息及其各自的角色保存在数据库中。因此,当用户使用SAML访问应用程序时,我们在数据库中插入一行并分配一个默认角色,然后将用户重定向到注册页面,在该页面中我们捕获一些特定于用户的信息 使用PreAuthenticatedProvider,我正在捕获UserNotFoundException,问题是如何将用户从我的authenticate方法重定向到注册页面 @Override public Authe
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
try {
// custom code
} catch (UsernameNotFoundException e) {
// Redirect user to a view
}
}
有没有更好的办法来处理这类案件
编辑:我对处理来自AuthenticationProviders的AuthenticationException感兴趣,尤其是预验证Providers。您可以编写自己的自定义
ExceptionTranslationFilter
并将其放在表单processin筛选器之前的筛选器链中,以捕获这些异常并正确处理
类似于application-context.xml中的内容
<bean id="customExceptionFilter" class="com.company.module.security.filters.CustomExceptionTranslationFilter">
<constructor-arg ref="authenticationEntryPoint"/>
<constructor-arg ref="savedRequestCache"></constructor-arg>
<property name="accessDeniedHandler" ref="customAccessDeniedHandler"/>
</bean>
请参见第8.2节:
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
ServletException
{
try
{
super.doFilter(req, res, chain);
}
catch (Exception ex)
{
// Handle the exception here
logger.error("Exception - ", ex);
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
if (SecurityHelper.isAjaxRequest(request))
{
writeFailedAjaxResponse(response, ErrorCode.INTERNAL_ERROR);
return;
}
else
{
throw new ServletException(ex);
}
}
}