0

我对 Spring 和 Spring 安全性非常陌生!

在学习了一些教程之后,我现在尝试在我们的应用程序中实现 Spring Security。我遇到的问题是我想UserDetailsImpl用存根测试我的课程。因此,我希望 Spring 将 2 个对象作为我创建的存根注入(它们从接口实现)。这样,当我运行测试时,存根将被实现,但在生产代码期间,将注入正常的实际实现。不幸的是,我得到了一个NullPointerException. 这是我的代码:

必须注入的类:

package com.somePackageName.security;

imports...

@Component
public class UserDetailsImpl implements UserDetails {

@Autowired
IUserFactory userFactory;

public void setUserFactory(IUserFactory userFactory){
    this.userFactory = userFactory;
}

@Autowired
IUser user;

public void setUser(IUser user){
    this.user = user;
}

IUser 和 IUserFactory 是接口。

这是IUserFactory我要实现的存根:

package com.somePackageName;

import com.su.rbac.virtuoso.IUser;
import com.su.rbac.virtuoso.IUserFactory;

public class StubUserFactory implements IUserFactory {

public StubUserFactory(){}

@Override
public IUser findUserWithUsername(String username) {
    return new StubUser();
}

@Override
public IUser createUser(String username, String password) {
    return new StubUser();
}
}

最后是我的application-context.xml文件:

<global-method-security pre-post-annotations="enabled"/>
<context:annotation-config />
<context:component-scan base-package="com.somePackageName" />

<beans:bean id="userDetailsImpl" class="com.somePackageName.security.UserDetailsImpl">
    <!--
    <beans:property name="userFactory" value="com.somePackageName.StubUserFactory"/>
    <beans:property name="user" value="com.somePackageName.StubUser"/>
    -->

    <beans:property name="userFactory" ref="StubUserFactory"/>
    <beans:property name="user" ref="StubUser"/>
</beans:bean>

<beans:bean id="StubUser" class="com.somePackageName.StubUser"/>
<beans:bean id="StubUserFactory" class="com.somePackageName.StubUserFactory"/>

我得到的错误是:

java.lang.NullPointerException
at com.somePackageName.security.UserDetailsImpl.<init>(UserDetailsImpl.java:44)
at com.somePackageName.security.QuadstoreUserService.loadUserByUsername(QuadstoreUserService.java:23)
at com.somePackageName.SpringSecurityTest.firstTest(SpringSecurityTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我真的坚持这个,因为我似乎无法在互联网上找到一个好的和清晰的例子。非常感谢您!

4

1 回答 1

1

好像您没有启用注释,请尝试添加:

<context:annotation-config />
<context:component-scan base-package="<your_package_name>,<your_another_package_name>" />

xmlns:

xmlns:context="http://www.springframework.org/schema/context"

xsi 架构位置:

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
于 2013-07-26T08:19:40.997 回答