写在前面
记录一次springSecurity测试登陆,退出接口
一、测试代码
import org.junit.Before;
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.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import javax.servlet.http.Cookie;
import static org.junit.Assert.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/** * 单元测试 - 登录模块 */
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@AutoConfigureMockMvc
public class LoginControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.build();
}
/** * 测试登陆 * 失败 * @throws Exception */
@Test
@WithMockUser(username = "zx", password = "11111111")
public void login() throws Exception {
mockMvc
.perform(formLogin("/auth/login")/*.user("zx").password("11111111")*/
.acceptMediaType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
// 测试登录成功
/** * 测试登录 * 成功 * @throws Exception */
@Test
public void login2() throws Exception {
mockMvc
.perform(formLogin("/auth/login").user("zx").password("11111111")
.acceptMediaType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
// 测试登录成功
/** * 测试登录 * 失败 * @throws Exception */
@Test
@WithUserDetails(value = "zx", userDetailsServiceBeanName = "userDetailsService")
public void login3() throws Exception {
mockMvc
.perform(formLogin("/auth/login")/*.user("zx").password("11111111")*/
.acceptMediaType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
@Test
public void login4() throws Exception {
mockMvc
.perform(formLogin("/auth/login")/*.user("zx").password("11111111")*/
.acceptMediaType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
// 测试登陆失败
@Test
public void loginError() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/auth/login")
// .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"username\": \"zxx\",\n" +
" \"passwoed\": 111,\n" +
"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(authenticated());
}
@Test
public void account() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.get("/auth/account")
.cookie(new Cookie(LoginController.HEADER, "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6eCIsImV4cCI6MTU4MzQyMzE4MH0.Gwl-FfLcZUXGbi69BtFXSbb5YlawPqB1R8Awwrzh9MDrRK1vegY1y4VAuihNBvKiR1E8mkzxLRqpZXMGZSvS_g")))
// .andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
// 测试退出成功
@Test
@WithUserDetails(value = "zx", userDetailsServiceBeanName = "userDetailsService")
public void logout() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.get("/auth/logout")
.cookie(new Cookie(LoginController.HEADER, "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6eCIsImV4cCI6MTU4MzQyMzE4MH0.Gwl-FfLcZUXGbi69BtFXSbb5YlawPqB1R8Awwrzh9MDrRK1vegY1y4VAuihNBvKiR1E8mkzxLRqpZXMGZSvS_g")))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
// 测试退出成功
@Test
@WithMockUser(username = "zx", password = "11111111")
public void logoutTest2() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.get("/auth/logout")
.cookie(new Cookie(LoginController.HEADER, "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6eCIsImV4cCI6MTU4MzQyMzE4MH0.Gwl-FfLcZUXGbi69BtFXSbb5YlawPqB1R8Awwrzh9MDrRK1vegY1y4VAuihNBvKiR1E8mkzxLRqpZXMGZSvS_g")))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
}
}
二、Debug 截图
三、注意
spring security 除了测试登陆需要完整的用户名密码外,其他所有的接口权限,只需用户名认证,即可完成相应的权限信息