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
package com.ekexiu.portal.oauth;
1
package com.ekexiu.portal.oauth;
2

2

3
import java.io.IOException;
4
import java.lang.reflect.Type;
3
import java.sql.Connection;
5
import java.sql.Connection;
4
import java.sql.SQLException;
6
import java.sql.SQLException;
5
import java.util.HashMap;
7
import java.util.HashMap;
7
import java.util.concurrent.TimeUnit;
9
import java.util.concurrent.TimeUnit;
8

10

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

22

19
import com.ekexiu.portal.dao.ProfessorDao;
23
import com.ekexiu.portal.dao.ProfessorDao;
23
import com.ekexiu.portal.po.User;
27
import com.ekexiu.portal.po.User;
24
import com.ekexiu.portal.po.UserOpenId;
28
import com.ekexiu.portal.po.UserOpenId;
25
import com.ekexiu.portal.pojo.SessionUser;
29
import com.ekexiu.portal.pojo.SessionUser;
30
import com.ekexiu.portal.util.HttpUtil;
26

31

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

34

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

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

39

32
	@Autowrie
40
	@Autowrie
36
	@Autowrie
44
	@Autowrie
37
	private ProfessorDao professorDao;
45
	private ProfessorDao professorDao;
38

46

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

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

51

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

126
		JfwAppContext.removeCachedObject(authCode);
137
		JfwAppContext.removeCachedObject(authCode);
127
		SessionUser ret = new SessionUser();
138
		SessionUser ret = new SessionUser();
128
		ret.setId(user.getId());
139
		ret.setId(user.getId());
191
			}
202
			}
192
		}
203
		}
193
	}
204
	}
194
	
195
	
196
	
205

197
	@Post
206
	@Post
198
	@Path("/checkAssociated")
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
		if (null == this.handlers.get(oauthType)) {
209
		if (null == this.handlers.get(oauthType)) {
201
			throw new JfwBaseException(-1, "invalid oauthType");
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
	@Post
215
	@Post
238
		return ret;
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
	public static class AuthLoginResponse {
259
	public static class AuthLoginResponse {
242
		private boolean associated;
260
		private boolean associated;
243
		private String authCode;
261
		private String authCode;