Skip to content

Commit

Permalink
User
Browse files Browse the repository at this point in the history
  • Loading branch information
AJLoveChina committed Apr 15, 2016
1 parent d17f46c commit 82763ef
Show file tree
Hide file tree
Showing 9 changed files with 2,052 additions and 5,113 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ java_demo/Pengfu/target
java_demo/Pengfu/target/Pengfu-0.0.1-SNAPSHOT.war
*.jar
*.properties
java_demo/Pengfu/src/ajax/model/safe/User.java
java_demo/Pengfu/src/ajax/model/safe
*.log
5,699 changes: 590 additions & 5,109 deletions java_demo/Pengfu/qqconnect.log

Large diffs are not rendered by default.

1,143 changes: 1,143 additions & 0 deletions java_demo/Pengfu/qqconnect.log.1

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions java_demo/Pengfu/src/ajax/model/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public static <T> T getBy(String column, String columnValue, Class<T> cls) {
* @return
*/
public static <T> boolean isExist(String column, String columnValue, Class<T> cls) {
List<T> list = (List<T>) getBy(column, columnValue, cls);
return list != null;
T t = getBy(column, columnValue, cls);
return t != null;
}


Expand Down
74 changes: 74 additions & 0 deletions java_demo/Pengfu/src/ajax/model/safe/Github.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ajax.model.safe;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import ajax.model.entity.Info;
import ajax.tools.Tools;

public class Github {

public static String getToken(String code, String state) {
//String url = "https://github.com/login/oauth/access_token";
String url = "http://www.nigeerhuo.com";

String clientId = Tools.getConfig("githubClientId");
String clientSecret = Tools.getConfig("githubClientSecret");
String redirectUri = "http://www.nigeerhuo.com";

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);


post.setHeader("Accept", "application/json");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("clientId", clientId));
urlParameters.add(new BasicNameValuePair("clientSecret", clientSecret));
urlParameters.add(new BasicNameValuePair("redirectUri", redirectUri));
urlParameters.add(new BasicNameValuePair("code", code));

StringBuffer result = new StringBuffer();

try {

post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));


String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}

Info info = new Info();
info.setKey("Github");
info.setValue(result.toString());
info.save();

return result.toString();
} catch (Exception e) {
System.out.println(e.getMessage());
}

return null;
}

public static void main(String[] args) {
String token = Github.getToken("1", "1");
System.out.println(token);
}

}
15 changes: 15 additions & 0 deletions java_demo/Pengfu/src/ajax/model/safe/QQ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ajax.model.safe;

import ajax.tools.Tools;

public class QQ {
public static void main(String[] args) {
String url = "https://graph.qq.com/user/get_user_info";

String appid = Tools.getConfig("qqAppId");
String appkey = Tools.getConfig("qqAppKey");
String accessToken = "";
String openId = "";
String format = "json";
}
}
28 changes: 28 additions & 0 deletions java_demo/Pengfu/src/ajax/model/safe/Safe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ajax.model.safe;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

public class Safe {
private static final String STATE_ATTR = "aj-sign-state-avoid-csrf";

public static void generateState(HttpServletRequest request) {
String state = new Random().nextInt(10000) + 10000 + "";
request.getSession().setAttribute(STATE_ATTR, state);
}

public static String getState(HttpServletRequest request) {
String state = (String) request.getSession().getAttribute(STATE_ATTR);

if (state == null) {
Safe.generateState(request);
state = (String) request.getSession().getAttribute(STATE_ATTR);
}

String hh = "123";
return state;
}


}
25 changes: 25 additions & 0 deletions java_demo/Pengfu/src/ajax/model/safe/SignStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ajax.model.safe;

/**
* 登陆状态
* @author ajax
*
*/
public class SignStatus {
private boolean isLogin;
private User user;
public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean isLogin) {
this.isLogin = isLogin;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}


}
175 changes: 175 additions & 0 deletions java_demo/Pengfu/src/ajax/model/safe/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package ajax.model.safe;

import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import ajax.model.entity.Entity;
import ajax.tools.HibernateUtil;

public class User extends Entity<User>{

enum Sex{
BOY(1),
GIRL(2),
BUZHIDAO(3);

private int id;
Sex(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}


enum UserRights{
NORMAL(1, "普通用户"),
FORMID(4, "黑名单用户"),
ADMIN(99, "管理员");

private int id;
private String info;
private UserRights(int id, String info) {
this.id = id;
this.info = info;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}

enum Source{
QQ(1, "QQ"),
WEIBO(2, "WEIBO");

private int id;
private String prefix;


private Source(int id, String prefix) {
this.id = id;
this.prefix = prefix;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}

}

private int id;
private String username;
private int sex;
private String openId;
private String accessToken;
private int userRights = UserRights.NORMAL.id;
private String dateEntered;




public String getDateEntered() {
return dateEntered;
}
public void setDateEntered(String dateEntered) {
this.dateEntered = dateEntered;
}
public int getUserRights() {
return userRights;
}
public void setUserRights(int userRights) {
this.userRights = userRights;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

private static String getFinalOpenId(String openId, Source source) {
return source.getPrefix() + openId;
}

private static boolean sign(String openId) {
User u = User.getBy("openId", openId, User.class);

if (u != null) {
SignStatus ss = new SignStatus();
ss.setLogin(true);
ss.setUser(u);


return true;
} else {
return false;
}
}

public static void signWithQQ(String openId, String accessToken) {
String finalOpenId = getFinalOpenId(openId, Source.QQ);

// 注册
if (!User.isExist("openId", finalOpenId, User.class)) {
User u = new User();
u.setOpenId(finalOpenId);
u.save();
}

sign(finalOpenId);
}


}

0 comments on commit 82763ef

Please sign in to comment.