7

SpringSecurity 是否有一些内置功能可以忽略用户名的字母大小写?例如,如果用户名是“student001”,那么它将接受“Student001”以及“stUdent001”。

我需要这个的原因是我们的系统使用电子邮件作为用户名。当然我可以通过扩展 DAOAuthenticationProvider 类来做到这一点,但我只是想知道这个问题是否存在任何内置选项?

4

3 回答 3

6

如果您使用的是,DaoAuthenticationProvider那么我认为您正在使用JdbcDaoImpl它,它从 JDBC 数据库加载用户。

如果是这样,您可以通过自己手动创建 bean 来覆盖JdbcDaoImpl用于查找用户的 SQL 查询。Spring Security 使用的默认查询是:

select username,password,enabled from users where username = ?

您可以使用 SQL lower 函数忽略大小写:

select username,password,enabled from users where lower(username) = lower(?)

适当的 Spring Security XML 配置是:

<bean id="org.springframework.security.authenticationManager" class="org.springframework.security.authentication.ProviderManager">
    <property name="providers">
        <list>
            <ref bean="daoAuthenticationProvider"/>
        </list>
    </property>
</bean>

<bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <property name="userDetailsService" ref="caseInsensitiveUserDetailsService"/>
</bean>

<bean id="caseInsensitiveUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
    <property name="usersByUsernameQuery" value="select username, password, enabled from users where lower(username) = lower(?)" />
</bean>
于 2012-06-07T04:42:20.407 回答
4

我相信任何身份验证提供程序都会利用 UserDetails 和 UserDetailsS​​ervice 接口。

当一个实施

UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;

是为自定义应用程序特定 UserDetailsService的,我们可以忽略的情况username并提供UserDetails给 spring-security 以继续进行进一步的身份验证/授权。

但是,如果org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl使用提供的弹簧,因为 UserDetailsService它将user使用条件从表中加载用户详细信息"where username=?"。所以它是区分大小写的。

于 2012-06-07T04:33:20.023 回答
1

古奇部分正确。它允许使用 JdbcDaoImpl 的用户对用户表进行不区分大小写的检查。但是您将要求 Authorities 表查询也需要更改。

<bean id="caseInsensitiveUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
    <property name="usersByUsernameQuery" value="select username, password, enabled from users where lower(username) = lower(?)" />
    <property name="authoritiesByUsernameQuery" value="select username,authority " +
        "from authorities where lower(username) = lower(?)" />
</bean>
于 2015-05-27T00:13:10.647 回答