Commit faa1cfc0 authored by Quxl's avatar Quxl

初始化语言环境,优化登陆TOKEN代码

parent 12ce8052
package com.egolm.film; package com.egolm.film;
import java.util.Locale;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableCaching @EnableCaching
...@@ -21,7 +17,6 @@ public class FilmApplication { ...@@ -21,7 +17,6 @@ public class FilmApplication {
private static ApplicationContext applicationContext; private static ApplicationContext applicationContext;
public static void main(String[] args) { public static void main(String[] args) {
LocaleContextHolder.setDefaultLocale(Locale.CHINA);
applicationContext = SpringApplication.run(FilmApplication.class, args); applicationContext = SpringApplication.run(FilmApplication.class, args);
} }
......
...@@ -35,4 +35,13 @@ public class MemberOpenApiController { ...@@ -35,4 +35,13 @@ public class MemberOpenApiController {
public Object getPassword(String email) { public Object getPassword(String email) {
return Rjx.jsonOk(); return Rjx.jsonOk();
} }
@PostMapping("setLocale")
@ApiOperation("设置语言环境")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", dataType = "string", required = true, name = "i18n_language", defaultValue="zh_CN"),
})
public Object setLocale(String i18n_language) {
return Rjx.jsonOk();
}
} }
...@@ -18,6 +18,7 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; ...@@ -18,6 +18,7 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.egolm.film.config.interceptor.AdminLoginInterceptor; import com.egolm.film.config.interceptor.AdminLoginInterceptor;
import com.egolm.film.config.interceptor.LocaleSessionInterceptor;
import com.egolm.film.config.interceptor.UserLoginInterceptor; import com.egolm.film.config.interceptor.UserLoginInterceptor;
@Configuration @Configuration
...@@ -32,9 +33,12 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter { ...@@ -32,9 +33,12 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired @Autowired
private UserLoginInterceptor memberLoginInterceptor; private UserLoginInterceptor memberLoginInterceptor;
@Autowired
private LocaleSessionInterceptor localeSessionInterceptor;
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeSessionInterceptor).addPathPatterns("/**");
registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/login"); registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/login");
registry.addInterceptor(userLoginInterceptor).addPathPatterns("/user/**").excludePathPatterns("/user/login"); registry.addInterceptor(userLoginInterceptor).addPathPatterns("/user/**").excludePathPatterns("/user/login");
registry.addInterceptor(memberLoginInterceptor).addPathPatterns("/member/**").excludePathPatterns("/member/login"); registry.addInterceptor(memberLoginInterceptor).addPathPatterns("/member/**").excludePathPatterns("/member/login");
......
package com.egolm.film.config.interceptor;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Component
public class LocaleSessionInterceptor extends HandlerInterceptorAdapter {
public static final String I18N_LANGUAGE_SESSION = "I18N_LANGUAGE_SESSION";
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
HttpSession session = req.getSession();
Locale locale = (Locale)session.getAttribute(I18N_LANGUAGE_SESSION);
if(locale == null) {
locale = req.getLocale();
}
LocaleContextHolder.setLocale(locale);
return super.preHandle(req, resp, handler);
}
}
package com.egolm.film.login.service;
import javax.servlet.http.HttpSession;
import com.egolm.film.config.WebMvcConfig;
import com.egolm.film.model.LoginToken;
public abstract class AbstractTokenService implements UserTokenService {
@Override
public LoginToken getToken() {
HttpSession session = WebMvcConfig.getSession();
LoginToken token = (LoginToken)session.getAttribute(TOKEN_NAME);
return token;
}
@Override
abstract public LoginToken doLogin(String username, String password);
@Override
public void doLogout() {
HttpSession session = WebMvcConfig.getSession();
session.removeAttribute(TOKEN_NAME);
session.invalidate();
}
@Override
public boolean isLogin() {
return this.getToken() != null;
}
}
...@@ -12,22 +12,15 @@ import com.egolm.common.StringUtil; ...@@ -12,22 +12,15 @@ import com.egolm.common.StringUtil;
import com.egolm.common.jdbc.JdbcTemplate; import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.film.config.WebMvcConfig; import com.egolm.film.config.WebMvcConfig;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
import com.egolm.film.login.service.AdminTokenService; import com.egolm.film.login.service.AbstractTokenService;
import com.egolm.film.model.LoginToken; import com.egolm.film.model.LoginToken;
@Service @Service
public class AdminTokenServiceImpl implements AdminTokenService { public class AdminTokenServiceImpl extends AbstractTokenService {
@Autowired @Autowired
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Override
public LoginToken getToken() {
HttpSession session = WebMvcConfig.getSession();
LoginToken token = (LoginToken)session.getAttribute(TOKEN_NAME);
return token;
}
@Override @Override
public LoginToken doLogin(String username, String password) { public LoginToken doLogin(String username, String password) {
HttpSession session = WebMvcConfig.getSession(); HttpSession session = WebMvcConfig.getSession();
...@@ -58,16 +51,5 @@ public class AdminTokenServiceImpl implements AdminTokenService { ...@@ -58,16 +51,5 @@ public class AdminTokenServiceImpl implements AdminTokenService {
} }
} }
@Override
public void doLogout() {
HttpSession session = WebMvcConfig.getSession();
session.removeAttribute(TOKEN_NAME);
session.invalidate();
}
@Override
public boolean isLogin() {
return this.getToken() != null;
}
} }
...@@ -12,22 +12,15 @@ import com.egolm.common.StringUtil; ...@@ -12,22 +12,15 @@ import com.egolm.common.StringUtil;
import com.egolm.common.jdbc.JdbcTemplate; import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.film.config.WebMvcConfig; import com.egolm.film.config.WebMvcConfig;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
import com.egolm.film.login.service.MemberTokenService; import com.egolm.film.login.service.AbstractTokenService;
import com.egolm.film.model.LoginToken; import com.egolm.film.model.LoginToken;
@Service @Service
public class MemberTokenServiceImpl implements MemberTokenService { public class MemberTokenServiceImpl extends AbstractTokenService {
@Autowired @Autowired
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Override
public LoginToken getToken() {
HttpSession session = WebMvcConfig.getSession();
LoginToken token = (LoginToken)session.getAttribute(TOKEN_NAME);
return token;
}
@Override @Override
public LoginToken doLogin(String username, String password) { public LoginToken doLogin(String username, String password) {
HttpSession session = WebMvcConfig.getSession(); HttpSession session = WebMvcConfig.getSession();
...@@ -58,16 +51,4 @@ public class MemberTokenServiceImpl implements MemberTokenService { ...@@ -58,16 +51,4 @@ public class MemberTokenServiceImpl implements MemberTokenService {
} }
} }
@Override
public void doLogout() {
HttpSession session = WebMvcConfig.getSession();
session.removeAttribute(TOKEN_NAME);
session.invalidate();
}
@Override
public boolean isLogin() {
return this.getToken() != null;
}
} }
...@@ -12,22 +12,15 @@ import com.egolm.common.StringUtil; ...@@ -12,22 +12,15 @@ import com.egolm.common.StringUtil;
import com.egolm.common.jdbc.JdbcTemplate; import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.film.config.WebMvcConfig; import com.egolm.film.config.WebMvcConfig;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
import com.egolm.film.login.service.UserTokenService; import com.egolm.film.login.service.AbstractTokenService;
import com.egolm.film.model.LoginToken; import com.egolm.film.model.LoginToken;
@Service @Service
public class UserTokenServiceImpl implements UserTokenService { public class UserTokenServiceImpl extends AbstractTokenService {
@Autowired @Autowired
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Override
public LoginToken getToken() {
HttpSession session = WebMvcConfig.getSession();
LoginToken token = (LoginToken)session.getAttribute(TOKEN_NAME);
return token;
}
@Override @Override
public LoginToken doLogin(String username, String password) { public LoginToken doLogin(String username, String password) {
HttpSession session = WebMvcConfig.getSession(); HttpSession session = WebMvcConfig.getSession();
...@@ -58,16 +51,4 @@ public class UserTokenServiceImpl implements UserTokenService { ...@@ -58,16 +51,4 @@ public class UserTokenServiceImpl implements UserTokenService {
} }
} }
@Override
public void doLogout() {
HttpSession session = WebMvcConfig.getSession();
session.removeAttribute(TOKEN_NAME);
session.invalidate();
}
@Override
public boolean isLogin() {
return this.getToken() != null;
}
} }
...@@ -2,15 +2,12 @@ package com.egolm.film.messages; ...@@ -2,15 +2,12 @@ package com.egolm.film.messages;
import java.util.Locale; import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException; import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestContextUtils;
import com.egolm.film.config.WebMvcConfig;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
@Component @Component
...@@ -21,8 +18,7 @@ public class DefaultMessages implements Messages { ...@@ -21,8 +18,7 @@ public class DefaultMessages implements Messages {
public String get(String name, String... args) { public String get(String name, String... args) {
try { try {
HttpServletRequest request = WebMvcConfig.getRequest(); Locale locale = LocaleContextHolder.getLocale();
Locale locale = RequestContextUtils.getLocale(request);
return messageSource.getMessage(name, args, locale == null ? Locale.CHINA : locale); return messageSource.getMessage(name, args, locale == null ? Locale.CHINA : locale);
} catch (NoSuchMessageException e) { } catch (NoSuchMessageException e) {
return name; return name;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment