【问题标题】:Can i add HIbernate functionality with spring security我可以添加带有弹簧安全性的 HIbernate 功能吗
【发布时间】:2014-02-20 20:51:15
【问题描述】:
我可以添加带有 spring security 的休眠功能,以便对于下面编写的代码,我可以添加一些休眠功能,以便它直接检查数据库中的用户名和密码,以查看下面显示的代码。
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="ananth" password="password" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
【问题讨论】:
标签:
hibernate
spring-mvc
spring-security
【解决方案1】:
如果您想通过数据库对用户进行身份验证:
<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, authorities ur
where u.user_id = ur.user_id and u.username =? "
/>
</authentication-provider>
</authentication-manager>
您的数据库如下所示:
create table users(
username varchar_ignorecase(50) not null primary key,
password varchar_ignorecase(50) not null,
enabled boolean not null);
create table authorities (
username varchar_ignorecase(50) not null,
authority varchar_ignorecase(50) not null,
constraint fk_authorities_users foreign key(username) references users(username));
create unique index ix_auth_username on authorities (username,authority);
来自official Spring Security doc
【解决方案2】:
您需要实现UserDetailsService。在线阅读各种教程,了解如何实现 UserDetailsService。
实现 UserDetailsService 后,将其用作您的身份验证提供程序。
您的身份验证管理器将如下所示:
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
</authentication-manager>
'userDetailsService' 是实现了 UserDetailService 的 bean。