作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我是一名优秀的程序员,十分优秀!