当前位置: 首页 > 知识库问答 >
问题:

如何使用Spring 3.2 new mvc测试登录用户

秦伯寅
2023-03-14

这很好,直到我必须测试一个需要登录用户的服务,我如何将用户添加到上下文:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
@WebAppConfiguration
public class FooTest {
@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Resource(name = "aService")
private AService aService; //uses logged in user

@Before
public void setup() {
    this.mockMvc = webAppContextSetup(this.webApplicationContext).build();
}

共有3个答案

白越
2023-03-14

您应该能够将用户添加到安全上下文:

List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
list.add(new GrantedAuthorityImpl("ROLE_USER"));        
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, password,list);
SecurityContextHolder.getContext().setAuthentication(auth);
商茂勋
2023-03-14

如果成功的身份验证产生了一些cookie,那么您可以捕获这些cookie(或者只是所有cookie),并在接下来的测试中传递这些cookie:

@Autowired
private WebApplicationContext wac;

@Autowired
private FilterChainProxy filterChain;

private MockMvc mockMvc;

@Before
public void setup() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
      .addFilter(filterChain).build();
}

@Test
public void testSession() throws Exception {
    // Login and save the cookie
    MvcResult result = mockMvc.perform(post("/session")
      .param("username", "john").param("password", "s3cr3t")).andReturn();
    Cookie c = result.getResponse().getCookie("my-cookie");
    assertThat(c.getValue().length(), greaterThan(10));

    // No cookie; 401 Unauthorized
    mockMvc.perform(get("/personal").andExpect(status().isUnauthorized());

    // With cookie; 200 OK
    mockMvc.perform(get("/personal").cookie(c)).andExpect(status().isOk());

    // Logout, and ensure we're told to wipe the cookie
    result = mockMvc.perform(delete("/session").andReturn();
    c = result.getResponse().getCookie("my-cookie");
    assertThat(c.getValue().length(), is(0));
}

虽然我知道我没有在这里发出任何HTTP请求,但我有点喜欢上面的集成测试与我的控制器和Spring Security实现之间更严格的分离。

为了让代码不那么冗长,我在发出每个请求后使用以下方法合并cookie,然后在每个后续请求中传递这些cookie:

/**
 * Merges the (optional) existing array of Cookies with the response in the
 * given MockMvc ResultActions.
 * <p>
 * This only adds or deletes cookies. Officially, we should expire old
 * cookies. But we don't keep track of when they were created, and this is
 * not currently required in our tests.
 */
protected static Cookie[] updateCookies(final Cookie[] current,
  final ResultActions result) {

    final Map<String, Cookie> currentCookies = new HashMap<>();
    if (current != null) {
        for (Cookie c : current) {
            currentCookies.put(c.getName(), c);
        }
    }

    final Cookie[] newCookies = result.andReturn().getResponse().getCookies();
    for (Cookie newCookie : newCookies) {
        if (StringUtils.isBlank(newCookie.getValue())) {
            // An empty value implies we're told to delete the cookie
            currentCookies.remove(newCookie.getName());
        } else {
            // Add, or replace:
            currentCookies.put(newCookie.getName(), newCookie);
        }
    }

    return currentCookies.values().toArray(new Cookie[currentCookies.size()]);
}

...还有一个小助手,cookie(…) 至少需要一个cookie:

/**
 * Creates an array with a dummy cookie, useful as Spring MockMvc
 * {@code cookie(...)} does not like {@code null} values or empty arrays.
 */
protected static Cookie[] initCookies() {
    return new Cookie[] { new Cookie("unittest-dummy", "dummy") };
}

...以:

Cookie[] cookies = initCookies();

ResultActions actions = mockMvc.perform(get("/personal").cookie(cookies)
  .andExpect(status().isUnauthorized());
cookies = updateCookies(cookies, actions);

actions = mockMvc.perform(post("/session").cookie(cookies)
  .param("username", "john").param("password", "s3cr3t"));
cookies = updateCookies(cookies, actions);

actions = mockMvc.perform(get("/personal").cookie(cookies))
  .andExpect(status().isOk());
cookies = updateCookies(cookies, actions);

马清野
2023-03-14

如果要在最新的Spring Security测试包中使用MockMVC,请尝试以下代码:

Principal principal = new Principal() {
        @Override
        public String getName() {
            return "TEST_PRINCIPAL";
        }
    };
getMockMvc().perform(get("http://your-url.com").principal(principal))
        .andExpect(status().isOk()));

请记住,要使其工作,必须使用基于主体的身份验证。

 类似资料:
  • 我们有一个应用程序,有一个关键斗篷登录。我想创建一个JMeter测试,它使用一些凭据登录,而不是做一些事情。问题是我不知道如何形成帖子URL

  • 问题内容: 我正在为我的Flask Web应用程序编写一些单元测试,并且试图测试匿名用户发出的请求和登录用户之间的响应差异。 我正在使用扩展程序来实现用户登录/注销。 显然,我可以执行匿名请求,但是如何模拟已登录用户的请求? 我认为发送标头中的cookie 就足够了,但是它不起作用。 会话Cookie值是我从浏览器中的真实登录名获得的值。 问题答案: Flask-Login 在会话中寻找,你可以使

  • 问题内容: 我正在为Flask Web应用程序编写一些单元测试,并试图测试匿名用户发出的请求和登录用户之间的响应差异。 我正在使用扩展程序来实现用户登录/注销。 显然,我可以执行匿名请求,但是如何模拟已登录用户的请求? 我认为发送标头中的cookie就足够了,但是它不起作用。 会话cookie值是我从浏览器中的真实登录名获得的值。 我想念什么? 问题答案: Flask- Login在会话中寻找,您

  • 我现在有一个小应用程序,它使用Keycloak来拥有一个SSO。但是,当我想对main.component.ts进行ng测试时,我遇到了标准的'It Wall Create'测试失败的问题,我得到了以下错误消息: 从“./main.component”导入{MainComponent};从“../app.topbar.component”导入{AppTopBarComponent};从'../ap

  • 我是一个新手在jeter工具。我想测试登录到一个asp.net网站。但是我在网上搜索了2天后没有成功。 我在下面列出了我遵循的步骤: > 添加一个。 添加一个。 为EVENTVALIDATION和VIEWSTATE添加两个正则表达式提取器。 添加HTTP URL重写修改器与(检查和)。 有两个页面,一个是,另一个是。在Login Post Page中,我添加了以下参数: 用户名: __EVENTV

  • 问题内容: 我们有一个Java EE 7应用程序,并使用Arquillian进行测试。现在,我们要检查当前登录用户的某些权限。我的问题很基本,在测试用例中时如何登录用户?我已经阅读了ProgrammaticLogin在Arquillian测试和Embedded Glassfish,安全性和Arquillian问题中不起作用,但是并没有明确回答。我当前的方法是这样的: 现在,当我尝试运行此命令时,将