我需要对数据库中的用户进行身份验证,Spring安全文档不会告诉我如何使用hibernate进行身份验证.这可能吗?我该怎么做?

推荐答案

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

Example code:

Service to load Users from 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);
  }
}

Service to convert your entity to a spring user object:

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

The namespace-based application-context-security.xml would look something like:

<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相关问答推荐

TYPO3 OOPS,出现错误!编码:202402180809040864ba5c

使用mongoose 在嵌套对象中查找特定字段

prisma 中的隐式或显式多对多关系

用于在 Excel 中的两个列表之间进行筛选的辅助列

使用 Java 对 mysql 数据库进行简单备份和恢复

Spring Framework 中的默认隔离级别

数据库中空值(nulls)使用的空间

每个请求可以多次查询 MongoDB 吗?

某些网站不允许在密码中使用句点是否有原因?

Boyce-Codd 范式的良好 KISS 描述是什么?

SqlAlchemy 中的动态表创建和 ORM 映射

如何删除 SQLite 中具有多个 where 参数的行?

如何使用 PHP 代码将图像上传到 MySQL 数据库

在 iphone 上本地存储数据

Select * 和 Select [列出每个列] 之间有区别吗

不带 WHERE 子句的 UPDATE 查询

MySQL:LAST_INSERT_ID() 返回 0

有没有一种简单的方法来告诉 alembic 迁移到特定版本?

如何将 DECIMAL 插入 MySQL 数据库

SQLite3 数据库的最大连接数是多少?