Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring安全自定义身份验证异常消息_Java_Spring_Jakarta Ee_Spring Security - Fatal编程技术网

Java Spring安全自定义身份验证异常消息

Java Spring安全自定义身份验证异常消息,java,spring,jakarta-ee,spring-security,Java,Spring,Jakarta Ee,Spring Security,您好,我需要在Spring security登录表单中添加一个新的异常,除了我希望有自己的错误消息(直到现在它显示“错误的登录/密码”消息)之外,其他一切都可以正常工作 我已覆盖用户名密码身份验证筛选器中的默认尝试身份验证方法: @Override public Authentication attemptAuthentication(final HttpServletRequest request, final HttpServletResponse response) { if (!myTe

您好,我需要在Spring security登录表单中添加一个新的异常,除了我希望有自己的错误消息(直到现在它显示“错误的登录/密码”消息)之外,其他一切都可以正常工作

我已覆盖用户名密码身份验证筛选器中的默认尝试身份验证方法:

@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\u SECURITY\u LAST\u exception下看到了我的异常,但错误消息始终是来自错误凭据的消息,我如何才能更改它


谢谢

您应该尝试本地化SPRING安全消息
尝试将这些行添加到
ApplicationContext.xml
文件中。其他的spring安全bean都在哪里

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

在messages.properties(或您命名的任何内容)中,添加一行,如:

AbstractUserDetailsAuthenticationProvider.badCredentials=The credentials you supplied are invalid.

您不需要CustomAuthenticationException。

在类路径中创建属性文件,如loginMessage.properties

在该属性文件中,指定

AbstractUserDetailsAuthenticationProvider.badCredentials=输入的用户名/密码不正确

在applicationContext.xml中添加以下bean

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

登录消息

在那之后,你会收到这样的信息,比如输入的用户名/密码不正确。一种非常简单的方法是在异常处理程序(@ControllerAdvice)中定义自定义消息,而不是错误的凭据,如下所示:

@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody
@ExceptionHandler(value = AuthenticationException.class)
public ResponseModal handleAuthenticationExceptions(AuthenticationException ex, HttpServletResponse response) {
    LOGGER.info("Authentication Exception: {}", ex.getMessage());
    response.addCookie(new Cookie(JWTConfigurer.JWT_TOKEN_COOKIE, null));
    return new ResponseModal(HttpStatus.UNAUTHORIZED.value(), "whatever you want");
}

您需要在登录页面或日志文件中显示不同的消息吗?如果它是登录页面,您需要对其进行配置:
在登录页面中,它们映射到从异常到自定义消息的每个AuthenticationException(BadCredentialsException、LockedException…请参见org.springframework.security.authentication中的),我想显示我自己的错误消息。是的,我已经有一个消息资源,但是自定义异常的密钥是什么?为您更新了答案。您所要做的就是为每个键设置一条
消息
。为什么不提供反馈?如果这解决了你的问题,你可以接受这个答案,如果你觉得它有用,你可以推广它。嗨,马汀,请告诉我如何根据地区获取消息。我在中文地区获取中文消息时遇到困难。亲爱的@MatinKh,你能回答我的问题吗?在弹簧靴上为我工作
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody
@ExceptionHandler(value = AuthenticationException.class)
public ResponseModal handleAuthenticationExceptions(AuthenticationException ex, HttpServletResponse response) {
    LOGGER.info("Authentication Exception: {}", ex.getMessage());
    response.addCookie(new Cookie(JWTConfigurer.JWT_TOKEN_COOKIE, null));
    return new ResponseModal(HttpStatus.UNAUTHORIZED.value(), "whatever you want");
}