栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Hibernate进行Spring Security 3数据库身份验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Hibernate进行Spring Security 3数据库身份验证

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

示例代码:

从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" >  <beans:property name="userDetailsService" ref="userDetailsService"/></beans:bean><beans:bean id="authenticationManager"    >  <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-enprer hash="md5"/>  </authentication-provider></authentication-manager>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390883.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号