首页>代码>Spring Security通过内存配置的用户账号实现登录验证的实例>/inmemory/src/test/java/com/memorynotfound/spring/security/test/InMemoryHttpBasicIT.java
package com.memorynotfound.spring.security.test;

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.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@RunWith(SpringJUnit4ClassRunner.class)
public class InMemoryHttpBasicIT {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void accessProtected() throws Exception {
        this.mockMvc.perform(get("/"))
                .andExpect(unauthenticated())
                .andExpect(status().isUnauthorized());
    }

    @Test
    public void loginUser() throws Exception {
        this.mockMvc.perform(get("/")
                .with(httpBasic("zuidaima", "111111")))
                .andExpect(status().isOk());
    }

    @Test
    public void loginInvalidUser() throws Exception {
        this.mockMvc.perform(formLogin().user("invalid").password("invalid"))
                .andExpect(unauthenticated());
    }

}
最近下载更多
15097136964  LV7 2021年6月17日
18070630131  LV6 2020年11月29日
最代码官方  LV167 2020年11月28日
最近浏览更多
quartz  LV7 3月13日
GJQ123  LV4 2023年12月8日
漫步的海星  LV4 2023年9月21日
zxc131313  LV12 2023年4月8日
develop  LV10 2022年6月11日
dhvdsgfggfjhfggfyu  LV4 2022年6月1日
刘亦菲9527  LV15 2022年5月24日
nbtest  LV1 2022年3月1日
houyue0074 2022年1月2日
暂无贡献等级
君知否  LV17 2021年11月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友