database - 使用 Hibernate 的 Spring Security 3 数据库身份验证

标签 database hibernate authentication spring-security

我需要从数据库对用户进行身份验证,Spring Security 文档没有说明如何使用 hibernate 进行身份验证。这可能吗?我该怎么做?

最佳答案

您必须创建自己的自定义身份验证提供程序。

示例代码:

从 Hibernate 加载用户的服务:

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;    

@Service("userDetailsService") 
public class UserDetailsServiceImpl implements UserDetailsService {

  @Autowired private UserDao dao;
  @Autowired private Assembler assembler;

  @Transactional(readOnly = true)
  public UserDetails loadUserByUsername(String username)
      throws UsernameNotFoundException, DataAccessException {

    UserDetails userDetails = null;
    UserEntity userEntity = dao.findByName(username);
    if (userEntity == null)
      throw new UsernameNotFoundException("user not found");

    return assembler.buildUserFromUserEntity(userEntity);
  }
}

将您的实体转换为 spring 用户对象的服务:

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;

@Service("assembler")
public class Assembler {

  @Transactional(readOnly = true)
  User buildUserFromUserEntity(UserEntity userEntity) {

    String username = userEntity.getName();
    String password = userEntity.getPassword();
    boolean enabled = userEntity.isActive();
    boolean accountNonExpired = userEntity.isActive();
    boolean credentialsNonExpired = userEntity.isActive();
    boolean accountNonLocked = userEntity.isActive();

    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    for (SecurityRoleEntity role : userEntity.getRoles()) {
      authorities.add(new GrantedAuthorityImpl(role.getRoleName()));
    }

    User user = new User(username, password, enabled,
      accountNonExpired, credentialsNonExpired, accountNonLocked, authorities, id);
    return user;
  }
}

基于命名空间的 application-context-security.xml 看起来像:

<http>
  <intercept-url pattern="/login.do*" filters="none"/>
  <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
  <form-login login-page="/login.do"
              authentication-failure-url="/login.do?error=failed"
              login-processing-url="/login-please.do" />
  <logout logout-url="/logoff-please.do"
          logout-success-url="/logoff.html" />
</http>

<beans:bean id="daoAuthenticationProvider"
 class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
  <beans:property name="userDetailsService" ref="userDetailsService"/>
</beans:bean>

<beans:bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
  <beans:property name="providers">
    <beans:list>
      <beans:ref local="daoAuthenticationProvider" />
    </beans:list>
  </beans:property>
</beans:bean>

<authentication-manager>
  <authentication-provider user-service-ref="userDetailsService">
    <password-encoder hash="md5"/>
  </authentication-provider>
</authentication-manager>

关于database - 使用 Hibernate 的 Spring Security 3 数据库身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2683308/

相关文章:

java - Ehcache 二级缓存不适用于 JPA 和 Hibernate?

javascript - 以下使用 JavaScript 处理 html &lt;input&gt; 是否会带来任何风险?

mysql - 使用模板和 MySql 登录 Django

sql-server - SQL Server 最佳实践 - 如何管理一个大型、用途广泛的表的索引

C# MySQL数据库相关

mysql - 导入和计数后行数不同

java - 如何使用@DateTimeFormat在Spring表单中绑定(bind)日期字段?

spring - org.hibernate.UnknownEntityTypeException : Unable to locate persister: entity. 设置

php - 重定向回页面时保持弹出框显示

mysql - 我在连接3个MySQL表时遇到问题,如何改善语法?