- 单元测试
见http://www.lancegleason.com/blog/2009/12/07/unit-testing-spring-security-with-annotations
由于这是一个旧教程,您可能需要更改引用的架构版本。但更重要的是,此处显示的 SecurityContext.xml 配置不适用于 Spring Security 3。有关正确配置,请参阅Spring Security - multiple authentication-providers。
我不需要提到的依赖项:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core-tiger</artifactId>
</dependency>
它在没有它们的情况下工作(但是没有创建抽象测试类)
根.this
这实际上是正确的方法
问题是您不能使用类参数的 getSimpleName() 。有关深入讨论,请参阅http://forum.springsource.org/showthread.php?98570-Getting-Payload-Classname-in-Header-Enricher-via-SpEL
那里显示的解决方法对我没有多大帮助。所以我想出了这个非常简单的解决方案:
只需添加字符串属性并将String compoundClassSimpleName
其CompoundServiceImpl
设置在构造函数中(由子类调用):
Public abstract class CompoundServiceImpl<T extends Compound>
implements CompoundService<T> {
private String compoundClassSimpleName;
//...
public ChemicalCompoundServiceImpl(Class<T> compoundClass) {
this.compoundClass = compoundClass;
this.compoundClassSimpleName = compoundClass.getSimpleName();
}
//...
public String getCompoundClassSimpleName(){
return compoundClassSimpleName;
}
}
和她实现上述抽象服务的服务:
public class TestCompoundServiceImpl extends CompoundServiceImpl<TestCompound>
implements TestCompoundService {
//...
public TestCompoundServiceImpl() {
super(TestCompound.class);
}
//...
}
最后@PreAuthorize
注释用法:
public interface CompoundService<T extends Compound> {
@PreAuthorize("hasRole('read_' + #root.this.getCompoundClassSimpleName())")
public T getById(final Long id);
}
对于上面的示例,表达式将评估为名为“read_TestCompound”的角色。
完毕!
通常解决方案非常简单,但要到达那里有一个 PITA ......
编辑:
为了完整性测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:ApplicationContext.xml",
"classpath:SecurityContext.xml"
})
public class CompoundServiceSecurityTest {
@Autowired
@Qualifier("testCompoundService")
private TestCompoundService testCompoundService;
public CompoundServiceSecurityTest() {
}
@Before
public void setUp() {
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken("user_test", "pass1"));
}
@Test
public void testGetById() {
System.out.println("getById");
Long id = 1000L;
TestCompound expResult = new TestCompound(id, "Test Compound");
TestCompound result = testCompoundService.getById(id);
assertEquals(expResult, result);
}
}