在 spring security dofilter 身份验证方法中使用 @Autowired 注释时出现空指针异常
Nullpointer Exception for @Autowired annotation while using that in spring security dofilter method of authentication
我用 @Service
注释了服务 class,如下所示:
@Service
public class SimpleApplicationSettingManager implements ApplicationSettingManager {
@Autowired
ApplicationSettingsDao applicationSettingsDao;
//Method to Fetch value from the database
@Override
public boolean isConfigValue() {
return applicationSettingsDao.getSettingsByKey(Constants.CONFIG_VALUE)
.getValue().equalsIgnoreCase("true")?true:false;
}
}
DAO class
public interface ApplicationSettingsDao extends GenericDao<ApplicationSettings, Long> {
/**
* @param key of the setting to retrive value
* @return the value mapped against the key.
*/
@Transactional
ApplicationSettings getSettingsByKey(String key);
}
下面我正在尝试在 Spring 安全性中自动装配以保持进入会话
public class AuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {
private static final Log LOG = LogFactory.getLog(AuthenticationProcessingFilter.class);
@Autowired
private ApplicationSettingManager applicationSettingManager;
public AuthenticationProcessingFilter(String filterProcessesUrl){
super(filterProcessesUrl);
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
if (req instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) req;
HttpSession httpSession = httpRequest.getSession();
isCIAMEnabled= **applicationSettingManager**.isConfigValue();
httpSession.setAttribute(Constants.SESSION_ATTR_CIAM_ENABLED, isCIAMEnabled);
}
super.doFilter(req, res, chain);
}
}
结果如下所示 NullPointerException
:
java.lang.NullPointerException
at service.SimpleApplicationSettingManager.isConfigValue(SimpleApplicationSettingManager.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy124.isConfigValue(Unknown Source)
at web.filter.security.ciam.AuthenticationProcessingFilter.doFilter(AuthenticationProcessingFilter.java:85)
在我看来,这可能有两个原因:
getSettingsByKey(Constants.CONFIG_VALUE)
returns null
;
getSettingsByKey(Constants.CONFIG_VALUE)
事实上 returns 一个有效的非空值 ApplicationSettings
但是 getValue()
方法调用了它 returns null
.
我用 @Service
注释了服务 class,如下所示:
@Service
public class SimpleApplicationSettingManager implements ApplicationSettingManager {
@Autowired
ApplicationSettingsDao applicationSettingsDao;
//Method to Fetch value from the database
@Override
public boolean isConfigValue() {
return applicationSettingsDao.getSettingsByKey(Constants.CONFIG_VALUE)
.getValue().equalsIgnoreCase("true")?true:false;
}
}
DAO class
public interface ApplicationSettingsDao extends GenericDao<ApplicationSettings, Long> {
/**
* @param key of the setting to retrive value
* @return the value mapped against the key.
*/
@Transactional
ApplicationSettings getSettingsByKey(String key);
}
下面我正在尝试在 Spring 安全性中自动装配以保持进入会话
public class AuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {
private static final Log LOG = LogFactory.getLog(AuthenticationProcessingFilter.class);
@Autowired
private ApplicationSettingManager applicationSettingManager;
public AuthenticationProcessingFilter(String filterProcessesUrl){
super(filterProcessesUrl);
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
if (req instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) req;
HttpSession httpSession = httpRequest.getSession();
isCIAMEnabled= **applicationSettingManager**.isConfigValue();
httpSession.setAttribute(Constants.SESSION_ATTR_CIAM_ENABLED, isCIAMEnabled);
}
super.doFilter(req, res, chain);
}
}
结果如下所示 NullPointerException
:
java.lang.NullPointerException
at service.SimpleApplicationSettingManager.isConfigValue(SimpleApplicationSettingManager.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy124.isConfigValue(Unknown Source)
at web.filter.security.ciam.AuthenticationProcessingFilter.doFilter(AuthenticationProcessingFilter.java:85)
在我看来,这可能有两个原因:
getSettingsByKey(Constants.CONFIG_VALUE)
returnsnull
;getSettingsByKey(Constants.CONFIG_VALUE)
事实上 returns 一个有效的非空值ApplicationSettings
但是getValue()
方法调用了它 returnsnull
.