[英]BadCredentialsException when migrating from Spring 3.0.x to 3.1.x
我們已經從3.0.7 spring security遷移到3.1.2,並且我們使用in-memory-config的一個測試在錯誤的憑據上失敗。
我們沒有做任何特別的事情,只需用純文本用戶名和密碼驗證其中一個用戶。 一旦通過認證,我們就會填補我們的權力。
碼:
public Authentication authenticate(UserDetails userDetails)
throws AuthenticationException {
try {
org.springframework.security.core.Authentication authenticate = authenticationManager.authenticate(createAuthenticationRequest(userDetails));
if (!authenticate.isAuthenticated()) {
throw new AuthenticationException("Authentication failed for user ["+userDetails.getUsername()+"]");
}
Collection<? extends GrantedAuthority> grantedAuthorities = authenticate.getAuthorities();
...
} catch(Exception exception) {
throw new AuthenticationException(exception);
}
碼:
<bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="daoUserDetailsService" />
</bean>
<bean id="daoUserDetailsService" class="org.springframework.security.core.userdetails.memory.InMemoryDaoImpl">
<property name="userMap">
<value>
Edward = koala, READ_ONLY
</value>
</property>
</bean>
我們在調用身份驗證時遇到以下異常:
Caused by: org.springframework.security.authentication.BadCre dentialsException: Bad credentials
at org.springframework.security.authentication.dao.Da oAuthenticationProvider.additionalAuthenticationCh ecks(DaoAuthenticationProvider.java:67)
at org.springframework.security.authentication.dao.Ab stractUserDetailsAuthenticationProvider.authentica te(AbstractUserDetailsAuthenticationProvider.java: 149)
at org.springframework.security.authentication.Provid erManager.authenticate(ProviderManager.java:156)
at org.openspaces.security.spring.SpringSecurityManag er.authenticate(SpringSecurityManager.java:117)
... 11 more
任何想法如何解決它或是否有一個補丁等待這個問題?
查看您的配置,它可能是一個空白解析問題,但它應該很容易通過在DaoAuthenticationProvider.additionalAuthenticationChecks
放置一個斷點來查看驗證失敗的原因。
在任何情況下,不推薦使用用於配置內存用戶的屬性編輯器方法來支持命名空間配置。 你可以使用類似的東西
<security:user-service id="daoUserDetailsService">
<security:user name="Edward" password="koala" authorities="READ_ONLY" />
</security:user-service>
得到相同的結果。 當然,您必須將安全命名空間添加到應用程序上下文文件中。
以下答案基於Guy Korland的評論(2012年8月16日20:40),他做了進一步的調試:
在使用Spring 3.1之后,erase-credentials的默認值從'false'更改為'true',這就是為什么從緩存中提取密碼時無效的密碼。 它還解釋了為什么您的測試用例在Spring 3.1之前通過。 您從緩存中檢索的類是UserDetails,一旦Spring驗證了未加密的密碼,它就不再使用它,因此它將其作為安全措施擦除。 對於簡單的測試場景,您可以將erase-credentials值覆蓋為“false”,但如果確實在建立身份驗證后依賴未加密的值,請考慮長期尋找更安全的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.