2016-06-20 10 views
0

私は、データベースとLdapを介してユーザを認証するためにSpringセキュリティとアノテーションを使用しています。 詳細につきましては、Ldapは属性取得を許可していないため、Ldap検索でユーザー(一意のコード)とパスワードが正しいかどうかを確認し、データベースを使用して権限を読み込みます。だから私のデータベースにすべてのユーザーがLdapに存在しますが、ユーザーがLdapに存在し、私のデータベースに存在しない場合は、特定のページを表示します。 これは、実際のコードです:私はシンプルなユーザーを持っていると私は名前/姓と電子メールのようないくつかの情報を追加したいと思いカスタムユーザのデータベースによるSpringセキュリティ認証

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true) 
@PropertySource(value = { "classpath:application.properties" }) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 


    @Autowired 
    AuthenticationConfiguration authenticationConfiguration; 

    @Configuration 
    protected static class AuthenticationConfiguration implements 
    AuthenticationProvider { 

     @Autowired 
     private UserServices userServices; 
     @Autowired 
     LdapServices ldapServices; 

     @Override 
     public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
      Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); 
      String name = authentication.getName(); 
      String password = authentication.getCredentials().toString(); 
      boolean isFind = ldapServices.ldapSearch(name, password);       
      if (isFind){ 
       com.domain.User user = userServices.getByUsersEnabled(name); 
       if (user!=null) 
        authorities.add(new SimpleGrantedAuthority("ROLE_"+user.getRole().getRole()));   
       return new UsernamePasswordAuthenticationToken(name, password, authorities); 
      }   
      else return null; 
     } 


     @Override 
     public boolean supports(Class<?> authentication) { 
      return authentication.equals(UsernamePasswordAuthenticationToken.class); 
     } 
    } 

    @Autowired 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(authenticationConfiguration); 
     } 

...web services authentication 

。私はUserDetailsloadUserByUsernameUserDetailsServiceインターフェイスを実装する必要があることを読んだが、私はloadUserByUsernameを自分のコードにマージできますか?このようにして、ユーザーコードの代わりに名前と姓を表示することができます。私も同様の問題のいくつかの問題を抱えていた、そして私は私がそれを得るために助けた偉大な記事を見つけたおかげ

答えて

0

私はreturn new UsernamePasswordAuthenticationToken(user, password, authorities);return new UsernamePasswordAuthenticationToken(name, password, authorities);を変え、私のHTMLページに、私はnameパラメータ

0

を取得するためにsec:authentication="principal.name"を使用します完了しました。 記事はこちらです:spring-ldap-custom-authorities

私は助けてくれることを願っています。基本的には、LDAPサーバーで認証プロセスを実行する必要があります。後でユーザーの詳細を取得できるように、「CustomLdapAuthoritiesPopulator」を作成する必要があります。

<beans:bean id="ldapAuthProvider" 
    class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"> 
    <beans:constructor-arg> 
     <beans:bean 
      class="org.springframework.security.ldap.authentication.BindAuthenticator"> 
      <beans:constructor-arg ref="contextSource" /> 
      <beans:property name="userSearch" ref="userSearch" /> 
     </beans:bean> 
    </beans:constructor-arg> 
    <beans:constructor-arg> 
     <!-- User roles --> 
     <beans:bean class="com.company.package.CustomLdapAuthoritiesPopulator" /> 
    </beans:constructor-arg> 
</beans:bean> 

を、後であなたのCustomLdapAuthoritiesPopulator上のユーザー・rolersに対処します:

あなたがXMLでこのような何かを持っている必要があります。

@Service("myAuthPopulator") 
public class CustomLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator { 
    @Transactional(readOnly=true) 
    @Override 
    public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { 

     Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); 
     try { 

      User user = userService.findUserByUsername(username); 

      if (user == null) { 
       // User doesn't exist in the database 
      } else { 

       // user exists 

       //get roles 
       Set<UserRole> userRoles = user.getUserRoles(); 

       //add roles 
       for (UserRole userRole : userRoles) { 
        authorities.add(new SimpleGrantedAuthority(userRole.getRole())); 
       } 

       return authorities; 
      } 
     } catch(Exception e) { 
      //exception 
     } 
     return authorities; 
    } 

} 
+0

このリンクは質問に答えるかもしれませんが、ここでは回答の重要な部分を含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューより](/レビュー/低品質の投稿/ 17660666) –

+0

こんにちは@ProkashSarkar、あなたの返信をありがとう。私は答えを編集し、いくつかのコードを追加しました。私は今それが良いと思う。 –

関連する問題