2017-05-19 10 views
0

私はJHipsterを数週間前から使い始めました。私はJHipsterのデフォルト認証と同時にLDAP認証をしたいと思っています。JHipster LDAP認証を使用

私はこれに続き、https://jhipster.github.io/tips/016_tip_ldap_authentication.html、それは計画どおりに動作しません。 実際に私の設定は私のLDAPサーバにうまく接続しています。私は、ログインがLDAPサーバに検索してパスワードを比較するというログを見ることで分かります。 (だけ

UT005023: Exception handling request to /api/authentication 

org.springframework.security.core.userdetails.UsernameNotFoundException: User nseys was not found in the database 
    at com.mycompany.myapp.security.PersistentTokenRememberMeServices.lambda$onLoginSuccess$1(PersistentTokenRememberMeServices.java:116) 
    at java.util.Optional.orElseThrow(Optional.java:290) 
    at com.mycompany.myapp.security.PersistentTokenRememberMeServices.onLoginSuccess(PersistentTokenRememberMeServices.java:116) 
    at org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices.loginSuccess(AbstractRememberMeServices.java:294) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    ... 

事がある私は、それはパラメータのマッピングにそこに存在しない場合JHipsterが自動的にデータベースにユーザーを作成します:

問題はログインがエラーで失敗していますLDAPユーザーの場合)、すでに完了していれば接続するだけです。

私はSpringセキュリティソリューションも検索しましたが、実装はJHipsterによって作成された最初のファイルから離れすぎており、このすべてを破棄したくありません。

答えて

0

私はこれがうまくいくかどうかは分かりませんでしたが、これについては何も分かっていませんでしたので、何も見つからなかったため、文書化されていません。より良い解決策を見つける。

// PersistentTokenRememberMeServices.java 

protected void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication 
    successfulAuthentication) { 

    String login = successfulAuthentication.getName(); 

    log.debug("Creating new persistent login for user {}", login); 

    PersistentToken t = new PersistentToken(); 
    t.setSeries(RandomUtil.generateSeriesData()); 
    t.setTokenValue(RandomUtil.generateTokenData()); 
    t.setTokenDate(LocalDate.now()); 
    t.setIpAddress(request.getRemoteAddr()); 
    t.setUserAgent(request.getHeader("User-Agent")); 

    PersistentToken token = userRepository.findOneByLogin(login).map(u -> { 
     t.setUser(u); 
     return t; 
    }).orElse(null); 

    if (token == null) { 
     if (successfulAuthentication.getPrincipal() instanceof LdapUserDetails) { 
      User ldapUser = new User(); 
      ldapUser.setLogin(login); 
      ldapUser.setPassword(RandomStringUtils.random(60)); // We use LDAP password, but the password need to be set 
      ldapUser.setActivated(true); 

      CustomLdapUserDetails customLdapUserDetails = (CustomLdapUserDetails) successfulAuthentication.getPrincipal(); 
      ldapUser.setEmail(customLdapUserDetails.getEmail()); 
      ldapUser.setFirstName(customLdapUserDetails.getFirstName()); 
      ldapUser.setLastName(customLdapUserDetails.getLastName()); 

      Set<Authority> authorities = new HashSet<>(); 
      authorities.add(this.authorityRepository.findOneByName("ROLE_USER")); 
      ldapUser.setAuthorities(authorities); 
      ldapUser.setLangKey("fr"); 

      userRepository.save(ldapUser); 
      t.setUser(ldapUser); 
      token = t; 
     } else { 
      throw new UsernameNotFoundException("User " + login + " was not found in the database"); 
     } 
    } 
    ... 
} 

そして私は、LDAPサーバの属性を取得するcontextMapperを追加

// SecurityConfiguration.java 
@Bean 
public UserDetailsContextMapper userDetailsContextMapper() { 
    return new LdapUserDetailsMapper() { 
     @Override 
     public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) { 
      UserDetails details = super.mapUserFromContext(ctx, username, authorities); 
      return new CustomLdapUserDetails((LdapUserDetails) details, ctx); 
     } 
    }; 
} 

@Inject 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    LdapContextSource contextSource = new LdapContextSource(); 
    contextSource.setUrl(applicationProperties.getLdap().getUrl()); 
    contextSource.setBase(applicationProperties.getLdap().getBase()); 
    contextSource.setUserDn(applicationProperties.getLdap().getUserDn()); 
    contextSource.setPassword(applicationProperties.getLdap().getPassword()); 
    contextSource.afterPropertiesSet(); //needed otherwise you will have a NullPointerException in spring 

    auth.ldapAuthentication() 
     .userDetailsContextMapper(userDetailsContextMapper()) 
     .userSearchBase(applicationProperties.getLdap().getSearchBase()) //don't add the base 
     .userSearchFilter(applicationProperties.getLdap().getSearchFilter()) 
     .contextSource(contextSource) 
    ; 
}