で使用Spring 4
していSpring Security 3.2
ます。アプリケーションはこれまでのところ完全に実行されています。MockMVC
現在、とを使用して、すべての REST サービスのテスト ケースを作成していますRestAssured
。
私は単純なコントローラーを持っています
@RestController
@RequestMapping("/secure")
public class MyController{
@RequestMapping(method = RequestMethod.GET)
public String getAllMedia(){
return "SECURE TEST COMPLETE";
}
}
そして、私は持っているspring-rest-servlet.xml
ファイルを持っています<mvc:annotation-driven />
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<mvc:annotation-driven />
<context:component-scan base-package="com.mp.web.controller.common" />
</beans>
そしてspring-security-context.xml
、このように見えます
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<security:http create-session="stateless"
entry-point-ref="authenticationEntryPoint"
authentication-manager-ref="authenticationManager">
<security:custom-filter ref="customRestFilter"
position="BASIC_AUTH_FILTER" />
<security:intercept-url pattern="/api/p/**" access="ROLE_USER" />
<security:intercept-url pattern="/api/login**"
access="IS_AUTHENTICATED_ANONYMOUSLY" requires-channel="https" />
</security:http>
<bean id="authenticationEntryPoint"
class="com.mp.web.security.CustomAuthenticationEntryPoint" />
<bean id="customRestFilter"
class="com.mp.web.security.filter.AuthenticationTokenProcessingFilter">
<constructor-arg name="authenticationManager"
ref="authenticationManager" />
</bean>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="restAuthenticationProvider" />
</security:authentication-manager>
<bean id="restAuthenticationProvider"
class="com.mp.web.security.auth.RestAuthenticationProvider" />
</beans>
アプリケーションは期待どおりに動作していますが、テスト ケースを実行すると、スプリング セキュリティ フィルターが実行されません。'customRestFilter'
これが私のテストクラスです
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:application-context.xml",
"classpath:spring-rest-servlet.xml",
"classpath:spring-security-context.xml"})
public class Test {
@Autowired
private FilterChainProxy springSecurityFilterChain;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
MyController myController;
@Before
public void init(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter(springSecurityFilterChain).build();
}
@Test
public void test1() throws Exception {
given().mockMvc(mockMvc).standaloneSetup(myController)
.header("Authorization", userHeaderToken) // Token for user
.when().get("/secure")
.then().log().body();
}
}
はuserHeaderToken
、どのユーザーからのリクエストを取得したかを識別するために使用され、そのすべてが で処理され'customRestFilter'
ます。しかし、テストケースからの私のリクエストはフィルターに届きません。
リクエストがそのフィルターを通過する方法を教えてもらえますか。
ありがとう。