gpt4 book ai didi

java - 当用户在 Spring Security 中被禁用时,如何向客户端显示用户禁用消息

转载 作者:行者123 更新时间:2023-12-02 01:40:48 24 4
gpt4 key购买 nike

我正在使用 Spring Security 对我的应用程序进行身份验证。我想通知客户其帐户已签名但尚未启用。这是 WebSecurityConfigurerAdapter 中的用户配置:

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1")
.password(passwordEncoder().encode("1"))
.authorities("ROLE_USER1")
.and()
.withUser("user2")
.password(passwordEncoder().encode("1"))
.authorities("ROLE_USER2")
.disabled(true)
;
}

我的授权配置:

     @Override
protected void configure(HttpSecurity http) throws Exception {

http
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/public").permitAll()
.antMatchers("/api/private1").hasAuthority("ROLE_USER1")
.antMatchers("/api/private2").hasAuthority("ROLE_USER2")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginNeeded").permitAll()
.loginProcessingUrl("/login")
.defaultSuccessUrl("/")
.failureForwardUrl("/login/failed")
.successForwardUrl("/login/succeed")
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").deleteCookies("JSESSIONID", "account")
;

}

user2 已禁用。我知道 Spring Security 中有一个 DisabledException ,但是当我尝试使用禁用的 user2 登录时,我无法得到这个异常。看来 Spring 会像任何其他登录失败尝试一样考虑禁用用户。

我使用 postman 作为客户端。

谁能帮忙解决这个问题吗?

提前致谢

最佳答案

在这种情况下,您应该尝试使用 AuthenticationFailureHandler(例如 TokenAuthenticationFailureHandler 等)来实现 Spring 安全性。此处理程序可用于修改每种情况的错误消息,以便您区分发生的错误类型。

关于java - 当用户在 Spring Security 中被禁用时,如何向客户端显示用户禁用消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57489792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com