Java 如何测试使用OAuth2保护的简单安全页面?
我有一个简单的应用程序(仅用于演示),它保护了一个页面“/secured”(使用谷歌进行身份验证),我想测试该页面。当我测试该页面时,我得到了错误NullPointerException,因为“oAuth2User”为null Spring安全配置:Java 如何测试使用OAuth2保护的简单安全页面?,java,spring-boot,spring-security-oauth2,spring-test,Java,Spring Boot,Spring Security Oauth2,Spring Test,我有一个简单的应用程序(仅用于演示),它保护了一个页面“/secured”(使用谷歌进行身份验证),我想测试该页面。当我测试该页面时,我得到了错误NullPointerException,因为“oAuth2User”为null Spring安全配置: @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override prot
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**").authorizeRequests()
.antMatchers("/", "/resources/**", "/static/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login();
控制器
@Controller
public class PageControllerSimpleSecurity {
@GetMapping("/secured")
public String securedPage(@AuthenticationPrincipal OAuth2User oAuth2User) {
String userEmail = oAuth2User.getAttributes().get("email").toString();
return "secured";
}
控制器测试:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class PageControllerSimpleSecurityTest {
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser
void itShouldTestSecuredPage() throws Exception {
mockMvc.perform(get("/secured")).andExpect(status().isOk());
}
你能帮助我,如何模仿oAuth2User吗