Spring Security 认证成功,密码错误

Raj*_*ami 6 authentication spring spring-security spring-boot

我的网络安全配置如下所示;

@EnableWebSecurity
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder builder) throws Exception {
        builder.inMemoryAuthentication().withUser("hellouser")
                .password("hellopass").roles("USER");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我提供错误的用户名时,身份验证会按预期失败。但是,如果我在身份验证中成功一次,则此后所有其他密码错误但用户名正确的请求都将成功通过身份验证....

它是否被缓存在某处?

我可以禁用此功能吗?

是不是假设密码错误会导致身份验证失败?

注意:我正在学习 spring-security。我在这个应用程序中没有任何 html 页面,也没有来自 PostMan 的测试。

小智 5

使用 http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 在配置方法中。


小智 4

即使凭证错误,我也能够使用 Postman 的基本身份验证从下面的配置访问 URL,这种情况发生是因为一旦您提供了正确的凭证,凭证就会存储在会话中,即使您重复相同的请求,也会使用相同的会话来访问 URL。

http
    .httpBasic()
        .and()
    .authorizeRequests()
        .antMatchers("/secure/admin/**").hasRole("ADMIN")
        .antMatchers("/api/**","/secure/getUserByName/**").hasAnyRole("USER","ADMIN")
        .anyRequest().fullyAuthenticated();
Run Code Online (Sandbox Code Playgroud)

解决方案:

http
    .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
Run Code Online (Sandbox Code Playgroud)

只需添加上面的代码即可。因此,此配置可确保一次仅对用户的单个实例进行身份验证。如果同一用户尝试访问该 URL,则其先前的会话将终止,然后该用户必须再次提供登录凭据才能创建新会话。