Spring 基于url的多语言登录页面
在我的Spring Boot应用程序中,我将区域设置作为url的一部分:Spring 基于url的多语言登录页面,spring,spring-mvc,spring-boot,spring-security,kotlin,Spring,Spring Mvc,Spring Boot,Spring Security,Kotlin,在我的Spring Boot应用程序中,我将区域设置作为url的一部分: /site - default locale /en/site - English locale 我为此使用自定义拦截器: import org.springframework.beans.propertyeditors.LocaleEditor import org.springframework.util.Assert import org.springframework.web.servl
/site - default locale
/en/site - English locale
我为此使用自定义拦截器:
import org.springframework.beans.propertyeditors.LocaleEditor
import org.springframework.util.Assert
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
import org.springframework.web.servlet.support.RequestContextUtils
import javax.servlet.ServletException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.util.Locale
import java.util.regex.Pattern
class CustomLocaleChangeInterceptor : HandlerInterceptorAdapter() {
private var localePattern: Pattern? = null
private fun setLocalePattern(localePattern: String) {
Assert.isTrue(localePattern.matches(".*\\(.*\\).*".toRegex()), "Your pattern needs to define a match group")
this.localePattern = Pattern.compile(localePattern)
}
@Throws(ServletException::class)
override fun preHandle(request: HttpServletRequest?, response: HttpServletResponse?, handler: Any?): Boolean {
this.setLocalePattern("(en)")
val pathTranslated = request!!.requestURI.substring(request.contextPath.length)
if (pathTranslated.isNotEmpty()) {
val matcher = localePattern!!.matcher(pathTranslated)
if (matcher.find()) {
resolver(request, response, matcher.group(1))
} else {
resolver(request, response, "th")
}
}
// Proceed in any case.
return true
}
private fun resolver(request: HttpServletRequest, response: HttpServletResponse?, locale: String) {
val localeResolver = RequestContextUtils.getLocaleResolver(request) ?: throw IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?")
val localeEditor = LocaleEditor()
localeEditor.asText = locale
localeResolver.setLocale(request, response, localeEditor.value as Locale)
}
}
问题是在Spring中处理两个自定义登录页面的最佳方式是什么?当受限url包含/en时,用户应重定向到/en/login页面(使用英语),否则,如果页面具有默认区域设置,则应重定向到/login url(使用默认语言)在spiring security中,您可以使用此选项
import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
@Component
public class Securityhandler implements AuthenticationSuccessHandler {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
Local local= LocaleContextHolder.getLocale();
if(local.equals("yourcodeLang"){
response.sendRedirect("/yourUrl");
}
else // your logic
}
}
并按如下方式更新配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
....
.successHandler(yourSuccessHandlerBean) // autowired or defined
...
}
在spiring security中,您可以使用此
import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
@Component
public class Securityhandler implements AuthenticationSuccessHandler {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
Local local= LocaleContextHolder.getLocale();
if(local.equals("yourcodeLang"){
response.sendRedirect("/yourUrl");
}
else // your logic
}
}
并按如下方式更新配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
....
.successHandler(yourSuccessHandlerBean) // autowired or defined
...
}
与此同时,我找到了这个解决方案。也许它并不完美,但它是有效的
@EnableWebSecurity
@Order(1)
class SecurityConfigTH : WebSecurityConfigurerAdapter() {
private val localePattern: Pattern = Pattern.compile("^/en(\$|/)")
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.requestMatcher { !localePattern.matcher(it.requestURI.toString()).find() }
.formLogin()
.loginPage("/login")
.permitAll()
}
}
@EnableWebSecurity
@Order(2)
class SecurityConfigEN : WebSecurityConfigurerAdapter() {
private val localePattern: Pattern = Pattern.compile("^/en(\$|/)")
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.requestMatcher { localePattern.matcher(it.requestURI.toString()).find() }
.formLogin()
.loginPage("/en/login")
.permitAll()
}
}
与此同时,我找到了这个解决方案。也许它并不完美,但它是有效的
@EnableWebSecurity
@Order(1)
class SecurityConfigTH : WebSecurityConfigurerAdapter() {
private val localePattern: Pattern = Pattern.compile("^/en(\$|/)")
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.requestMatcher { !localePattern.matcher(it.requestURI.toString()).find() }
.formLogin()
.loginPage("/login")
.permitAll()
}
}
@EnableWebSecurity
@Order(2)
class SecurityConfigEN : WebSecurityConfigurerAdapter() {
private val localePattern: Pattern = Pattern.compile("^/en(\$|/)")
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.requestMatcher { localePattern.matcher(it.requestURI.toString()).find() }
.formLogin()
.loginPage("/en/login")
.permitAll()
}
}
谢谢你,Hicham,但据我所知,你的答案与成功认证后将用户重定向到哪里有关。我需要以相反的方式来做:到哪个登录页面发送用户进行身份验证。谢谢Hicham,但据我所知,您的答案涉及到成功身份验证后将用户重定向到哪里。我需要以相反的方式来做:向哪个登录页面发送用户来进行身份验证。