2013-05-26 134 views
4

嗨,我需要在Spring安全登錄表單中添加一個新的異常,除了我想有我自己的錯誤消息(直到現在它顯示「錯誤的登錄名/密碼」之一),一切都工作完美。春季安全自定義AuthenticationException消息

我有一個從用戶名密碼身份驗證篩選器覆蓋默認嘗試的身份驗證方法:

@Override 
public Authentication attemptAuthentication(final HttpServletRequest request, final HttpServletResponse response) 
{ 
if (!myTest()) { 
throw new CustomAuthenticationException("custom message"); 
} 
} 

我的例外:

public class CustomAuthenticationException extends AuthenticationException { 

    public CustomAuthenticationException(final String message) 
    { 
     super(message); 
    } 

    public CustomAuthenticationException(final String message, final Throwable cause) 
    { 
     super(message, cause); 
    } 

} 

在我的控制,我看到下SPRING_SECURITY_LAST_EXCEPTION我的異常,但錯誤信息總是一個來自不良憑證,我怎麼能改變這個?

謝謝

+0

您是否需要在登錄頁面或日誌文件中顯示不同的消息?如果登錄頁面需要配置它:'' – Michael

+0

在登錄頁面。 –

+0

在登錄頁面中,它們映射到自定義消息的異常中的每個AuthenticationException(BadCredentialsException,LockedException ...請參閱org.springframework.security.authentication)的某處,我想顯示自己的錯誤消息。 –

回答

11

你應該嘗試本地化SPRING安全消息
嘗試將這些行添加到您的ApplicationContext.xml文件中。你的春季安全豆的其餘部分在哪裏。

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="yourFolder/myMessages"/> 
</bean> 

你應該找到你的彈簧默認類<KEY, MESSAGE>存儲。請將您的myMessage文件與KEY s和本地化MESSAGE s相同。


根據您的評論,您的項目中有一個 messages.properties。因此,所有你需要做的是爲每個這個屬性文件中這些鍵的 MESSAGE,有一個完全本地化的消息:

AbstractAccessDecisionManager.accessDenied= your message in any language 
AbstractSecurityInterceptor.authenticationNotFound= 
AbstractUserDetailsAuthenticationProvider.badCredentials= 
AbstractUserDetailsAuthenticationProvider.credentialsExpired= 
AbstractUserDetailsAuthenticationProvider.disabled= 
AbstractUserDetailsAuthenticationProvider.expired= 
AbstractUserDetailsAuthenticationProvider.locked= 
AbstractUserDetailsAuthenticationProvider.onlySupports= 
AccountStatusUserDetailsChecker.credentialsExpired= 
AccountStatusUserDetailsChecker.disabled= 
AccountStatusUserDetailsChecker.expired= 
AccountStatusUserDetailsChecker.locked= 
AclEntryAfterInvocationProvider.noPermission= 
AnonymousAuthenticationProvider.incorrectKey= 
BindAuthenticator.badCredentials= 
BindAuthenticator.emptyPassword= 
CasAuthenticationProvider.incorrectKey= 
CasAuthenticationProvider.noServiceTicket= 
ConcurrentSessionControlStrategy.exceededAllowed= 
DigestAuthenticationFilter.incorrectRealm= 
DigestAuthenticationFilter.incorrectResponse= 
DigestAuthenticationFilter.missingAuth= 
DigestAuthenticationFilter.missingMandatory= 
DigestAuthenticationFilter.nonceCompromised= 
DigestAuthenticationFilter.nonceEncoding= 
DigestAuthenticationFilter.nonceExpired= 
DigestAuthenticationFilter.nonceNotNumeric= 
DigestAuthenticationFilter.nonceNotTwoTokens= 
DigestAuthenticationFilter.usernameNotFound= 
JdbcDaoImpl.noAuthority= 
JdbcDaoImpl.notFound= 
LdapAuthenticationProvider.badCredentials= 
LdapAuthenticationProvider.credentialsExpired= 
LdapAuthenticationProvider.disabled= 
LdapAuthenticationProvider.expired= 
LdapAuthenticationProvider.locked= 
LdapAuthenticationProvider.emptyUsername= 
LdapAuthenticationProvider.onlySupports= 
PasswordComparisonAuthenticator.badCredentials= 
PersistentTokenBasedRememberMeServices.cookieStolen= 
ProviderManager.providerNotFound= 
RememberMeAuthenticationProvider.incorrectKey= 
RunAsImplAuthenticationProvider.incorrectKey= 
SubjectDnX509PrincipalExtractor.noMatching= 
SwitchUserFilter.noCurrentUser= 
SwitchUserFilter.noOriginalAuthentication= 
+0

是的,我已經有一個消息資源,但是我的自定義異常的關鍵是什麼? –

+0

爲您更新了答案。你所要做的就是爲每個鍵設置一個「MESSAGE」。 –

+0

你爲什麼不提供反饋?如果解決了您的問題,您可以接受此答案作爲答案,或者如果您發現問題有用,則可以將其提升。 –

2

在您的messages.properties(或任何你把它命名爲),加線如:

AbstractUserDetailsAuthenticationProvider.badCredentials=The credentials you supplied are invalid. 

您不需要CustomAuthenticationException。

1

創建像loginMessage.properties

在該屬性文件中的類路徑屬性文件,指定

AbstractUserDetailsAuthenticationProvider.badCredentials =輸入的用戶名/密碼不正確。進入

添加以下豆在你的applicationContext.xml,

<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>loginMessage</value> 
     </list> 
    </property> 
</bean> 

之後,日子會把得到的消息像用戶名/密碼不正確。而不是憑證