2015-11-26 24 views
1

我已經實現了彈簧引導安全性的安全層和我已經使用MD5加密機制來編碼如預期的那樣呈現password.It的完美工作,但我需要得到用戶名和密碼的原始用戶在DAO已經進入或服務layer.Following是代碼我已經使用如何獲得用戶名和輸入的密碼不編碼與春天啓動安全

@Autowired 
UserDao userDao; 

@Autowired 
@Qualifier("userDetailsService") 
UserDetailsService userDetailsService; 

@Autowired 
private RESTAuthenticationEntryPoint authenticationEntryPoint; 

@Autowired 
private RESTAuthenticationFailureHandler authenticationFailureHandler; 
@Autowired 
private RESTAuthenticationSuccessHandler authenticationSuccessHandler; 

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers("/css/**", "/fonts/**", "/images/**"); 
} 


/** 
* Security implementation to access the services 
*/ 
@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests().antMatchers("/", "/index.html","/home.html","/page/*","/home/*", "/login.html","/login","/cms/createPhoneNo").permitAll(); 
    http.authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic().and().csrf().disable(); 
    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint); 
    http.formLogin().loginProcessingUrl("/login/authenticate").successHandler(authenticationSuccessHandler); 
    http.formLogin().failureHandler(authenticationFailureHandler); 
    http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).invalidateHttpSession(true); 
    http.exceptionHandling().accessDeniedHandler(accessDeniedHandler()); 

    // CSRF tokens handling 
    http.addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class); 
} 

/** 
* Configures the authentication manager bean which processes authentication 
* requests. 
*/ 
@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    // Dao based authentication 
    auth.userDetailsService(userDetailsService).passwordEncoder(new Md5PasswordEncoder()); 
} 

private AccessDeniedHandler accessDeniedHandler() { 
    return new AccessDeniedHandler() { 

     @Override 
     public void handle(HttpServletRequest request, HttpServletResponse response, 
       AccessDeniedException accessDeniedException) throws IOException, ServletException { 
      response.getWriter().append("Access denied"); 
      response.setStatus(403); 
     } 
    }; 
} 

/** 
* This bean is load the user specific data when form login is used. 
*/ 
@Bean 
public UserDetailsService userDetailsService() { 
    return new MyCustomUserDetailsService(userDao); 
} 

}

有誰請幫助我實現這個方案?

感謝,

+1

您無法「解碼」md5。 Md5是一個(非常弱)單向散列。我可以問爲什麼你想要原始密碼?散列點就是沒有這個。另外,考慮使用至少sha-256或者bcrypt或者scrypt,md5非常弱,對於校驗和以外的任何事情都不適合。 – Taylor

+0

@Taylor其實,我需要檢查與基於用戶type.In1分貝我已經編碼與用戶類型的用戶的密碼如果輸入的用戶沒有找到該用戶的類型我需要切換兩個數據庫中的用戶身份驗證到另一個數據庫,我有密碼作爲原始密碼,所以我需要輸入原始密碼。你有什麼想法嗎? – DIVA

+1

您不需要原始密碼。您只需使用與前端相同的方法從數據庫中散列密碼並比較散列。 – dunni

回答

1

添加到您的configure(AuthenticationManagerBuilder)方法:

auth.eraseCredentials(false); 

然後你就可以得到與當前用戶的用戶名和密碼:

String username = SecurityContextHolder.getContext().getAuthentication().getName(); 
Object rawPassword = SecurityContextHolder.getContext().getAuthentication().getCredentials(); 
+0

@holmis我們可以使用這種方法來獲得用戶的身份驗證。我對嗎? – DIVA

+0

@DIVA是的,通過身份驗證。 – holmis83

+0

我需要用戶憑證才能對用戶進行身份驗證。然後,只有我可以實現scenario.Is纔有可能在Dao中使用HttpServletRequest。我用過像下面這樣的錯誤,但是出現如下錯誤:「沒有找到線程綁定的請求:您是否在實際的Web請求之外引用請求屬性,或者在最初接收的線程之外處理請求?」。幫幫我 ? – DIVA

1

之前,我提供回答,強制性警告:

以明文存儲用戶密碼是非常危險的。任何人誰 訪問DB訪問用戶的密碼,這意味着它們可以 冒充您的應用程序的用戶。用戶還傾向於重用 密碼,這樣你就暴露該用戶在其他系統(其 電子郵件和Facebook等)的風險。

有了這樣的出路,哦,等等,多了一個:

MD5是一個非常弱的單向散列。它受rainbow tables 和純文本生成給定的散列很容易發現。 考慮切換到更強烈的東西,像SHA-256,Scrypt, Bcrypt或PBKDF2。

好吧,那些完成。使用org.springframework.security.authentication.encoding.PlaintextPasswordEncoder並在您的DAO中根據需要應用MD5(或更好,微調微調)散列。

+0

如何使用此類獲取daoorg.springframework.security.authentication.encoding.PlaintextPasswordEncoder – DIVA

+0

中的原始密碼@Taylor其實我需要用戶在Dao中提供的密碼,就像我們使用HttpServletRequest獲取請求參數一樣。是否有可能? – DIVA

+0

如果您使用上述內容,它應該位於身份驗證令牌中。 – Taylor

相關問題