[置顶]       安全-用户身份验证

有两种验证方法:

1、通过Cookie验证用户

用户登录游戏的时候进行Cookie设置
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
String sessionKey = (String) request.getParameter(Constants.PARAM_XN_SIG_SESSION_KEY);
 
if   (sessionKey == null ) {
     Exception e = new   Exception( "xn_sig_session_key is null!" );
     throw   e;
}
 
PremierContext.setCurrentUserSessionKey(sessionKey);
 
// 把sessionKey保存到Cookie
Cookie cookie = new   Cookie(Constants.COOKIE_KEY_CURRENTSESSIONKEY, sessionKey);
cookie.setMaxAge(Constants.SECONDS_OF_DAY);
response.addCookie(cookie);
// 设置Cookie初始化flag为true,为了初始化Cookie的时候不进行Cookie校验
request.setAttribute(Constants.REQUEST_KEY_INITCOOKIE, true );
 
通过Cookie进行当前用户校验
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 从Memcache取得sessionKey
String sessionKey = (String) MemcacheUtil.get(Constants.MEM_KEY_XN_SIG_SESSION_PREFIX + playerId);
 
// 通过Cookie进行当前用户校验
boolean   hasCookie = false ;
if   (request.getAttribute(Constants.REQUEST_KEY_INITCOOKIE) == null
         || !(Boolean) request.getAttribute(Constants.REQUEST_KEY_INITCOOKIE)) {
     Cookie[] cookies = request.getCookies();
     if   (cookies != null   && cookies.length != 0 ) {
         for   (Cookie cookie : cookies) {
             if   (StringUtils.equals(Constants.COOKIE_KEY_CURRENTSESSIONKEY, cookie.getName())) {
                 hasCookie = true ;
                 if   (StringUtils.equals(sessionKey, cookie.getValue())) {
                     // 是同一个用户,验证通过
                     break ;
                 } else   {
                     Exception e = new   Exception( "playerId is not the Login player!" );
                     throw   e;
                 }
             }
         }
     }
     // 如果没有Cookie,说明用户没有正常登录
     if   (!hasCookie) {
         Exception e = new   Exception( "Cookie is not exit!" );
         throw   e;
     }
}

 

2、通过MD5进行签名验证

每个链接传递的参数不止包括用户信息还包括后台生成的签名,获得签名后验证是否合法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
             String sigReceived = request.getParameter( "sig" );
 
             String appId = request.getParameter( "appId" );
 
             String userId = request.getParameter( "userId" );
 
             String inviteId = request.getParameter( "inviteId" );
 
             ArrayList<String> sigList = new   ArrayList<String>();
             sigList.add( "appId="   + appId);
             sigList.add( "userId="   + userId);
             sigList.add( "inviteId="   + inviteId);
 
             String sigGenerated = CommonUtil.generateSignature(sigList, Constants.SECRET);
 
             // 如果key相等 或者 算出的sig和收到的sig相等,则校验通过
             if   (sigGenerated.equals(sigReceived)) {
...
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
  * 生成签名
  *
  * @param params
  * @param secret
  * @return
  */
public   static   String generateSignature(List<String> params, String secret) {
 
     StringBuffer buffer = new   StringBuffer();
     // 对 List<String> params 三个参数字段 采取稳定的字典排序, 然后 append
     Collections.sort(params);
     for   (String param : params) {
         buffer.append(param);
     }
     // 继续append 应用的 secret
     buffer.append(secret);
     // 生成 buffer 的 MD5 值
     try   {
         java.security.MessageDigest md = java.security.MessageDigest.getInstance( "MD5" );
         StringBuffer result = new   StringBuffer();
         try   {
             for   ( byte   b : md.digest(buffer.toString().getBytes( "UTF-8" ))) {
                 result.append(Integer.toHexString((b & 0xf0 ) >>> 4 ));
                 result.append(Integer.toHexString(b & 0x0f ));
             }
         } catch   (UnsupportedEncodingException e) {
             for   ( byte   b : md.digest(buffer.toString().getBytes())) {
                 result.append(Integer.toHexString((b & 0xf0 ) >>> 4 ));
                 result.append(Integer.toHexString(b & 0x0f ));
             }
         }
         return   result.toString();
     } catch   (java.security.NoSuchAlgorithmException ex) {
         logger.error( "MD5 does not appear to be supported" , ex);
         return   Constants.REMARK_EMPTY;
     } catch   (Exception e) {
         logger.error( "generateSignature error" , e);
         return   Constants.REMARK_EMPTY;
     }
}

 

你可能感兴趣的:(java,用户验证,安全用户身份验证)