【问题标题】:Spring Security 5.1.5 with WebFlux user disable not working带有 WebFlux 用户禁用的 Spring Security 5.1.5 不起作用
【发布时间】:2019-11-21 04:25:27
【问题描述】:

我正在尝试使用 Spring WebFlux 框架和 MongoDB 实现 Web 应用程序。一切都按预期工作,但即使 enabled 属性在数据库中设置为 false,它仍然允许我成功登录。不应该是这样。我的安全配置如下 -

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class WebfluxSecurityConfig {

    @Autowired
    private Constants constants;
    @Autowired
    private UserRepository userRepo;

    @Bean
    public ReactiveUserDetailsService userDetailsService(UserRepository users) {
         return (username) -> users.findByUsername(username)
                    .map(u -> new UserAuth(u.getUserId()
                            , u.getUsername()
                            , u.getPassword()
                            , u.isEnabled()
                            , !u.isAccountExpired()
                            , !u.isCredentialsExpired()
                            , !u.isAccountLocked()
                            , UserAuth.getGrantedAuthorities(u.getRoles().toArray(new String[0]))
                            , StringUtils.isEmpty(u.getAuthSalt()) ? u.getUsername() : u.getAuthSalt()
                        )
                    );
    }

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain3(ServerHttpSecurity http) {
         http
          .authorizeExchange()
              .pathMatchers("/web/**").authenticated()
              .pathMatchers("/**").permitAll()
          .and().formLogin()
                .loginPage("/login")
          .and().logout()
                .logoutUrl("/logout");

         return http.build();
    }
}

mongodb中的用户入口是-

{
    "_id": "5d149e3b3c1206008cf56af9",
    "username": "admin",
    "password": "{noop}admin",
    "firstName": "Admin",
    "lastName": "1",
    "email": "admin@eightfolds.in",
    "enabled": false,
    "accountLocked": true,
    "roles": ["ADMIN"]
}

谁能帮我理解我做错了什么。

还是想不通。

【问题讨论】:

    标签: java spring spring-security spring-webflux


    【解决方案1】:

    感谢您报告此事。
    您的配置没有任何问题。检查用户帐户是否被禁用的行为还不是 WebFlux 的一部分。
    我在 spring-security GitHub repo 上创建了一个issue 来添加这个功能。

    【讨论】:

    • 感谢您的意见。我想在这种情况下,除了等待弹簧团队修复错误之外,我们无事可做。你能建议我现在的任何权宜之计解决方案吗?
    • @Krishnendu 您使用的是哪个版本的 spring-security?
    • 我相信它是 Spring security 5.1.5,因为我使用的是 spring-boot-starter-security 2.1.6
    • @Krishnendu 如果你想试用 spring-security 5.2.0.M3,那么你可以在UserDetailsRepositoryReactiveAuthenticationManager 上使用新方法setPostAuthenticationChecks 并自己设置启用检查。
    • 如果您继续使用 5.1.5,您可以创建自定义 ReactiveAuthenticationManager 扩展 UserDetailsRepositoryReactiveAuthenticationManager 并覆盖 authenticate 方法以包含启用的检查。
    猜你喜欢
    • 2022-12-15
    • 2018-12-12
    • 2021-05-06
    • 2016-06-30
    • 2019-09-27
    • 1970-01-01
    • 2020-11-21
    • 2019-12-06
    • 2020-10-02
    相关资源
    最近更新 更多