コントローラーの URL が適切に保護されているかどうかを単体テストする方法を見つけようとしていました。誰かが設定を変更して、誤ってセキュリティ設定を削除した場合に備えてです。
私のコントローラーメソッドは次のようになります:
@RequestMapping("/api/v1/resource/test")
@Secured("ROLE_USER")
public @ResonseBody String test() {
return "test";
}
次のように WebTestEnvironment を設定しました。
import javax.annotation.Resource;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
"file:src/main/webapp/WEB-INF/spring/security.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/servlet-context.xml" })
public class WebappTestEnvironment2 {
@Resource
private FilterChainProxy springSecurityFilterChain;
@Autowired
@Qualifier("databaseUserService")
protected UserDetailsService userDetailsService;
@Autowired
private WebApplicationContext wac;
@Autowired
protected DataSource dataSource;
protected MockMvc mockMvc;
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
protected UsernamePasswordAuthenticationToken getPrincipal(String username) {
UserDetails user = this.userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(
user,
user.getPassword(),
user.getAuthorities());
return authentication;
}
@Before
public void setupMockMvc() throws NamingException {
// setup mock MVC
this.mockMvc = MockMvcBuilders
.webAppContextSetup(this.wac)
.addFilters(this.springSecurityFilterChain)
.build();
}
}
実際のテストでは、次のようなことを試しました。
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import eu.ubicon.webapp.test.WebappTestEnvironment;
public class CopyOfClaimTest extends WebappTestEnvironment {
@Test
public void signedIn() throws Exception {
UsernamePasswordAuthenticationToken principal =
this.getPrincipal("test1");
SecurityContextHolder.getContext().setAuthentication(principal);
super.mockMvc
.perform(
get("/api/v1/resource/test")
// .principal(principal)
.session(session))
.andExpect(status().isOk());
}
}
私はここでこれを拾いました:
- http://java.dzone.com/articles/spring-test-mvc-junit-テストここ:
- http://techdive.in/solutions/how-mock-securitycontextholder-perfrom-junit-tests-spring-controllerまたはここ:
- Spring MVC コントローラーによって指定された @PreAuthorize アノテーションとその Spring EL を JUnit テストする方法を教えてください。
しかし、よく見ると、これは URL に実際のリクエストを送信しない場合にのみ役立ち、機能レベルでサービスをテストする場合にのみ役立ちます。私の場合は、「アクセス拒否」例外がスローされました。
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83) ~[spring-security-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:206) ~[spring-security-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:60) ~[spring-security-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.1.RELEASE.jar:3.2.1.RELEASE]
...
Principal
次の 2 つのログ メッセージは注目に値します。基本的に、ユーザーが認証されなかったこと、つまり設定が機能しなかったか上書きされたことを示しています。
14:20:34.454 [main] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Secure object: ReflectiveMethodInvocation: public java.util.List test.TestController.test(); target is of class [test.TestController]; Attributes: [ROLE_USER]
14:20:34.454 [main] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9055e4a6: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
ベストアンサー1
答えを探していたのですが、簡単で柔軟性のある答えが見つからず、Spring セキュリティ リファレンスそして、ほぼ完璧なソリューションがあることに気付きました。AOP ソリューションはテストに最適なソリューションであることが多く、Springでは、この成果物で、、@WithMockUser
および@WithUserDetails
が提供されています。@WithSecurityContext
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.2.2.RELEASE</version>
<scope>test</scope>
</dependency>
ほとんどの場合、@WithUserDetails
必要な柔軟性とパワーが得られます。
@WithUserDetails はどのように機能しますか?
UserDetailsService
基本的には、テストしたいすべてのユーザープロファイルを含むカスタムを作成する必要があります。例:
@TestConfiguration
public class SpringSecurityWebAuxTestConfig {
@Bean
@Primary
public UserDetailsService userDetailsService() {
User basicUser = new UserImpl("Basic User", "[email protected]", "password");
UserActive basicActiveUser = new UserActive(basicUser, Arrays.asList(
new SimpleGrantedAuthority("ROLE_USER"),
new SimpleGrantedAuthority("PERM_FOO_READ")
));
User managerUser = new UserImpl("Manager User", "[email protected]", "password");
UserActive managerActiveUser = new UserActive(managerUser, Arrays.asList(
new SimpleGrantedAuthority("ROLE_MANAGER"),
new SimpleGrantedAuthority("PERM_FOO_READ"),
new SimpleGrantedAuthority("PERM_FOO_WRITE"),
new SimpleGrantedAuthority("PERM_FOO_MANAGE")
));
return new InMemoryUserDetailsManager(Arrays.asList(
basicActiveUser, managerActiveUser
));
}
}
これでユーザーの準備ができたので、このコントローラー関数へのアクセス制御をテストするとします。
@RestController
@RequestMapping("/foo")
public class FooController {
@Secured("ROLE_MANAGER")
@GetMapping("/salute")
public String saluteYourManager(@AuthenticationPrincipal User activeUser)
{
return String.format("Hi %s. Foo salutes you!", activeUser.getUsername());
}
}
ここにはマップされた関数を取得するルートへ/foo/敬礼また、アノテーションを使用してロールベースのセキュリティをテストしていますが、とも@Secured
テストできます。 2 つのテストを作成しましょう。1 つは、有効なユーザーがこの敬礼応答を表示できるかどうかを確認するテスト、もう 1 つは、実際に禁止されているかどうかを確認するテストです。@PreAuthorize
@PostAuthorize
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = SpringSecurityWebAuxTestConfig.class
)
@AutoConfigureMockMvc
public class WebApplicationSecurityTest {
@Autowired
private MockMvc mockMvc;
@Test
@WithUserDetails("[email protected]")
public void givenManagerUser_whenGetFooSalute_thenOk() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get("/foo/salute")
.accept(MediaType.ALL))
.andExpect(status().isOk())
.andExpect(content().string(containsString("[email protected]")));
}
@Test
@WithUserDetails("[email protected]")
public void givenBasicUser_whenGetFooSalute_thenForbidden() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get("/foo/salute")
.accept(MediaType.ALL))
.andExpect(status().isForbidden());
}
}
ご覧のとおり、SpringSecurityWebAuxTestConfig
テスト用にユーザーに提供するためにインポートしました。それぞれは、簡単な注釈を使用するだけで対応するテスト ケースで使用され、コードと複雑さが軽減されます。
よりシンプルなロールベースのセキュリティのために@WithMockUserを使用する方が良いでしょう。
ご覧のとおり、@WithUserDetails
ほとんどのアプリケーションに必要な柔軟性がすべて備わっています。ロールや権限などの任意の GrantedAuthority を持つカスタムユーザーを使用できます。ただし、ロールのみを扱っている場合は、テストがさらに簡単になり、カスタムの構築を避けることができますUserDetailsService
。そのような場合は、ユーザー、パスワード、ロールの単純な組み合わせを次のように指定します。モックユーザー。
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@WithSecurityContext(
factory = WithMockUserSecurityContextFactory.class
)
public @interface WithMockUser {
String value() default "user";
String username() default "";
String[] roles() default {"USER"};
String password() default "password";
}
このアノテーションは、非常に基本的なユーザーのデフォルト値を定義します。私たちの場合、テストしているルートでは、認証されたユーザーがマネージャーである必要があるだけなので、使用をやめてSpringSecurityWebAuxTestConfig
これを実行できます。
@Test
@WithMockUser(roles = "MANAGER")
public void givenManagerUser_whenGetFooSalute_thenOk() throws Exception
{
mockMvc.perform(MockMvcRequestBuilders.get("/foo/salute")
.accept(MediaType.ALL))
.andExpect(status().isOk())
.andExpect(content().string(containsString("user")));
}
注目すべきは、ユーザーの代わりに[メールアドレス]デフォルトは次のものから提供されます@WithMockUser
:ユーザー; しかし、それは問題ではありません。なぜなら、私たちが本当に気にかけているのは彼の役割だからですROLE_MANAGER
。
結論
ご覧の通り、注釈@WithUserDetails
や@WithMockUser
異なる認証済みユーザーシナリオを切り替える単純なテストを行うためだけに、アーキテクチャから離れたクラスを構築する必要はありません。@セキュリティコンテキストさらに柔軟性が高まります。