Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何测试使用OAuth2保护的简单安全页面?_Java_Spring Boot_Spring Security Oauth2_Spring Test - Fatal编程技术网

Java 如何测试使用OAuth2保护的简单安全页面?

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

我有一个简单的应用程序(仅用于演示),它保护了一个页面“/secured”(使用谷歌进行身份验证),我想测试该页面。当我测试该页面时,我得到了错误NullPointerException,因为“oAuth2User”为null

Spring安全配置:

@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吗