在MockMvc测试中从Spring Security模拟OAuth2AuthenticationToken

时间:2018-07-20 20:25:39

标签: java spring-mvc spring-security spring-test-mvc

我在MockMvc测试中使用uuid,db,table,start,stop,time,size 941439639,test,,"2018-06-14 17:35:07","2018-06-14 17:35:07",62.9666666666667,141329782065 890252165,test,,"2018-06-14 23:35:38","2018-06-14 23:35:38",61.7166666666667,141380294237 943883747,test,,"2018-06-15 05:38:39","2018-06-15 05:38:39",77.7666666666667,141469254934 827384296,test,,"2018-06-15 11:35:11","2018-06-15 11:35:11",63.4166666666667,141276941916 454468935,test,,"2018-06-15 17:35:23","2018-06-15 17:35:23",64.4333333333333,141380122325 705894402,test,,"2018-06-15 23:35:29","2018-06-15 23:35:29",63.9,141715941073 396694772,test,,"2018-06-16 05:39:59","2018-06-16 05:39:59",75.0666666666667,141789270192 @WithAnonymousUser,但是只有@WithMockUser可以正常工作。我怀疑这是因为我在Spring中使用Oauth2的方式与其他解决方案不同。据我所知,我与UserDetails没有任何关系。相反,我正在使用@WithAnonymousUser。 Spring神奇地将来自Google的oauth2服务的信息放入该令牌中,我可以提取出Google的唯一ID(“子”),然后将其解析为我创建并存储在JPA中的自定义用户类。我似乎找不到找到将Google ID或令牌注入测试的方法。有提示吗?

build.gradle

这些是我的OAuth2AuthenticationToken依赖项。

build.gradle

PantryControllerMvcTest

这是我的MockMvc测试类。我最终想用这样的测试来测试我的控制器,但是我不知道在测试中使用哪种注释来模拟我用来验证用户的dependencies { compile 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.6' compile group: 'net.minidev', name: 'json-smart', version: '2.3' compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-mail') compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter-oauth2') compile('org.springframework.cloud:spring-cloud-starter-security') compile('javax.xml.bind:jaxb-api:2.3.0') compile('org.springframework.security:spring-security-oauth2-client:5.0.5.RELEASE') compile('org.springframework.security:spring-security-oauth2-jose:5.0.5.RELEASE') runtime('org.springframework.boot:spring-boot-devtools') runtime('org.postgresql:postgresql') testRuntime('com.h2database:h2') testCompile('org.springframework.boot:spring-boot-starter-test') testCompile('org.springframework.security:spring-security-test') } 。我唯一失败的测试是最后一个称为OAuth2AuthenticationToken的测试。

shouldViewSettingsOk

PantryController

这是我目前正在测试的控制器。如您所见,我正在使用方法package org.wecancodeit.pantryplus2electricboogaloo.controllers; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.security.test.context.support.WithAnonymousUser; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class PantryControllerMvcTest { @Autowired private MockMvc mvc; @Test @WithAnonymousUser public void shouldViewIndexOk() throws Exception { mvc.perform(get("/")).andExpect(status().isOk()); } @Test @WithAnonymousUser public void shouldRedirectFromSettingsToLoginPage() throws Exception { mvc.perform(get("/settings")).andExpect(redirectedUrlPattern("**/oauth2/authorization/google")); } @Test @WithAnonymousUser public void shouldRedirectFromShoppingToLoginPage() throws Exception { mvc.perform(get("/shopping")).andExpect(redirectedUrlPattern("**/oauth2/authorization/google")); } @Test @WithAnonymousUser public void shouldRedirectFromCartToLoginPage() throws Exception { mvc.perform(get("/cart")).andExpect(redirectedUrlPattern("**/oauth2/authorization/google")); } @Test @WithMockUser public void shouldViewSettingsPageOk() throws Exception { mvc.perform(get("/settings")).andExpect(status().isOk()); } } 检索我的用户类,称为resolveUser(OAuth2AuthenticationToken token)。该方法来自抽象类PantryUser

LoginController

LoginController

这是上一个控制器类扩展的package org.wecancodeit.pantryplus2electricboogaloo.controllers; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.transaction.Transactional; import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.wecancodeit.pantryplus2electricboogaloo.cart.Cart; import org.wecancodeit.pantryplus2electricboogaloo.cart.CartRepository; import org.wecancodeit.pantryplus2electricboogaloo.category.CategoryRepository; import org.wecancodeit.pantryplus2electricboogaloo.user.PantryUser; import org.wecancodeit.pantryplus2electricboogaloo.user.UserRepository; @Controller public class PantryController extends LoginController { @Resource private CategoryRepository categoryRepo; @Resource private UserRepository userRepo; @Resource private CartRepository cartRepo; @Resource private EntityManager entityManager; @Transactional @RequestMapping("/settings") public String displayUserForm(Model model, OAuth2AuthenticationToken token) { model.addAttribute("user", resolveUser(token)); return "user-form"; } @Transactional @RequestMapping("/shopping") public String displayShopping(Model model, OAuth2AuthenticationToken token) { model.addAttribute("categories", categoryRepo.findAll()); PantryUser user = resolveUser(token); if(!user.isValid()) { return "redirect:/settings"; } model.addAttribute("cart", user.getCart()); return "shopping"; } @Transactional @RequestMapping("/cart") public String displayCart(Model model, OAuth2AuthenticationToken token) { Cart cart = resolveUser(token).getCart(); model.addAttribute("cart", cart); model.addAttribute("lineItems", cart.getLineItems()); model.addAttribute("countedLineItems", cart.getCountedLineItems()); return "cart"; } @RequestMapping("/about-us") public String displayAboutUs() { return "about-us"; } @Transactional @RequestMapping("/") public String displayWelcomeView(Model model, OAuth2AuthenticationToken token) { boolean isAuthenticated = token != null; model.addAttribute("authenticated", isAuthenticated); if (isAuthenticated) { PantryUser user = resolveUser(token); if(!user.isValid()) { return "redirect:/settings"; } model.addAttribute("user", user); } return "welcome"; } } 。它将LoginController变成我的OAuth2AuthenticationToken

PantryUser

0 个答案:

没有答案