Skip to content

Commit

Permalink
如果密码正确,将生成 sequence, token 存入数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireply committed May 23, 2016
1 parent a33fde7 commit c63f168
Show file tree
Hide file tree
Showing 33 changed files with 247 additions and 108 deletions.
4 changes: 4 additions & 0 deletions WebRoot/profile/user.jsp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html lang="cn">
<head>
Expand All @@ -18,6 +19,9 @@
</head>
<body>
用户主页
userId: <s:property value="#session.userId" />
sequence: <s:property value="#session.sequence" />
token: <s:property value="#session.token"/>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="js/ie10-viewport-bug-workaround.js"></script>

Expand Down
48 changes: 46 additions & 2 deletions src/main/java/org/fireply/enter/action/LoginAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.fireply.enter.constant.ResultConstants.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand All @@ -13,6 +14,8 @@
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.fireply.enter.model.Authorization;
import org.fireply.enter.model.User;
import org.fireply.enter.security.Md5;
import org.fireply.enter.security.Sign;
import org.fireply.enter.service.LoginService;
Expand Down Expand Up @@ -47,7 +50,48 @@ public String execute() throws Exception {
if (userId != null && userPassword != null) {
String signedPassword = Md5.sign(userPassword);
success = loginService.loginByPassword(userId, signedPassword, remoteAddr);
} else if (cookies != null) { // 使用 Cookie 登录
if (success) {
// 数据库是否有 sequence, token 记录,如果有更新 session, 否则生成后存入 session
List<Authorization> authorizations = loginService.onceLogined(userId);
if (authorizations == null) {
List<String> unEncrypted = new ArrayList<>();
unEncrypted.add(userId);
unEncrypted.add(userPassword);

String sequence = Sign.encrypt(unEncrypted);
String token = Sign.encrypt(unEncrypted);

User user = (User) loginService.get(User.class, userId);
Authorization authorization = new Authorization();
authorization.setUser(user);
authorization.setSequence(sequence);
authorization.setToken(token);
authorization.setLastTime(new Date());

loginService.persist(authorization);

session.put("userId", userId);
session.put("sequence", sequence);
session.put("token", token);
} else {
String value;
for (Authorization auth : authorizations) {
value = auth.getUser().getId();
if (value != null && value.length() > 0) {
session.put("userId", value);
}
value = auth.getSequence();
if (value != null && value.length() > 0) {
session.put("sequence", value);
}
value = auth.getToken();
if (value != null && value.length() > 0) {
session.put("token", value);
}
}
}
}
} else if (cookies != null) {
String cookieUserId = null;
String cookieSequence = null;
String cookieToken = null;
Expand All @@ -68,7 +112,7 @@ public String execute() throws Exception {
}
}

if (found) {
if (found) { // 使用 Cookie 登陆
success = loginService.loginByCookie(cookieUserId, cookieSequence, cookieToken, remoteAddr);
} else {
return LOGIN;
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/fireply/enter/dao/impl/DaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

import org.fireply.enter.dao.Dao;
import org.fireply.enter.util.ClumnNameUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
Expand Down Expand Up @@ -50,18 +51,18 @@ public Object get(String modelName, Serializable id) {

@Override
public List get(Class<?> clazz, String fieldName, Object fieldValue) {
String hql = "from" + clazz.getSimpleName() + "where" + fieldName + "'" + fieldValue + "'";
String hql = "from " + clazz.getSimpleName() + " where " + ClumnNameUtil.getClumnName(fieldName) + "='" + fieldValue + "'";
return executeQuery(hql);
}

@Override
public List get(Class<?> clazz, Map<String, Object> fieldsMap) {
public List get(String modelName, String fieldName, Object fieldValue) {
// TODO Auto-generated method stub
return null;
}

@Override
public List get(String modelName, String fieldName, Object fieldValue) {
public List get(Class<?> clazz, Map<String, Object> fieldsMap) {
// TODO Auto-generated method stub
return null;
}
Expand Down Expand Up @@ -90,7 +91,8 @@ public List executeQuery(String hql, int firstResult, int maxResults) {
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
}
return query.list();
List list = query.list();
return list;
}

@Override
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/fireply/enter/model/Account.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fireply.enter.model;
// Generated 2016-5-22 15:15:44 by Hibernate Tools 4.3.1.Final
// Generated 2016-5-23 16:15:06 by Hibernate Tools 4.3.1.Final

import java.util.HashSet;
import java.util.Set;
Expand All @@ -9,30 +9,25 @@
*/
public class Account implements java.io.Serializable {

private String id;
private Integer id;
private String wexinId;
private String alipayId;
private Set<User> users = new HashSet<User>(0);

public Account() {
}

public Account(String id) {
this.id = id;
}

public Account(String id, String wexinId, String alipayId, Set<User> users) {
this.id = id;
public Account(String wexinId, String alipayId, Set<User> users) {
this.wexinId = wexinId;
this.alipayId = alipayId;
this.users = users;
}

public String getId() {
public Integer getId() {
return this.id;
}

public void setId(String id) {
public void setId(Integer id) {
this.id = id;
}

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/fireply/enter/model/Admin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.fireply.enter.model;
// Generated 2016-5-23 16:15:06 by Hibernate Tools 4.3.1.Final

/**
* Admin generated by hbm2java
*/
public class Admin implements java.io.Serializable {

private String id;
private User user;

public Admin() {
}

public Admin(String id, User user) {
this.id = id;
this.user = user;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public User getUser() {
return this.user;
}

public void setUser(User user) {
this.user = user;
}

}
11 changes: 5 additions & 6 deletions src/main/java/org/fireply/enter/model/Authorization.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fireply.enter.model;
// Generated 2016-5-22 15:15:44 by Hibernate Tools 4.3.1.Final
// Generated 2016-5-23 16:15:06 by Hibernate Tools 4.3.1.Final

import java.util.Date;

Expand All @@ -8,7 +8,7 @@
*/
public class Authorization implements java.io.Serializable {

private String id;
private Integer id;
private User user;
private String sequence;
private String token;
Expand All @@ -17,19 +17,18 @@ public class Authorization implements java.io.Serializable {
public Authorization() {
}

public Authorization(String id, User user, String sequence, String token, Date lastTime) {
this.id = id;
public Authorization(User user, String sequence, String token, Date lastTime) {
this.user = user;
this.sequence = sequence;
this.token = token;
this.lastTime = lastTime;
}

public String getId() {
public Integer getId() {
return this.id;
}

public void setId(String id) {
public void setId(Integer id) {
this.id = id;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fireply/enter/model/Category.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fireply.enter.model;
// Generated 2016-5-22 15:15:44 by Hibernate Tools 4.3.1.Final
// Generated 2016-5-23 16:15:06 by Hibernate Tools 4.3.1.Final

import java.util.HashSet;
import java.util.Set;
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/fireply/enter/model/Commit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fireply.enter.model;
// Generated 2016-5-22 15:15:44 by Hibernate Tools 4.3.1.Final
// Generated 2016-5-23 16:15:06 by Hibernate Tools 4.3.1.Final

import java.util.Date;

Expand All @@ -8,7 +8,7 @@
*/
public class Commit implements java.io.Serializable {

private String id;
private Integer id;
private News news;
private User user;
private String content;
Expand All @@ -17,26 +17,24 @@ public class Commit implements java.io.Serializable {
public Commit() {
}

public Commit(String id, News news, String content, Date createTime) {
this.id = id;
public Commit(News news, String content, Date createTime) {
this.news = news;
this.content = content;
this.createTime = createTime;
}

public Commit(String id, News news, User user, String content, Date createTime) {
this.id = id;
public Commit(News news, User user, String content, Date createTime) {
this.news = news;
this.user = user;
this.content = content;
this.createTime = createTime;
}

public String getId() {
public Integer getId() {
return this.id;
}

public void setId(String id) {
public void setId(Integer id) {
this.id = id;
}

Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/fireply/enter/model/Login.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fireply.enter.model;
// Generated 2016-5-22 15:15:44 by Hibernate Tools 4.3.1.Final
// Generated 2016-5-23 16:15:06 by Hibernate Tools 4.3.1.Final

import java.util.Date;

Expand All @@ -8,7 +8,7 @@
*/
public class Login implements java.io.Serializable {

private String id;
private Integer id;
private User user;
private int ip;
private Date lastTime;
Expand All @@ -22,16 +22,14 @@ public class Login implements java.io.Serializable {
public Login() {
}

public Login(String id, User user, int ip, Date lastTime) {
this.id = id;
public Login(User user, int ip, Date lastTime) {
this.user = user;
this.ip = ip;
this.lastTime = lastTime;
}

public Login(String id, User user, int ip, Date lastTime, Short passwordSuccess, Short passwordFailure,
Short cookieSuccess, Short sequenceFailure, Short multiDeviceFactor, Short ipFactor) {
this.id = id;
public Login(User user, int ip, Date lastTime, Short passwordSuccess, Short passwordFailure, Short cookieSuccess,
Short sequenceFailure, Short multiDeviceFactor, Short ipFactor) {
this.user = user;
this.ip = ip;
this.lastTime = lastTime;
Expand All @@ -43,11 +41,11 @@ public Login(String id, User user, int ip, Date lastTime, Short passwordSuccess,
this.ipFactor = ipFactor;
}

public String getId() {
public Integer getId() {
return this.id;
}

public void setId(String id) {
public void setId(Integer id) {
this.id = id;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fireply/enter/model/News.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fireply.enter.model;
// Generated 2016-5-22 15:15:44 by Hibernate Tools 4.3.1.Final
// Generated 2016-5-23 16:15:06 by Hibernate Tools 4.3.1.Final

import java.util.Date;
import java.util.HashSet;
Expand Down
Loading

0 comments on commit c63f168

Please sign in to comment.