A lot of stuff on the Internet spring boot International articles are the normal way to use them

If you use it like I do Spring Security So when you are multilingual, you may encounter an abyss

Spring Security The exception itself is multilingual , But only through Accept-Language To switch the language of the error message

And we need to customize exception information , And with url Parameters lang=en_US perhaps lang=cn_ZH To control the language, this method on the Internet will not work

@Configuration
public class I18nConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver slr = new CookieLocaleResolver();
slr.setCookieName("locale");
// Default language
slr.setDefaultLocale(Locale.US);
return slr;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// Parameter name It is used to distinguish the types of languages used
lci.setParamName("lang");
return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}

The key is coming.

as a result of Spring Security Before DispatcherServlet,Spring Security When the exception exits, the above code has not been executed , So there was no successful modification locale.

And I set it manually in the result process locale Also met a pit

I'm printing the journal Filter Added LocaleContextHolder.setLocale(StringUtils.parseLocale(newLocale)); And then the language is still wrong when printing .

This problem is because of my filter Of ordered It's too early , So the back is RequestContextFilter Set it back .

therefore

The solution is RequestContextFilter Set it later locale That's right .

String newLocale = request.getParameter("lang");
if (StringUtils.isNotBlank(newLocale)) {
LocaleContextHolder.setLocale(StringUtils.parseLocale(newLocale));
}

This code needs to be RequestContextFilter After performing .

Here's a screenshot of the document

If it helps you, please comment , Like to let me know , thank you !

Spring Boot Security internationalization Multilingual i18n More articles about wading through the giant pit

  1. Spring Boot Security OAuth2 Implementation supports JWT Token's authorization server

    Summary Two previous articles , Tells the story of Spring Security combination OAuth2 .JWT Use , This section requires that OAuth2.JWT I understand , If not clear , First move to the next two articles to learn about . Spring Bo ...

  2. Spring Boot Security Integrate JWT Realization Stateless distribution API Interface

    brief introduction JSON Web Token( abbreviation JWT) Is the most popular cross domain authentication solution .JSON Web Token Introductory tutorial - Ruan Yifeng , This article can help you understand JWT The concept of . This article focuses on Spring Boo ...

  3. Spring Boot Security Integrate OAuth2 Design safety API Interface services

    brief introduction OAuth It's about empowerment (authorization) Open network standards , It's widely used all over the world , The current version is 2.0 edition . This article focuses on Spring Boot Project pair OAuth2 Implementation in progress , If you are right about OAut ...

  4. boke practice : spring boot: security post Data time , Either close crst, Or add hidden fields

    spring boot: security post Data time , Either close crst, Or add hidden fields http.csrf().disable(); perhaps : <input name="${_cs ...

  5. Spring Boot Security Configuration tutorial

    1. brief introduction In this paper , We will understand Spring Boot Yes spring Security Support for . In short , We will focus on the default Security Configure and how to disable or customize it when needed . 2. Default Security set up ...

  6. php gettext How to achieve UTF-8 International multilingualism (i18n)

    php gettext How to achieve UTF-8 International multilingualism (i18n) One . summary One sentence summary : Two .php gettext How to achieve UTF-8 International multilingualism (i18n) near Come and follow i18n( internationalization ) The gradual standardization of ...

  7. Spring Boot Security Oauth2 Client mode and password mode implementation

    Spring Boot Security Oauth2 Client mode and password mode implementation Main content of the example 1. Multi authentication mode ( Password mode . Client mode ) 2.token Deposit in redis Support 3. resource protection 4. Password mode users ...

  8. Spring Boot Security And JSON Web Token

    Spring Boot Security And JSON Web Token explain Process description When to generate and use jwt, In fact, we are mainly token It's more meaningful and carries some information https://github.co ...

  9. Spring Boot Security Protect your program

    Spring Boot Security The content of this example Role based access control encryption . Decrypt be based on Spring Boot Security The rights management framework protects applications String Security Introduce ...

  10. Spring Boot Security Use the tutorial

    although , What I use in actual projects is shiro Permission management , but spring boot security Already famous , Although his introduction is a bit more complicated , But design depends on being more attractive . This chapter is a quick start sp ...

Random recommendation

  1. ios Develop how to get the input date to get the week

    + (NSString*)weekdayStringFromDate:(NSDate*)inputDate { NSArray *weekdays = [NSArray arrayWithObject ...

  2. git Development based on publishing branches

    Create a publication branch : (1) Software hello-world Of 1.0 There is a corresponding mileage in the release repository . /home/jackluo/workspace/user1/workspace/hello-worldgit ...

  3. The box model Box Model( float )

    One . Size of standard box model :border+padding+content(width)         Weird box model size :padding+border   Two .display inline   Default , And become the line from inside ...

  4. About URL rewrite

    1.  Why use url rewrite ? First from session From the principle of , Why can we get session What's in it ? If not disabled cookie Words , that session After creating , Will session id The value of is passed r ...

  5. difference client、offset、scroll Series and event Several distance properties of

    element Element node attributes One . offset series 1.offsetWidth and offsetHeight element.offsetWidth Is a read-only property , It includes : css width + p ...

  6. An advantage of pojo Class from the front page request A small framework for getting parameters in ~

    I didn't know before I wrote Spring We've implemented this function , So I wrote this stupid thing ! The implementation principle is very interesting. Let's record it here . Since last October java Since the development, I have made some progress . So happy . solve the problem ( Detailed explanation ): front end form Form reference ...

  7. [OC] NSTimer

    Be careful NSTimer Destruction of NSTimer After initialization , Join in RunLoop Will cause the current interface to be strongly referenced , So it won't execute dealloc, It needs to be done in a logical way NSTimer Destruction of [_timer invali ...

  8. Real time monitoring input Change in value

    a key :$('#xx').bind('input propertychange', function() {} For example : html: <div ><span id="numb ...

  9. C++ in cin.clear() Usage of

    Let's talk. cin.clear The role of , The first time I saw this thing , A lot of people think it's empty cin The data flow inside , In fact, it's far from that , First, let's look at the following code : #include <iostream>  usin ...

  10. ORA-00054:resource busy and acquire with nowait specified resolvent

    1. use dba Users with permissions can view the locks in the database SELECT T2.USERNAME,T2.SID,T2.SERIAL#,T2.LOGON_TIME        FROM V$LOCKED_OBJECT ...