Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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 如何在安全上下文中使用@Autowiring从应用程序上下文调用bean_Java_Spring_Spring Security - Fatal编程技术网

Java 如何在安全上下文中使用@Autowiring从应用程序上下文调用bean

Java 如何在安全上下文中使用@Autowiring从应用程序上下文调用bean,java,spring,spring-security,Java,Spring,Spring Security,经过研究,我仍然没有找到解决这个问题的办法 我的目标是使用数据库验证自定义身份验证提供程序中的用户,但@Autowiring总是抛出空指针异常 这是我的代码: 自定义身份验证提供程序: @Component public class CustomAuthenticationProvider implements AuthenticationProvider { @Autowired private Validator iValidate; @Override

经过研究,我仍然没有找到解决这个问题的办法

我的目标是使用数据库验证自定义身份验证提供程序中的用户,但@Autowiring总是抛出空指针异常

这是我的代码:

自定义身份验证提供程序:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private Validator iValidate;

    @Override
    public Authentication authenticate(Authentication auth) throws AuthenticationException{         
        if (iValidate.userNameCheck(auth.getName()) != "00") {
            auth=null;
        }     
        return auth:            
    }

    @Override
    public boolean supports(Class<?> auth) {
        return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(auth));
    }

}
Spring安全XML:

<global-method-security pre-post-annotations="enabled" />



<http pattern="/resources/**" security="none" />
<http auto-config="false" use-expressions="true" disable-url-rewriting="true" >


    <session-management session-fixation-protection="newSession" session-authentication-error-url="/login.do?sessionalreadyexist">
        <concurrency-control max-sessions="1" expired-url="/login.do?expired" error-if-maximum-exceeded="true" />
    </session-management>

    <intercept-url pattern="/clearcache" access="permitAll()" />
    <intercept-url pattern="/login.do" access="permitAll()" />
    <intercept-url pattern="/**" access="isFullyAuthenticated()" />

    <logout logout-success-url="/login.do?logout" delete-cookies="JSESSIONID" invalidate-session="true" />
    <access-denied-handler error-page="/403" />

 <form-login login-page='/login.do' login-processing-url="/login" default-target-url="/main" always-use-default-target="true" authentication-failure-url="/login.do?error" username-parameter="username"    password-parameter="password" authentication-details-source-ref="CustomAuthInfoSource" /> -->

</http>

<authentication-manager>
    <authentication-provider ref="CustomAuthenticationProvider" />
</authentication-manager>

<beans:bean id="CustomAuthenticationProvider" class="xx.xxx.xxxx.xxxxx.CustomAuthenticationProvider" />

-->

beans.xml:

<beans:bean id="iValidate" class="xx.xxx.xxxx.xxxxx.Validator" scope="prototype" />

/////// Other beans ////////

///////其他豆类////////
当我调用
@Autowired private Validator iValidate时
在@Controller类中,它正常工作,但在CustomAuthenticationProvider中,它将返回null


有解决方案吗?

@组件
注释
CustomAuthenticationProvider
,注释仍然会抛出空指针,应该放在
的位置此链接会有所帮助
<beans:bean id="iValidate" class="xx.xxx.xxxx.xxxxx.Validator" scope="prototype" />

/////// Other beans ////////