@Autowire不在Spring安全自定义身份验证提供程序中工作

时间:2011-08-11 09:58:48

标签: spring-mvc spring-security autowired

我们有Spring MVC应用程序。我们正在尝试将Spring安全性集成到其中。

我们编写了自定义身份验证提供程序,它将执行身份验证工作。

以下是我的自定义身份验证提供程序的代码。

    public class CustomAuthenticationProvider extends DaoAuthenticationProvider {

    @Autowired
    private AuthenticationService authenticationService;

    @Override
    public Authentication authenticate(Authentication authentication) {

        CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication;

        String username = String.valueOf(auth.getPrincipal());
        String password = String.valueOf(auth.getCredentials());

        try {

            Users user = new User();
            user.setUsername(username);
            user.setPassword(PasswordUtil.encrypt(password));

            user = authenticationService.validateLogin(user);

            return auth;
        } catch (Exception e) {
            throw new BadCredentialsException("Username/Password does not match for " + username);
        }
    }

    @Override
    public boolean supports(Class<? extends Object> authentication) {
        return (CustomAuthenticationToken.class.isAssignableFrom(authentication));

    }
}

这里我在下一行获得NullpointerException

user = authenticationService.validateLogin(user);

authenticationService未在自定义身份验证提供程序中自动装配。虽然在我的MVC控制器中以相同的方式自动装配相同的服务authenticationService。

这是因为身份验证提供程序是Spring安全组件吗?

下面是我的web.xml

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/myApp-security.xml
    </param-value>
</context-param>

<servlet>
    <servlet-name>myApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/myApp-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myApp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

编辑1: -

我在spring安全配置文件中添加了以下行。

<beans:bean id="customAuthenticationProvider" class="com.myApp.security.provider.CustomAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService"/>   
</beans:bean>

请帮助如何在Spring安全组件中自动装配我的服务类?

7 个答案:

答案 0 :(得分:5)

也许自动装配后处理器未在根应用程序上下文中启用(但在DispatcherServlet的上下文中启用,作为<mvc:annotation-driven><context:component-scan>的副作用。

您可以通过将<context:annotation-config>添加到myApp-security.xml来启用它。

答案 1 :(得分:3)

您使用的是<debug/>元素吗?如果是,请尝试删除以查看是否修复了您的问题,因为SEC-1885在使用@Autowired时阻止<debug/>工作。

答案 2 :(得分:3)

我遇到了这个问题并得出结论,当自动装配正在进行时,弹簧安全性正在以完全不同的类实例运行。为了解决这个问题,我将安全配置导入spring mvc配置,如下所示。

这允许Spring安全性与我的spring mvc共享上下文。

<import resource="myapp-security.xml" />

答案 3 :(得分:1)

我遇到了同样的问题并修复了它。

即使您为Service类设置了@Autowired注释,解决方案也是如此。

 @Autowired
 private AuthenticationService authenticationService;

在dispatcher-servlet.xml中删除了bean定义,它将起作用。

 <!--
 <beans:bean id="customAuthenticationProvider" class="com.myApp.security.provider.CustomAuthenticationProvider">
 <beans:property name="userDetailsService" ref="userDetailsService"/>   
 </beans:bean>
 -->

并将其添加到安全上下文文件

答案 4 :(得分:0)

您需要将CustomAuthenticationProvider定义为spring bean(一般在applicationContext.xml中,如果有的话,还可以在applicationContext-security.xml中)

答案 5 :(得分:0)

你应该使用 您无法使用,因为您的myApp-security.xml正在创建另一个ApplicationContext,它不会看到myApp-servlet.xml创建的上下文中的所有自动装配

答案 6 :(得分:0)

如果您使用的是Spring MVC,则必须在contextConfigLocation中添加 spring-security.xml和dispatcher-servlet.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml
            /WEB-INF/dispatcher-servlet.xml
        </param-value>
    </context-param>