.
This commit is contained in:
@@ -2,12 +2,17 @@ package net.tccn.service;
|
||||
|
||||
import com.arangodb.ArangoDBException;
|
||||
import com.google.gson.Gson;
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.arango.ArangoSource;
|
||||
import net.tccn.user.User;
|
||||
import net.tccn.user.UserService;
|
||||
import org.redkale.net.http.HttpRequest;
|
||||
import org.redkale.net.http.HttpResponse;
|
||||
import org.redkale.net.http.HttpScope;
|
||||
import org.redkale.net.http.HttpServlet;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -17,6 +22,9 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
public class BaseServlet extends HttpServlet {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
public Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||
protected static Gson gson = new Gson();
|
||||
|
||||
@@ -31,8 +39,8 @@ public class BaseServlet extends HttpServlet {
|
||||
}
|
||||
|
||||
if (sessionid != null) {
|
||||
//User user = userService.current(sessionid);
|
||||
//request.setCurrentUser(user);
|
||||
User user = userService.current(sessionid);
|
||||
request.setCurrentUser(user);
|
||||
}
|
||||
|
||||
super.preExecute(request, response);
|
||||
@@ -40,7 +48,7 @@ public class BaseServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void authenticate(HttpRequest request, HttpResponse response) throws IOException {
|
||||
/* fixme: 权限拦截
|
||||
//fixme: 权限拦截
|
||||
if (request.currentUser() == null) {
|
||||
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
|
||||
response.finish(JBean.by(-2, "未登陆"));
|
||||
@@ -48,7 +56,7 @@ public class BaseServlet extends HttpServlet {
|
||||
response.finish(HttpScope.refer("/user/login.html"));
|
||||
}
|
||||
return;
|
||||
}*/
|
||||
}
|
||||
super.authenticate(request, response);
|
||||
}
|
||||
|
||||
|
||||
@@ -199,4 +199,13 @@ public class MetadataService extends BaseService { //arango
|
||||
return JBean.by(0, "");
|
||||
}
|
||||
|
||||
@RestMapping(name = "plat_list", comment = "平台列表")
|
||||
public JBean platList() {
|
||||
JBean jBean = new JBean();
|
||||
List<SysPlat> plats = SysPlat.dao.find();
|
||||
|
||||
jBean.setBody(plats);
|
||||
return jBean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
108
src/main/java/net/tccn/user/User.java
Normal file
108
src/main/java/net/tccn/user/User.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package net.tccn.user;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.arango.Doc;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.ConvertType;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/11/22 17:37.
|
||||
*/
|
||||
@Table(name = "sys_user", catalog = "db_dev")
|
||||
public class User extends Doc<User> {
|
||||
public static User dao = dao(User.class);
|
||||
|
||||
private String username;
|
||||
private String pwd;
|
||||
private Long createTime;
|
||||
private Long loginTime;
|
||||
private Integer status;
|
||||
private String sessionid;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String sessionid) {
|
||||
this.sessionid = sessionid;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@ConvertColumn(ignore = true,type = ConvertType.JSON)
|
||||
public String getPwd() {
|
||||
return pwd;
|
||||
}
|
||||
|
||||
public void setPwd(String pwd) {
|
||||
this.pwd = pwd;
|
||||
}
|
||||
|
||||
public Long getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public Long getLoginTime() {
|
||||
return loginTime;
|
||||
}
|
||||
|
||||
public void setLoginTime(Long loginTime) {
|
||||
this.loginTime = loginTime;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getSessionid() {
|
||||
return sessionid;
|
||||
}
|
||||
|
||||
public void setSessionid(String sessionid) {
|
||||
this.sessionid = sessionid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
//-------------------------------
|
||||
|
||||
public static String md5IfNeed(String password){
|
||||
if (password == null || password.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
return Utility.md5Hex(password);
|
||||
}
|
||||
|
||||
public JBean checkLogin(String pwd) {
|
||||
JBean jBean = JBean.by(0, "");
|
||||
|
||||
if (!this.pwd.equalsIgnoreCase(md5IfNeed(pwd))) {
|
||||
jBean.set(-1, "密码错误");
|
||||
} else if (status != 1) {
|
||||
jBean.set(-1, "登陆失败,限制登陆。");
|
||||
}
|
||||
jBean.setBody(this);
|
||||
return jBean;
|
||||
}
|
||||
|
||||
}
|
||||
56
src/main/java/net/tccn/user/UserService.java
Normal file
56
src/main/java/net/tccn/user/UserService.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package net.tccn.user;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.service.BaseService;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.net.http.RestSessionid;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/11/22 17:16.
|
||||
*/
|
||||
@RestService(name = "user", automapping = true, comment = "用户服务")
|
||||
public class UserService extends BaseService {
|
||||
|
||||
@RestMapping(name = "login", auth = false, comment = "登陆验证")
|
||||
public JBean login(@RestSessionid String sessionid,
|
||||
String username,
|
||||
String pwd) {
|
||||
User bean = new User();
|
||||
bean.setUsername(username);
|
||||
|
||||
User user = User.dao.findFirst(bean);
|
||||
if (user == null) {
|
||||
return JBean.by(-1, "登陆失败:账号无效");
|
||||
}
|
||||
|
||||
JBean jBean = user.checkLogin(pwd);
|
||||
if (jBean.getCode() == 0) {
|
||||
cacheSource.set(30 * 60 * 2,sessionid, User.class, user);
|
||||
|
||||
user.setSessionid(sessionid);
|
||||
user.setLoginTime(System.currentTimeMillis());
|
||||
user.update();
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(name = "current")
|
||||
public User current(@RestSessionid String sessionid) {
|
||||
return getT("user_" + sessionid, User.class, () -> User.dao.findFirst(new User(sessionid)));
|
||||
}
|
||||
|
||||
@RestMapping(name = "logout", comment = "退出登陆")
|
||||
public JBean logout(@RestSessionid String sessionid) {
|
||||
User user = User.dao.findFirst(new User(sessionid));
|
||||
if (user != null) {
|
||||
user.setSessionid("");
|
||||
user.update();
|
||||
}
|
||||
cacheSource.removeAsync("user_" + sessionid);
|
||||
|
||||
return new JBean();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user