Java Spring安全性:数据库身份验证提供程序
无法使Spring Security与DB身份验证提供程序一起工作。 内存中的身份验证提供程序工作正常 复制步骤: 当我使用凭据sb、sb登录时,AuthenticationService的登录方法返回false。 Tomcat中没有相关日志 applicationContext.xml: 托管bean级别: applicationContext-security.xml: 假设: 数据源注入org.springframework.security.core.userdetails.jdbc.JdbcDaoImplJava Spring安全性:数据库身份验证提供程序,java,database,authentication,spring-security,provider,Java,Database,Authentication,Spring Security,Provider,无法使Spring Security与DB身份验证提供程序一起工作。 内存中的身份验证提供程序工作正常 复制步骤: 当我使用凭据sb、sb登录时,AuthenticationService的登录方法返回false。 Tomcat中没有相关日志 applicationContext.xml: 托管bean级别: applicationContext-security.xml: 假设: 数据源注入org.springframework.security.core.userdetails.jdbc.J
就使用Hibernate ORM而言,也许应该使用JdbcDaoImpl以外的一些?检查您的空catch块中是否出现异常,这总是一个坏主意。在什么意义上它不起作用?jtoberon,我已经更新了我的帖子。请参阅“复制步骤”部分。什么是AuthenticationService?AuthenticationService只是带有登录方法的简单接口。关于异常,您是对的,我将对此进行调查。jtoberon,谢谢您的提示,我得到了:PreparedStatementCallback;错误的SQL语法[选择用户名、密码,从用户名=?]的用户启用]继续:org.springframework.jdbc.badsqlgramarException:PreparedStatementCallback;错误的SQL语法[选择用户名、密码,从用户名=?]的用户启用;嵌套异常是com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的未知列“enabled”看起来我应该再提供一个名为enabled的字段,但为什么?我根据SpringDocs创建了表用户。哦,我错了,有这样一个字段,叫做enabled:no prob——它通常是最容易浪费时间的东西!
<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&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;
}
public String doLogin() {
boolean isLoggedIn = authenticationService.login(name, password);
if (isLoggedIn) {
return "index";
}
FacesContext.getCurrentInstance().addMessage("login failure", new FacesMessage());
return "failureLogin";
}
<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>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="sb" password="sb" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>