Commit cf027b5a authored by Quxl's avatar Quxl

优化登陆

parent 99e999b0
...@@ -4,6 +4,8 @@ import com.egolm.film.model.LoginToken; ...@@ -4,6 +4,8 @@ import com.egolm.film.model.LoginToken;
public interface TokenService { public interface TokenService {
final String JSESSIONID = "JSESSIONID";
LoginToken getToken(); LoginToken getToken();
LoginToken doLogin(String username, String password); LoginToken doLogin(String username, String password);
...@@ -12,4 +14,5 @@ public interface TokenService { ...@@ -12,4 +14,5 @@ public interface TokenService {
boolean isLogin(); boolean isLogin();
boolean isLoginByToken(String token);
} }
...@@ -3,6 +3,7 @@ package com.egolm.film.api.admin.service.impl; ...@@ -3,6 +3,7 @@ package com.egolm.film.api.admin.service.impl;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -35,6 +36,11 @@ public class AdminTokenServiceImpl implements AdminTokenService { ...@@ -35,6 +36,11 @@ public class AdminTokenServiceImpl implements AdminTokenService {
Integer adminid = (Integer)map.get("adminid"); Integer adminid = (Integer)map.get("adminid");
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);
if(cookie != null) {
String sql = "update fc_admin set token = ? where id = ?";
jdbcTemplate.executeUpdate(sql, cookie.getValue(), adminid);
}
return token; return token;
} else if(state == 2) { } else if(state == 2) {
throw new XException("用户已禁用"); throw new XException("用户已禁用");
...@@ -69,4 +75,18 @@ public class AdminTokenServiceImpl implements AdminTokenService { ...@@ -69,4 +75,18 @@ public class AdminTokenServiceImpl implements AdminTokenService {
public boolean isLogin() { public boolean isLogin() {
return this.getToken() != null; return this.getToken() != null;
} }
@Override
public boolean isLoginByToken(String token_string) {
HttpSession session = WebMvcConfig.getSession();
try {
String sql = "select adminid from fc_admin where token = ?";
Integer adminid = jdbcTemplate.queryForInt(sql, token_string);
LoginToken token = new LoginToken(adminid);
session.setAttribute(TOKEN_NAME, token);
return true;
} catch (Exception e) {
return false;
}
}
} }
...@@ -125,4 +125,8 @@ public interface MemberService { ...@@ -125,4 +125,8 @@ public interface MemberService {
boolean changePasswordByMemberId(Integer member_id, String password, String newPassword); boolean changePasswordByMemberId(Integer member_id, String password, String newPassword);
Fc_member getMemberByToken(String token);
void updateToken(Integer member_id, String token_string);
} }
...@@ -466,4 +466,16 @@ public class MemberServiceImpl implements MemberService { ...@@ -466,4 +466,16 @@ public class MemberServiceImpl implements MemberService {
return false; return false;
} }
@Override
public Fc_member getMemberByToken(String token) {
String sql = "select * from fc_member where token = ?";
return jdbcTemplate.queryForBean(sql, Fc_member.class, token);
}
@Override
public void updateToken(Integer member_id, String token_string) {
String sql = "update fc_member set token = ? where id = ?";
jdbcTemplate.executeUpdate(sql, token_string, member_id);
}
} }
package com.egolm.film.api.member.service.impl; package com.egolm.film.api.member.service.impl;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -41,6 +42,10 @@ public class MemberTokenServiceImpl implements MemberTokenService { ...@@ -41,6 +42,10 @@ 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);
if(cookie != null) {
memberService.updateToken(id, cookie.getValue());
}
return token; return token;
} else { } else {
throw new XException(messages.get("err.user_pwd_err")); throw new XException(messages.get("err.user_pwd_err"));
...@@ -71,4 +76,23 @@ public class MemberTokenServiceImpl implements MemberTokenService { ...@@ -71,4 +76,23 @@ public class MemberTokenServiceImpl implements MemberTokenService {
return this.getToken() != null; return this.getToken() != null;
} }
@Override
public boolean isLoginByToken(String token_string) {
HttpSession session = WebMvcConfig.getSession();
try {
Fc_member member = memberService.getMemberByToken(token_string);
Integer state = member.getState();
if(state == 1) {
Integer id = member.getId();
LoginToken token = new LoginToken(id);
session.setAttribute(TOKEN_NAME, token);
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
} }
...@@ -3,6 +3,7 @@ package com.egolm.film.api.user.service.impl; ...@@ -3,6 +3,7 @@ package com.egolm.film.api.user.service.impl;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -35,6 +36,11 @@ public class UserTokenServiceImpl implements UserTokenService { ...@@ -35,6 +36,11 @@ public class UserTokenServiceImpl implements UserTokenService {
Integer uid = (Integer)map.get("uid"); Integer uid = (Integer)map.get("uid");
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);
if(cookie != null) {
String sql = "update fc_user set token = ? where id = ?";
jdbcTemplate.executeUpdate(sql, cookie.getValue(), uid);
}
return token; return token;
} else if(state == 2) { } else if(state == 2) {
throw new XException("用户已禁用"); throw new XException("用户已禁用");
...@@ -69,4 +75,18 @@ public class UserTokenServiceImpl implements UserTokenService { ...@@ -69,4 +75,18 @@ public class UserTokenServiceImpl implements UserTokenService {
public boolean isLogin() { public boolean isLogin() {
return this.getToken() != null; return this.getToken() != null;
} }
@Override
public boolean isLoginByToken(String token_string) {
HttpSession session = WebMvcConfig.getSession();
try {
String sql = "select uid from fc_user where token = ?";
Long uid = jdbcTemplate.queryForLong(sql, token_string);
LoginToken token = new LoginToken(uid);
session.setAttribute(TOKEN_NAME, token);
return true;
} catch (Exception e) {
return false;
}
}
} }
...@@ -2,6 +2,7 @@ package com.egolm.film.config; ...@@ -2,6 +2,7 @@ package com.egolm.film.config;
import java.util.EventListener; import java.util.EventListener;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
...@@ -82,4 +83,16 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter { ...@@ -82,4 +83,16 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter {
return null; return null;
} }
public static Cookie getCookie(String name) {
HttpServletRequest request = WebMvcConfig.getRequest();
if(request != null) {
Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
if(cookie.getName().equals(name)) {
return cookie;
}
}
}
return null;
}
} }
package com.egolm.film.config.interceptor; package com.egolm.film.config.interceptor;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -7,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -7,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.egolm.film.api.TokenService;
import com.egolm.film.api.admin.service.AdminTokenService; import com.egolm.film.api.admin.service.AdminTokenService;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
...@@ -21,6 +23,16 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -21,6 +23,16 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter {
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} else { } else {
String sessionid = null;
Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies) {
if(cookie.getName().equals(TokenService.JSESSIONID)) {
sessionid = cookie.getValue();
}
}
if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
return super.preHandle(req, resp, handler);
}
throw new XException("用户未登陆", 300); throw new XException("用户未登陆", 300);
} }
} }
......
package com.egolm.film.config.interceptor; package com.egolm.film.config.interceptor;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -7,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -7,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.egolm.film.api.TokenService;
import com.egolm.film.api.member.service.MemberTokenService; import com.egolm.film.api.member.service.MemberTokenService;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
...@@ -21,6 +23,16 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -21,6 +23,16 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter {
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} else { } else {
String sessionid = null;
Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies) {
if(cookie.getName().equals(TokenService.JSESSIONID)) {
sessionid = cookie.getValue();
}
}
if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
return super.preHandle(req, resp, handler);
}
throw new XException("用户未登陆", 300); throw new XException("用户未登陆", 300);
} }
} }
......
package com.egolm.film.config.interceptor; package com.egolm.film.config.interceptor;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -7,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -7,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.egolm.film.api.TokenService;
import com.egolm.film.api.user.service.UserTokenService; import com.egolm.film.api.user.service.UserTokenService;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
...@@ -21,6 +23,16 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -21,6 +23,16 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter {
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} else { } else {
String sessionid = null;
Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies) {
if(cookie.getName().equals(TokenService.JSESSIONID)) {
sessionid = cookie.getValue();
}
}
if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
return super.preHandle(req, resp, handler);
}
throw new XException("用户未登陆", 300); throw new XException("用户未登陆", 300);
} }
} }
......
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