2011-12-07 65 views
0

无法使Spring Security与数据库身份验证提供程序一起使用。
内存认证提供程序正常工作。Spring Security:数据库身份验证提供程序

步骤重现:
当我登录凭证与sbsb,的AuthenticationServicelogin()方法返回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'


与用户内存中所有可与以下配置OK:

<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以外的其他应用程序?

+0

从何种意义上说它不起作用? – jtoberon

+0

jtoberon,我已更新我的帖子。请参阅“步骤重现”部分。 – sergionni

+0

什么是AuthenticationService? – jtoberon

回答

1

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

+0

jtoberon,谢谢你的提示,我有:'PreparedStatementCallback;错误的SQL语法[select username,password,enabled from users where username =?]' – sergionni

+0

continue:'org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback;错误的SQL语法[选择用户名,密码,从用户名=?开启];嵌套异常是com.mysql.jdbc.exceptions.jdbc4。MySQLSyntaxErrorException:'字段列表'中的未知列'enabled'看起来我应该提供一个字段,称为'enabled',但为什么?我根据Spring文档创建了表用户。 – sergionni

+0

哦,我错了,有这样的领域,称为'enabled':http://static.springsource.org/spring-security/site/docs/3.0.x/reference/appendix-schema.html – sergionni

相关问题