【问题标题】:Spring Security:DataBase authentication providerSpring Security:数据库身份验证提供程序
【发布时间】:2012-01-15 00:08:35
【问题描述】:

无法让 Spring Security 与 DB 身份验证提供程序一起使用。
内存中身份验证提供程序工作正常。

复制步骤:
当我使用凭据登录时 sb,sb,login()AuthenticationService 方法返回 false
Tomcat没有相关日志。

applicationContext.xml:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/chirokDB?useUnicode=true&amp;characterEncoding=utf8"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

<bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
        <property name="dataSource" ref="dataSource"/>
</bean>

服务层:

@Service("authenticationService")
   public class AuthenticationServiceImpl implements AuthenticationService {
    @Resource(name = "authenticationManager")
    private AuthenticationManager authenticationManager;
        public boolean login(String username, String password) {
        try {
        Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                    username, password));
            if (authenticate.isAuthenticated()) {
    SecurityContextHolder.getContext().setAuthentication(authenticate);
                    return true;
                }
            } catch (AuthenticationException e) {
            }
            return false;
   }

托管 bean 级别:

public String doLogin() {
    boolean isLoggedIn = authenticationService.login(name, password);
    if (isLoggedIn) {
        return "index";
    }
    FacesContext.getCurrentInstance().addMessage("login failure", new FacesMessage());
    return "failureLogin";
}

applicationContext-security.xml:

<global-method-security pre-post-annotations="enabled"/>  
    <http auto-config="true">
    <form-login login-page="/login.xhtml" default-target-url="/index.xhtml"/>
        <intercept-url pattern="/contacts.xhtml" access="ROLE_ANONYMOUS,ROLE_USER"/>
        <intercept-url pattern="/delivery.xhtml" access="ROLE_USER"/>
        <logout invalidate-session="true"/>
        <session-management>
            <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
        </session-management>   
    </http>          

    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"/>
        </authentication-provider>
    </authentication-manager>

持久性级别:
MySql DB 具有以下标准表(Spring 要求):
1. 用户
2. 当局

users 表有记录,用户名='sb',密码='sb'
authorities 表有记录,用户名='sb',权限='ROLE_USER'

注意
使用用户内存在以下配置下一切正常:

    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="sb" password="sb" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

假设:
dataSource 注入到org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl
就使用 Hibernate ORM 而言,也许应该使用 JdbcDaoImpl 以外的其他 ORM?

【问题讨论】:

  • 在什么意义上不起作用?
  • jtoberon,我已经更新了我的帖子。请参阅“复制步骤”部分。
  • 什么是 AuthenticationService?
  • AuthenticationService 只是一个简单的接口与 login() 方法。关于异常,你是对的,我会调查这个。

标签: java database authentication spring-security provider


【解决方案1】:

检查您是否在空的 catch 块中收到 Exception(这总是一个坏主意)。

【讨论】:

  • jtoberon,谢谢你的提示,我得到了:PreparedStatementCallback; bad SQL grammar [select username,password,enabled from users where username = ?]
  • continue:org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select username,password,enabled from users where username = ?]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'enabled' in 'field list' 看起来我应该再提供一个字段,称为enabled,但为什么呢?我根据 Spring 文档创建了表用户。
  • 哦,我错了,有这样一个字段,叫enabled:static.springsource.org/spring-security/site/docs/3.0.x/…
  • 没有问题——通常是最简单的东西浪费了最多的时间!
猜你喜欢
  • 2012-02-18
  • 1970-01-01
  • 2019-07-16
  • 2012-03-07
  • 2011-02-09
  • 2013-04-25
  • 1970-01-01
  • 2016-07-19
  • 2016-05-23
相关资源
最近更新 更多