Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在预验证提供程序中处理UserNotFoundException_Java_Spring_Spring Security - Fatal编程技术网

Java 在预验证提供程序中处理UserNotFoundException

Java 在预验证提供程序中处理UserNotFoundException,java,spring,spring-security,Java,Spring,Spring Security,我有两个身份验证提供者。一个检查数据库,另一个是SAML断言的预认证提供者 用户信息及其各自的角色保存在数据库中。因此,当用户使用SAML访问应用程序时,我们在数据库中插入一行并分配一个默认角色,然后将用户重定向到注册页面,在该页面中我们捕获一些特定于用户的信息 使用PreAuthenticatedProvider,我正在捕获UserNotFoundException,问题是如何将用户从我的authenticate方法重定向到注册页面 @Override public Authe

我有两个身份验证提供者。一个检查数据库,另一个是SAML断言的预认证提供者

用户信息及其各自的角色保存在数据库中。因此,当用户使用SAML访问应用程序时,我们在数据库中插入一行并分配一个默认角色,然后将用户重定向到注册页面,在该页面中我们捕获一些特定于用户的信息

使用PreAuthenticatedProvider,我正在捕获UserNotFoundException,问题是如何将用户从我的authenticate方法重定向到注册页面

    @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);
            }
        }
    }