jiapeng 7 years ago
parent
commit
ffd0d4d801
1 changed files with 24 additions and 6 deletions
  1. 24 6
      src/main/java/com/ekexiu/portal/oauth/OAuthService.java

+ 24 - 6
src/main/java/com/ekexiu/portal/oauth/OAuthService.java

@ -1,5 +1,7 @@
1 1
package com.ekexiu.portal.oauth;
2 2

3
import java.io.IOException;
4
import java.lang.reflect.Type;
3 5
import java.sql.Connection;
4 6
import java.sql.SQLException;
5 7
import java.util.HashMap;
@ -7,6 +9,7 @@ import java.util.Map;
7 9
import java.util.concurrent.TimeUnit;
8 10

9 11
import org.jfw.apt.annotation.Autowrie;
12
import org.jfw.apt.annotation.Nullable;
10 13
import org.jfw.apt.web.annotation.Path;
11 14
import org.jfw.apt.web.annotation.method.SetCookie;
12 15
import org.jfw.apt.web.annotation.operate.Get;
@ -14,6 +17,7 @@ import org.jfw.apt.web.annotation.operate.Post;
14 17
import org.jfw.apt.web.annotation.param.JdbcConn;
15 18
import org.jfw.util.context.JfwAppContext;
16 19
import org.jfw.util.exception.JfwBaseException;
20
import org.jfw.util.reflect.TypeReference;
17 21
import org.jfw.util.state.StateCode;
18 22

19 23
import com.ekexiu.portal.dao.ProfessorDao;
@ -23,10 +27,14 @@ import com.ekexiu.portal.po.Professor;
23 27
import com.ekexiu.portal.po.User;
24 28
import com.ekexiu.portal.po.UserOpenId;
25 29
import com.ekexiu.portal.pojo.SessionUser;
30
import com.ekexiu.portal.util.HttpUtil;
26 31

27 32
@Path("/oauth")
28 33
public class OAuthService {
29 34

35
	private static final Type MAP_TYPE = new TypeReference<Map<String, Object>>() {
36
	}.getType();
37

30 38
	private long expriesWithOpenId = 1000 * 60 * 10;
31 39

32 40
	@Autowrie
@ -36,6 +44,9 @@ public class OAuthService {
36 44
	@Autowrie
37 45
	private ProfessorDao professorDao;
38 46

47
	private String xcxAppid = "";
48
	private String xcxSecret = "";
49

39 50
	private Map<String, OAuthHandler> handlers;
40 51

41 52
	public Map<String, OAuthHandler> getHandlers() {
@ -122,7 +133,7 @@ public class OAuthService {
122 133
		User user = this.userDao.query(con, sc.getValue().getUserid());
123 134
		if (null == user)
124 135
			return null;
125
		
136

126 137
		JfwAppContext.removeCachedObject(authCode);
127 138
		SessionUser ret = new SessionUser();
128 139
		ret.setId(user.getId());
@ -191,16 +202,14 @@ public class OAuthService {
191 202
			}
192 203
		}
193 204
	}
194
	
195
	
196
	
205

197 206
	@Post
198 207
	@Path("/checkAssociated")
199
	public boolean associcated(@JdbcConn(false) Connection con,String userid,String oauthType)throws SQLException,JfwBaseException{
208
	public boolean associcated(@JdbcConn(false) Connection con, String userid, String oauthType) throws SQLException, JfwBaseException {
200 209
		if (null == this.handlers.get(oauthType)) {
201 210
			throw new JfwBaseException(-1, "invalid oauthType");
202 211
		}
203
		return userOpenIdDao.queryByUserid(con, oauthType, userid)!=null;
212
		return userOpenIdDao.queryByUserid(con, oauthType, userid) != null;
204 213
	}
205 214

206 215
	@Post
@ -238,6 +247,15 @@ public class OAuthService {
238 247
		return ret;
239 248
	}
240 249

250
	@Get
251
	@Path("/xcx/openid")
252
	public String jscode2session(String code,@Nullable String appid,@Nullable String secret) throws IOException {
253
		if(appid==null) appid = this.xcxAppid;
254
		if(secret ==null) secret = this.xcxSecret;
255
		return (String)HttpUtil.<Map<String,Object>>loadJsonObject("https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code
256
				+ "&grant_type=authorization_code", MAP_TYPE).get("openid");
257
	}
258

241 259
	public static class AuthLoginResponse {
242 260
		private boolean associated;
243 261
		private String authCode;