Commit a81874b8 authored by Quxl's avatar Quxl

xx

parent 9c3e3a80
...@@ -6,7 +6,7 @@ import com.egolm.film.model.LoginToken; ...@@ -6,7 +6,7 @@ import com.egolm.film.model.LoginToken;
public interface TokenService { public interface TokenService {
final String JSESSIONID = "JSESSIONID"; final String LOGINID_COOKIE_NAME = "token";
LoginToken getToken(); LoginToken getToken();
......
...@@ -43,7 +43,7 @@ public class AdminTokenServiceImpl implements AdminTokenService { ...@@ -43,7 +43,7 @@ public class AdminTokenServiceImpl implements AdminTokenService {
Integer adminid = admin.getAdminid(); Integer adminid = admin.getAdminid();
LoginToken token = new LoginToken(adminid); LoginToken token = new LoginToken(adminid);
session.setAttribute(TOKEN_NAME, token); session.setAttribute(TOKEN_NAME, token);
Cookie cookie = WebMvcConfig.getCookie(JSESSIONID); Cookie cookie = WebMvcConfig.getOrCreateUUIDCookie(LOGINID_COOKIE_NAME, "/", 60*60, true);
if(cookie != null) { if(cookie != null) {
String sql = "update fc_admin set token = ? where adminid = ?"; String sql = "update fc_admin set token = ? where adminid = ?";
jdbcTemplate.executeUpdate(sql, cookie.getValue(), adminid); jdbcTemplate.executeUpdate(sql, cookie.getValue(), adminid);
......
...@@ -51,7 +51,7 @@ public class MemberTokenServiceImpl implements MemberTokenService { ...@@ -51,7 +51,7 @@ public class MemberTokenServiceImpl implements MemberTokenService {
Integer id = member.getId(); Integer id = member.getId();
LoginToken token = new LoginToken(id); LoginToken token = new LoginToken(id);
session.setAttribute(TOKEN_NAME, token); session.setAttribute(TOKEN_NAME, token);
Cookie cookie = WebMvcConfig.getCookie(JSESSIONID); Cookie cookie = WebMvcConfig.getCookie(LOGINID_COOKIE_NAME);
if(cookie != null) { if(cookie != null) {
memberService.updateToken(id, cookie.getValue()); memberService.updateToken(id, cookie.getValue());
} }
......
...@@ -47,7 +47,7 @@ public class UserTokenServiceImpl implements UserTokenService { ...@@ -47,7 +47,7 @@ public class UserTokenServiceImpl implements UserTokenService {
Long uid = user.getUid(); Long uid = user.getUid();
LoginToken token = new LoginToken(uid); LoginToken token = new LoginToken(uid);
session.setAttribute(TOKEN_NAME, token); session.setAttribute(TOKEN_NAME, token);
Cookie cookie = WebMvcConfig.getCookie(JSESSIONID); Cookie cookie = WebMvcConfig.getCookie(LOGINID_COOKIE_NAME);
if(cookie != null) { if(cookie != null) {
jdbcTemplate.executeUpdate("update fc_user set token = ? where uid = ?", cookie.getValue(), uid); jdbcTemplate.executeUpdate("update fc_user set token = ? where uid = ?", cookie.getValue(), uid);
} }
......
...@@ -18,6 +18,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; ...@@ -18,6 +18,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; 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.common.StringUtil;
import com.egolm.film.config.interceptor.AdminLoginInterceptor; import com.egolm.film.config.interceptor.AdminLoginInterceptor;
import com.egolm.film.config.interceptor.LocaleInterceptor; import com.egolm.film.config.interceptor.LocaleInterceptor;
import com.egolm.film.config.interceptor.MemberLoginInterceptor; import com.egolm.film.config.interceptor.MemberLoginInterceptor;
...@@ -98,6 +99,26 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter { ...@@ -98,6 +99,26 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter {
return null; return null;
} }
public static Cookie getOrCreateUUIDCookie(String name, String path, int expiry, boolean httpOnly) {
HttpServletRequest request = WebMvcConfig.getRequest();
if(request != null) {
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies) {
if(cookie.getName().equals(name)) {
return cookie;
}
}
}
}
Cookie cookie = new Cookie(name, StringUtil.getUid().toLowerCase());
cookie.setPath(path);
cookie.setHttpOnly(httpOnly);
cookie.setMaxAge(expiry);
getResponse().addCookie(cookie);
return cookie;
}
public static String getRemoteIp() { public static String getRemoteIp() {
HttpServletRequest request = WebMvcConfig.getRequest(); HttpServletRequest request = WebMvcConfig.getRequest();
String ip = request.getHeader("X-Real_IP"); String ip = request.getHeader("X-Real_IP");
......
...@@ -28,7 +28,7 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -28,7 +28,7 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter {
Cookie[] cookies = req.getCookies(); Cookie[] cookies = req.getCookies();
if(cookies != null) { if(cookies != null) {
for(Cookie cookie : cookies) { for(Cookie cookie : cookies) {
if(cookie.getName().equals(TokenService.JSESSIONID)) { if(cookie.getName().equals(TokenService.LOGINID_COOKIE_NAME)) {
sessionid = cookie.getValue(); sessionid = cookie.getValue();
} }
} }
......
...@@ -27,7 +27,7 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -27,7 +27,7 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter {
String sessionid = null; String sessionid = null;
Cookie[] cookies = req.getCookies(); Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies) { for(Cookie cookie : cookies) {
if(cookie.getName().equals(TokenService.JSESSIONID)) { if(cookie.getName().equals(TokenService.LOGINID_COOKIE_NAME)) {
sessionid = cookie.getValue(); sessionid = cookie.getValue();
} }
} }
......
...@@ -27,7 +27,7 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -27,7 +27,7 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter {
String sessionid = null; String sessionid = null;
Cookie[] cookies = req.getCookies(); Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies) { for(Cookie cookie : cookies) {
if(cookie.getName().equals(TokenService.JSESSIONID)) { if(cookie.getName().equals(TokenService.LOGINID_COOKIE_NAME)) {
sessionid = cookie.getValue(); sessionid = cookie.getValue();
} }
} }
......
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