【问题标题】:Authentication using Spring 3 and Hibernate 3 (annotation based)使用 Spring 3 和 Hibernate 3 进行身份验证(基于注释)
【发布时间】:2012-10-26 14:57:34
【问题描述】:

我的应用程序基于 Spring 3、Hibernate 3、MySQL。我阅读了 Spring Security 文档并了解到我可以实现如下所示的身份验证,

<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>

我了解以上部分,但我担心的是,在我的应用程序中,用户表不仅存储用户名、密码和启用字段。它还存储名字和姓氏、emailID、电话等。在成功验证后,我希望下一个 jsp 自动填充所有用户详细信息,而不是向用户询问与未注册用户相同的信息。

  1. 我想使用基于注解的配置,而不是基于 xml 的配置(不像 spring 2.5 示例中提到)
  2. spring 文档不使用休眠来保证安全。我应该使用 休眠或 jdbc 用户服务?如果休眠,那怎么办?
  3. 我看到几个使用自定义 UserService 的示例。是我需要的吗 也这样做?

有人可以提供一些好的例子吗?对其他帖子的任何引用也会有所帮助。

【问题讨论】:

    标签: database spring hibernate spring-mvc spring-security


    【解决方案1】:

    目前 Spring Security 大部分配置没有注解,但是你可以使用特殊的 XML 命名空间来简单定制 Spring Security。

    您需要实现自己的UserDetailsService,它将负责从持久存储中加载用户的详细信息。

    UserDetailsService 接口只有一种方法

    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
    

    实现并创建组件的 bean 后,您需要注入它:

    <authentication-manager>
       <authentication-provider user-service-ref="myUserDetailsService"/>
    </authentication-manager>
    

    Spring 安全文档中的更多内容:

    http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#tech-userdetailsservice

    http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#nsa-authentication-provider

    如果您的数据库中没有enabled 之类的信息,您将始终必须将true 值放入该字段的UserDetails 对象中。

    【讨论】:

    猜你喜欢
    • 2012-01-24
    • 2012-11-18
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多