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

Spring Security测试期间的空身份验证

冯元魁
2023-03-14

我想集成测试Spring Security认证。只要登录有效。但在测试过程中出现以下错误:

java.lang.AssertionError: Authentication should not be null

我的自定义UserDetailsService只是从数据库中获取用户并将其映射到我的自定义UserDetailsService。我在这里做错了什么?这是我还有什么:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Application.class})
@WebAppConfiguration
public class SecurityConfigTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders
            .webAppContextSetup(context)
            .apply(springSecurity())
            .build();
    }
    @Test
    public void shouldAuthenticate() throws Exception {
        mockMvc
            .perform(formLogin())
            .andExpect(authenticated());
    }
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .userDetailsService(userDetailsService)
            .passwordEncoder(new BCryptPasswordEncoder());
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http
            .httpBasic()
            .and()
            .authorizeRequests()
                .antMatchers("/", "/login").permitAll()
            .and()
            .csrf().disable();
    }
}
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

共有1个答案

薛望
2023-03-14

如Spring Security参考手册中所述,mockMvc。执行(formLogin())

将使用用户名“user”、密码“password”和有效的CSRF令牌向“/登录”提交帖子。

因此,如果您的登录URL不是/login,用户名不是user,或者密码不是密码,您不应该期望它工作。

要为数据库中实际存在的用户提供用户名/密码组合,您应该能够使用mockMvc。执行(formLogin(“/auth”)。用户(“管理员”)。密码(“pass”)(假设登录URL为auth,则用户名为admin,密码为pass)。

有关更多详细信息和示例,请参阅参考手册。

 类似资料:
  • 问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe

  • 我想测试一个WSO2身份提供程序,实际上是它用OpenID配置的联合IdP,我计划使用TraSpeed工具对其进行测试。我正在WEB-INF配置中编辑travelocity.properties文件,将起始参数设置为以下值: 在Tomcat7服务器中声明映像时,它接受新的服务器配置,但代码仍将SAML身份验证激活为主要SSO机制,抛出错误: 是否可以仅使用OpenID堆栈在该Web工具中停用SAM

  • 问题内容: 我正在开发Flask应用程序,并使用Flask-security进行用户身份验证(反过来又在下面使用Flask-login)。 我有一条需要身份验证的路由。我正在尝试编写一个单元测试,该测试对经过身份验证的用户返回适当的响应。 在单元测试中,我正在创建一个用户并以该用户身份登录,如下所示: 在测试内返回正确的用户。但是,请求的视图始终返回的。 所述路线定义为: 我可以肯定我只是不完全了

  • 要做什么? 我们在 Sharepoint(企业)服务器上部署了一个应用程序,该应用程序使用 Windows 凭据登录到该应用程序。 > 应用程序URL格式:http://testmachine:1000/sites/test/ 视窗凭据格式: user_id@domain.co.in 目标是为如此数量的用户在应用程序上执行负载/性能测试(尤其是登录功能)。 通常,当我在Firefox/IE中点击应

  • 看起来它与我们在正常项目中的websecurity配置有冲突。这是文件: 我尝试添加来修复上述问题,但最终仍然以结束

  • 我用的是Spring boot Spring Security和Thymeleaf。目前,身份验证和授权工作良好时访问页面,但我不能使thymeleaf授权工作。流程非常简单:我尝试访问admin.html页面,对于该页面,我需要一个“admin”角色。Spring Security正确地截取了请求,首先将我转发到登录页面,当以管理员身份登录时,它允许我继续。 现在我想根据角色“隐藏”管理页面上的