【发布时间】:2015-07-29 09:17:20
【问题描述】:
我是 Spring Security 的新手,我想使用数据库对用户进行身份验证。 我已经使用 jdbc 创建了一个登录页面和一个身份验证提供程序,用于检查用户是否存在于数据库中。 但是我的代码没有这样做的问题,它允许所有用户登录! 我的代码有什么问题? 感谢您的帮助。
@Component(value = "userService")
public class UserService implements AuthenticationProvider {
@Inject
@Named(value = "dataSource")
private DataSource dataSource;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
name=auth.getName();
pwd=auth.getCredentials().toString();
public Authentication authenticate(Authentication auth)
throws AuthenticationException {
final String select_auth = "select username,password from users where username='"+name+"' and password='"+pwd+"'";
try {
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(select_auth);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
//what to return here ?
}
这是我的 security-confg.xml:
<http auto-config="true">
<form-login login-page="/login" username-parameter="j_username"
password-parameter="j_password" default-target-url="/accueil"
authentication-failure-url="/403" />
<logout logout-success-url="/login" />
</http>
<authentication-manager>
<authentication-provider ref="userService">
</authentication-provider>
</authentication-manager>
【问题讨论】:
标签: java spring-mvc spring-security