2017-02-25 9 views
2

webSecurityConfigurerAdapterのカスタム実装があります。ここでは、マッチャーで要求を許可するconfig()メソッドをオーバーライドします。Spring MockMVCをカスタムSpringセキュリティで使用する方法WebSecurityConfigurerAdapter

モックmvcを使用してコントローラにリクエストを送信して、それらが正しくブロックされていることを確認するユニットテストを作成する必要があります。しかし、私がテストを実行するとき、WebSecurityConfigurerAdapterの私のインプリケーションをロードしません。私SecurityConfigSso.classから

オーバーライドさWebSecurityConfigurerAdapter ::のconfigure()メソッド:ここ

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests() 
      .antMatchers("/img/**", "lib/**", "/api/event**", "/api/event/**","/login/cas**").permitAll() 
      .antMatchers(HttpMethod.GET, "/**").hasAnyAuthority(AvailableRoles.ANY) 
      .antMatchers(HttpMethod.POST, "/**").hasAnyAuthority(AvailableRoles.ADMIN, AvailableRoles.GIS_ANALYST) 
      .antMatchers(HttpMethod.PUT, "/**").hasAnyAuthority(AvailableRoles.ADMIN, AvailableRoles.GIS_ANALYST) 
      .antMatchers(HttpMethod.DELETE, "/**").hasAnyAuthority(AvailableRoles.ADMIN, AvailableRoles.GIS_ANALYST) 
      .anyRequest().authenticated(); 
} 

私のユニットテストです:

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(classes = { SecurityConfigSso.class }) 

public class SecurityTestControllerTests { 

    private final String SECURITY_URL = "/security"; 

    private MockMvc mockMvc; 

    @Autowired 
    private WebApplicationContext context; 

    @Before 
    public void init() { 
     Assert.assertNotNull(context); 
     mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); 
    } 

    @Test 
    public void postMethodShouldBeForbiddenToGuest() throws Exception { 
     this.mockMvc.perform(post(SECURITY_URL).with(user("test").roles(AvailableRoles.GUEST))) 
      .andExpect(status().isForbidden()).andReturn(); 
    } 
} 

このテストの結果はから403でなければなりませんサーバはまだ200です...(

答えて

関連する問題