我正在使用 Spring Security,用户通过 JDBC 存储在我的数据库中。我也在使用 Spring MVC。
注册用户类:
@Entity
public class RegisteredUser extends User implements Serializable {
protected Long id;
private static final long serialVersionUID = -8500791712795093944L;
private String ssn;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
public RegisteredUser (String username, String password, String ssn,
boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
Collection<? extends GrantedAuthority> authorities)
{
super(username, password, enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, authorities);
this.setSsn(ssn);
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn= ssn;
}
}
我的 security-config.xml 的一部分。数据库中存在“用户”和“用户角色”表。
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="
select username,password, enabled
from users where username=?"
authorities-by-username-query="
select u.username, ur.authority from users u, user_roles ur
where u.user_id = ur.user_id and u.username =? " />
</authentication-provider>
</authentication-manager>
当我尝试保存一个新用户时,我存储了 RegisteredUser 信息(即 ssn),但是用户表中没有插入任何条目,因此用户名和密码没有存储在数据库中,并且 RegisteredUser 的条目没有引用任何条目用户表。
我保存的方法是在 RegisteredUserRepository 类型的自动装配属性registeredUserRepo 上调用保存:
@Repository
public interface RegisteredUserRepositoryextends
PagingAndSortingRepository<RegisteredUser , Long> {
public RegisteredUser findByUsername(String username);
}
在我的数据库中,我有这个:
--
-- Table structure for table `roles`
--
CREATE TABLE IF NOT EXISTS `roles` (
`role_id` int(11) NOT NULL,
`rolename` varchar(30) NOT NULL,
PRIMARY KEY (`role_id`),
UNIQUE KEY `rolename` (`rolename`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `roles` (`role_id`, `rolename`) VALUES
(1, 'ROLE_AMM'),
(2, 'ROLE_DIP'),
(3, 'ROLE_RES');
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(10) unsigned NOT NULL,
`username` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`enabled` tinyint(1) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `user_roles` (
`user_role_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`authority` varchar(45) NOT NULL,
PRIMARY KEY (`user_role_id`),
KEY `FK_user_roles` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `registereduser` (
`DTYPE` varchar(31) NOT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`sn` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
我哪里错了?