- ユニットテスト
http://www.lancegleason.com/blog/2009/12/07/unit-testing-spring-security-with-annotationsを参照してください
これは古いチュートリアルなので、参照されるスキーマのバージョンを変更する必要があるかもしれません。ただし、さらに重要なのは、そこに示されているSecurityContext.xml構成がSpringSecurity3では機能しないことです。適切な構成についてはSpringSecurity-複数の認証プロバイダーを参照してください。
私は言及された依存関係を必要としませんでした:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core-tiger</artifactId>
</dependency>
それらがなくても機能しました(ただし、抽象的なテストクラスは作成されませんでした)
root.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);
}
}