2017-08-10 18 views
0

我知道我的問題有點奇怪,但在連接到LDAP服務器的應用程序中,我們只需要讓用戶登錄到應用程序。是否可以使用Spring SecurityConfiguration HttpSecurity無密碼登錄?

該應用程序是一個Spring AngularJS。

所以,現在我正在改變代碼來做到這一點,但我發現登錄方法是做成Spring安全配置並使用密碼,所以我需要從數據庫中獲取密碼(user.getPassword()),問題是,密碼與加密的PasswordEncoder所以沒有辦法decrypte它(在這裏在制酒紅一個答覆中提到)..

反正我不知道是否有使用彈簧安全登錄方式只是用戶名(沒有密碼)..

我試圖從配置類中刪除密碼聲明,但它doesn沒有工作。

在該類調用(public class SecurityConfiguration extends WebSecurityConfigurerAdapter)我有未來的配置:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .csrf() 
     .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) 
    .and() 
     .exceptionHandling() 
     .authenticationEntryPoint(authenticationEntryPoint) 
    .and() 
     .rememberMe() 
     .rememberMeServices(rememberMeServices) 
     .rememberMeParameter("remember-me") 
     .key(jHipsterProperties.getSecurity().getRememberMe().getKey()) 
    .and() 
     .formLogin() 
     .loginProcessingUrl("/api/authentication") 
     .successHandler(ajaxAuthenticationSuccessHandler) 
     .failureHandler(ajaxAuthenticationFailureHandler) 
     .usernameParameter("j_username") 
     .passwordParameter("j_password") 
     .permitAll() 
    .and() 
     .logout() 
     .logoutUrl("/api/logout") 
     .logoutSuccessHandler(ajaxLogoutSuccessHandler) 
     .permitAll() 
    .and() 
     .headers() 
     .frameOptions() 
     .disable() 
} 

這裏是登錄方法:

 function login (credentials) { 
     alert("log in this"); 
     var data = 'j_username=' + encodeURIComponent(credentials.username) 
      +'&j_password=' + encodeURIComponent(credentials.password) + 
      '&remember-me=' + credentials.rememberMe + '&submit=Login'; 

     return $http.post('api/authentication', data, { 
      headers: { 
       'Content-Type': 'application/x-www-form-urlencoded' 
      } 
     }).success(function (response) { 
      return response; 
     }); 
    } 

任何想法的傢伙?

謝謝youuu

回答

1

您可以隨時在你的應用程序的任何時間創建SecurityContext,這並不重要,如果它有一個密碼或者沒有:

Authentication authentication = 
    new UsernamePasswordAuthenticationToken(user, null, authorities);//set with authorities 

SecurityContextHolder.getContext().setAuthentication(authentication); 

有了這個,你實際上只是通知春天你有一個registered用戶。

+0

最佳答案非常感謝你! – Smahane

0

我不確定你是如何檢索你的用戶詳細信息,但也許你可以只用j_username用戶名和密碼,然後在檢索用戶詳細信息的方法中忽略它。

+0

它不工作:( – Smahane