-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d17f46c
commit 82763ef
Showing
9 changed files
with
2,052 additions
and
5,113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |