我们有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没有在自定义身份验证提供程序中自动连接。而同样的服务authenticationService在我的MVC控制器中以同样的方式自动连接。
这是因为身份验证提供者是Spring安全组件吗?
下面是一个my 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安全组件中自动装配我的服务类?
是否使用<debug/>
元素?如果是这样,请尝试删除,看看它是否解决了您的问题,因为SEC-1885在使用<debug/>
时阻止@Autowired
工作。
也许自动装配后处理器在根应用程序上下文中没有启用(但在DispatcherServlet
的上下文中作为<mvc:annotation-driven>
或<context:component-scan>
的副作用启用)。
您可以通过将<context:annotation-config>
添加到myApp-security.xml
来启用它
我经历了这个问题,并得出结论,当自动装配发生时,spring安全性正在使用一个完全不同的类实例进行操作。为了解决这个问题,我将安全配置导入到spring mvc配置中,如下所示。
这允许Spring security与我的Spring mvc共享上下文。
<import resource="myapp-security.xml" />
我遇到了同样的问题并修复了它。
解决方案是,即使你为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>
-->
并添加到安全上下文文件
如果你正在使用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>
您需要将您的CustomAuthenticationProvider定义为spring bean(通常在applicationContext.xml中,如果有applicationContext-security.xml)
您应该使用你不能使用,因为你的myApp-security.xml正在创建另一个ApplicationContext,它看不到myApp-servlet.xml