jiapeng 7 years ago
parent
commit
9baf59c831

+ 49 - 10
src/main/java/com/ekexiu/portal/service/SysService.java

@ -46,6 +46,7 @@ import com.ekexiu.portal.po.Professor;
46 46
import com.ekexiu.portal.po.User;
47 47
import com.ekexiu.portal.po.UserOpenId;
48 48
import com.ekexiu.portal.pojo.SessionUser;
49
import com.ekexiu.portal.util.PictureVC;
49 50
50 51
@Path
51 52
public class SysService {
@ -1804,9 +1805,18 @@ public class SysService {
1804 1805
1805 1806
	@Get
1806 1807
	@Path("/vcWithBind")
1807
	public String reqBindBindMobilePhone(@JdbcConn(false) Connection con, String userid, String mobilePhone,String vcode,@SessionVal(value="verification",remove=true) String scode) throws JfwBaseException, SQLException {
1808
		if(!vcode.toUpperCase().equals(scode)){
1809
			throw new JfwBaseException(20001, "valid code error");
1808
	public String reqBindBindMobilePhone(@JdbcConn(false) Connection con, String userid, String mobilePhone,String vcode,@Nullable @SessionVal(value="verification",remove=true) String scode,@Nullable String token) throws JfwBaseException, SQLException {
1809
		if(token!=null){
1810
			if(!PictureVC.match(token, vcode.toUpperCase(), true)){
1811
				throw new JfwBaseException(20001, "valid code error");
1812
			}
1813
		}else{
1814
			if(scode==null) {
1815
				throw new IllegalArgumentException("not found session value:verification");
1816
			}
1817
			if(!vcode.toUpperCase().equals(scode)){
1818
				throw new JfwBaseException(20001, "valid code error");
1819
			}
1810 1820
		}
1811 1821
		User user = this.userDao.query(con, userid);
1812 1822
		if (null == user)
@ -1856,10 +1866,19 @@ public class SysService {
1856 1866
	 */
1857 1867
	@Get
1858 1868
	@Path("/regmobilephone")
1859
	public String regMobilePhone(@JdbcConn(false) Connection con, String mobilePhone, @DefaultValue("true") boolean checkExists,String vcode,@SessionVal(value="verification",remove=true) String scode)
1869
	public String regMobilePhone(@JdbcConn(false) Connection con, String mobilePhone, @DefaultValue("true") boolean checkExists,String vcode,@Nullable @SessionVal(value="verification",remove=true) String scode,@Nullable String token)
1860 1870
			throws JfwBaseException, SQLException {
1861
		if(!vcode.toUpperCase().equals(scode)){
1862
			throw new JfwBaseException(20001, "valid code error");
1871
		if(token!=null){
1872
			if(!PictureVC.match(token, vcode.toUpperCase(), true)){
1873
				throw new JfwBaseException(20001, "valid code error");
1874
			}
1875
		}else{
1876
			if(scode==null) {
1877
				throw new IllegalArgumentException("not found session value:verification");
1878
			}
1879
			if(!vcode.toUpperCase().equals(scode)){
1880
				throw new JfwBaseException(20001, "valid code error");
1881
			}
1863 1882
		}
1864 1883
		if (checkExists) {
1865 1884
			User user = this.userDao.queryByEmailOrMobilePhone(con, mobilePhone);
@ -1895,9 +1914,18 @@ public class SysService {
1895 1914
	
1896 1915
	@Get
1897 1916
	@Path("/phoneValidCode")
1898
	public String phoneValidCode(String phone,String vcode,@SessionVal(value="verification",remove=true)String scode) throws JfwBaseException{
1899
		if(!vcode.toUpperCase().equals(scode)){
1900
			throw new JfwBaseException(20001, "valid code error");
1917
	public String phoneValidCode(String phone,String vcode,@Nullable @SessionVal(value="verification",remove=true)String scode,@Nullable String token) throws JfwBaseException{
1918
		if(token!=null){
1919
			if(!PictureVC.match(token, vcode.toUpperCase(), true)){
1920
				throw new JfwBaseException(20001, "valid code error");
1921
			}
1922
		}else{
1923
			if(scode==null) {
1924
				throw new IllegalArgumentException("not found session value:verification");
1925
			}
1926
			if(!vcode.toUpperCase().equals(scode)){
1927
				throw new JfwBaseException(20001, "valid code error");
1928
			}
1901 1929
		}
1902 1930
		StateCode<String, String> sc = new StateCode<String, String>();
1903 1931
		final String key = JfwAppContext.cacheObjectAndGenKey(sc);
@ -2034,8 +2062,13 @@ public class SysService {
2034 2062
2035 2063
	@Post
2036 2064
	@Path("/checkPicture")
2037
	public boolean checkPictureVC(@SessionVal(value = "verification", defaultvalue = "null", remove = true) String verification, String submitVerification)
2065
	public boolean checkPictureVC(@Nullable @SessionVal(value = "verification", remove = false) String verification, String submitVerification,@Nullable String token)
2038 2066
			throws JfwBaseException {
2067
		if(token!=null){
2068
			return PictureVC.match(token, submitVerification.toUpperCase(), false);
2069
		}
2070
		
2071
		
2039 2072
		if (null == verification) {
2040 2073
			throw new JfwBaseException("Picture is expire !");
2041 2074
		}
@ -2205,6 +2238,12 @@ public class SysService {
2205 2238
		return emails.length;
2206 2239
	}
2207 2240
2241
	@Get
2242
	@Path("/guid")
2243
	public String guid(){
2244
		return StringUtil.buildUUID();
2245
	}
2246
2208 2247
	public void sendInviteMail(@JdbcConn Connection con, String email, @Nullable String mobilePhone, String inviteCode)
2209 2248
			throws SQLException, MessagingException {
2210 2249
		String mailContent = this.inviteMailContentTempalte;

+ 97 - 18
src/main/java/com/ekexiu/portal/util/PictureVC.java

@ -1,6 +1,9 @@
1 1
package com.ekexiu.portal.util;
2 2

3 3
import java.io.IOException;
4
import java.util.LinkedList;
5
import java.util.concurrent.ConcurrentHashMap;
6
import java.util.concurrent.TimeUnit;
4 7

5 8
import javax.servlet.Servlet;
6 9
import javax.servlet.ServletException;
@ -9,23 +12,99 @@ import javax.servlet.http.HttpServletRequest;
9 12
import javax.servlet.http.HttpServletResponse;
10 13
import javax.servlet.http.HttpSession;
11 14

12
public class PictureVC extends HttpServlet implements Servlet {  
13
  
15
import org.jfw.util.context.JfwAppContext;
16
import org.jfw.util.state.StateCode;
17

18
public class PictureVC extends HttpServlet implements Servlet {
19

14 20
	private static final long serialVersionUID = 2459158903781864570L;
15 21

16
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
17
        response.setHeader("Pragma", "No-cache");  
18
        response.setHeader("Cache-Control", "no-cache");  
19
        response.setDateHeader("Expires", 0);  
20
        response.setContentType("image/jpeg");  
21
        HttpSession session = request.getSession();
22
        //生成随机字符串  
23
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
24
        //将验证码设置到session里 方便验证
25
        session.setAttribute("verification", verifyCode);
26
        //生成图片  
27
        int w = 100, h = 40;  
28
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);  
29
  
30
    }  
31
} 
22
	private static long timeout = 1000 * 60 * 5;
23
	private static long interval = 1000 * 60;
24

25
	private static ConcurrentHashMap<String, StateCode<String, String>> tokens = new ConcurrentHashMap<String, StateCode<String, String>>();
26

27
	public static void put(String key, String val) {
28
		StateCode<String, String> code = new StateCode<String, String>();
29
		code.setBuildTime(System.currentTimeMillis());
30
		code.setExpiredTime(code.getBuildTime() + timeout);
31
		code.setKey(key);
32
		code.setValue(val);
33
		tokens.put(key, code);
34
	}
35

36
	public static boolean match(String key, String val, boolean removed) {
37
		boolean result = false;
38
		StateCode<String, String> code = tokens.get(key);
39
		if (code != null) {
40
			if (System.currentTimeMillis() < code.getExpiredTime()) {
41
				result = code.getValue().equals(val);
42
				if (removed) {
43
					tokens.remove(key, code);
44
				}
45
			}
46
		}
47
		return result;
48
	}
49

50
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51
		response.setHeader("Pragma", "No-cache");
52
		response.setHeader("Cache-Control", "no-cache");
53
		response.setDateHeader("Expires", 0);
54
		response.setContentType("image/jpeg");
55

56
		// 生成随机字符串
57
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
58
		String token = request.getParameter("token");
59
		if (token != null && token.length() == 32) {
60
			put(token, verifyCode);
61
		} else {
62
			HttpSession session = request.getSession();
63
			// 将验证码设置到session里 方便验证
64
			session.setAttribute("verification", verifyCode);
65
		}
66
		// 生成图片
67
		int w = 100, h = 40;
68
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
69
	}
70

71
	@Override
72
	public void init() throws ServletException {
73
		long longValue = 0;
74
		String tmp = this.getServletConfig().getInitParameter("timeout");
75
		try {
76
			longValue = Long.parseLong(tmp);
77
			if (longValue > (1000 * 90)) {
78
				PictureVC.timeout = longValue;
79
			}
80
		} catch (Throwable th) {
81
		}
82

83
		tmp = this.getServletConfig().getInitParameter("cleanInterval");
84
		try {
85
			longValue = Long.parseLong(tmp);
86
			if (longValue > (1000)) {
87
				PictureVC.interval = longValue;
88
			}
89
		} catch (Throwable th) {
90
		}
91
		JfwAppContext.getScheduledExecutorService().scheduleAtFixedRate(new Runnable() {
92

93
			@Override
94
			public void run() {
95
				LinkedList<StateCode<String, String>> list = new LinkedList<StateCode<String, String>>();
96
				long time = System.currentTimeMillis();
97
				for (StateCode<String, String> st : tokens.values()) {
98
					if ((time - st.getExpiredTime()) > 1000) {
99
						list.add(st);
100
					}
101
				}
102
				for (StateCode<String, String> st : list) {
103
					tokens.remove(st.getKey(), st);
104
				}
105
			}
106
		}, 1000, PictureVC.interval, TimeUnit.MILLISECONDS);
107

108
	}
109

110
}