0
点赞
收藏
分享

微信扫一扫

spring boot 使用security会根据当前系统使用对应方言

spring boot采用当前系统的语言显示对应的错误信息

throw new BadCredentialsException(this.messages
      .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));

其中默认的messages配置是:

/*
 * Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.core;

import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
 * The default <code>MessageSource</code> used by Spring Security.
 * <p>
 * All Spring Security classes requiring message localization will by default use this
 * class. However, all such classes will also implement <code>MessageSourceAware</code> so
 * that the application context can inject an alternative message source. Therefore this
 * class is only used when the deployment environment has not specified an alternative
 * message source.
 * </p>
 *
 * @author Ben Alex
 */
public class SpringSecurityMessageSource extends ResourceBundleMessageSource {

	public SpringSecurityMessageSource() {
		setBasename("org.springframework.security.messages");
	}

	public static MessageSourceAccessor getAccessor() {
		return new MessageSourceAccessor(new SpringSecurityMessageSource());
	}

}

往上翻继承的父类找到如下,并找到关键的默认配置信息如下:

private boolean fallbackToSystemLocale = true;//这里就是采用本地系统方言

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.context.support;

import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

public abstract class AbstractResourceBasedMessageSource extends AbstractMessageSource {
    private final Set<String> basenameSet = new LinkedHashSet(4);
    @Nullable
    private String defaultEncoding;
    private boolean fallbackToSystemLocale = true;//这里就是采用本地系统方言
    @Nullable
    private Locale defaultLocale;
    private long cacheMillis = -1L;

    public AbstractResourceBasedMessageSource() {
    }

    public void setBasename(String basename) {
        this.setBasenames(basename);
    }

    public void setBasenames(String... basenames) {
        this.basenameSet.clear();
        this.addBasenames(basenames);
    }

    public void addBasenames(String... basenames) {
        if (!ObjectUtils.isEmpty(basenames)) {
            String[] var2 = basenames;
            int var3 = basenames.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String basename = var2[var4];
                Assert.hasText(basename, "Basename must not be empty");
                this.basenameSet.add(basename.trim());
            }
        }

    }

    public Set<String> getBasenameSet() {
        return this.basenameSet;
    }

    public void setDefaultEncoding(@Nullable String defaultEncoding) {
        this.defaultEncoding = defaultEncoding;
    }

    @Nullable
    protected String getDefaultEncoding() {
        return this.defaultEncoding;
    }

    public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
        this.fallbackToSystemLocale = fallbackToSystemLocale;
    }

    /** @deprecated */
    @Deprecated
    protected boolean isFallbackToSystemLocale() {
        return this.fallbackToSystemLocale;
    }

    public void setDefaultLocale(@Nullable Locale defaultLocale) {
        this.defaultLocale = defaultLocale;
    }

    @Nullable
    protected Locale getDefaultLocale() {
        if (this.defaultLocale != null) {
            return this.defaultLocale;
        } else {
            return this.fallbackToSystemLocale ? Locale.getDefault() : null;
        }
    }

    public void setCacheSeconds(int cacheSeconds) {
        this.cacheMillis = (long)cacheSeconds * 1000L;
    }

    public void setCacheMillis(long cacheMillis) {
        this.cacheMillis = cacheMillis;
    }

    protected long getCacheMillis() {
        return this.cacheMillis;
    }
}

好了,弄出来为啥我这spring boot怎么会自动提示中文的security错误信息,就舒服了

自己明明没设置,怎么就给我的是中文,我特意修改了下系统为英文,然后提示信息就如下了,就不再是中文了:

this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials") = "Bad credentials"
举报

相关推荐

0 条评论