ZK + Spring Framework + HibernateでのSpring Securityのデプロイ:パート3

すべての良い一日。 それで、ZKで書かれたWebアプリケーションでのSpring Securityの実装に関する一連の記事(1つと 2つ )は終わりました。 前回の投稿では、継承されたAbstractUserDetailsAuthenticationProviderクラスから保護されたUserDetails retrieveUserメソッドをオーバーライドすることにより、ユーザー名とパスワードが正しいことを確認する責任を引き受けました。

他の方法で行きましょう。 ログインパスワードチェックを、Spring Securityシステムの重要なインターフェースの1つであるUserDetailsS​​erviceに割り当てます。UserDetailsS​​erviceは、loadUserByUsernameメソッドを通じてユーザーデータをロードします。



UserDetailsS​​erviceImplクラスにこのインターフェイスを実装します。

package com.sample.service; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.GrantedAuthorityImpl; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.sample.data.Role; @SuppressWarnings("deprecation") @Service("userDetailsService") @Transactional public class UserDetailsServiceImpl implements UserDetailsService { @Autowired public ISecur userDao; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user; List<com.sample.data.User> listUser = userDao.findAllUsers(); if (listUser.size() == 0) { throw new UsernameNotFoundException("    "); } com.sample.data.User person = getUser(listUser, username); if (person == null) { throw new UsernameNotFoundException(" "); } else { user = new User(person.getUsername(), person.getPassword(), true, true, true, true, getAuthorities((Set<Role>) person.getRoleList())); } return user; } private com.sample.data.User getUser(List<com.sample.data.User> lp, String userName) { com.sample.data.User pers = null; for (com.sample.data.User p : lp) { if (userName.equals(p.getUsername())) { pers = p; } } return pers; } private Collection<GrantedAuthority> getAuthorities(Set<Role> set) { Collection<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(); for (Role role : set) { authList.add(new GrantedAuthorityImpl(role.getName())); } return authList; } @SuppressWarnings("deprecation") private Collection<GrantedAuthority> getAuthorities(String grant_name) { Collection<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(); authList.add(new GrantedAuthorityImpl("ROLE_USER")); return authList; } }
      
      





次のステップは、spring-config.xmlファイルに記述されている認証マネージャーの構成を変更することです。 行の代わりに:

  <security:authentication-manager> <security:authentication-provider ref="userDetailsService"> </security:authentication-provider> </security:authentication-manager>
      
      





以下を書いてください:

  <security:authentication-manager> <security:authentication-provider user-service-ref="userDetailsService"/> </security:authentication-manager> <security:authentication-manager>
      
      





これですべてです。

PSそれがおもしろいことを願っています。SpringSecurityとZKの両方について質問がある場合は、質問してください。すべてに答えようとします。

ご清聴ありがとうございました。



All Articles