Bladeren bron

退出登录功能模块开发完成

wuxw7 7 jaren geleden
bovenliggende
commit
c267611cd1

+ 7 - 21
Api/src/main/java/com/java110/api/listener/CheckLoginServiceListener.java

@@ -46,24 +46,8 @@ public class CheckLoginServiceListener extends AbstractServiceApiDataFlowListene
 
 
 
 
     /**
     /**
-     * 请求参数格式:
-     * {
-     "userId": "-1",
-     "name": "张三",
-     "email": "928255095@qq.com",
-     "address": "青海省西宁市城中区129号",
-     "password": "ERCBHDUYFJDNDHDJDNDJDHDUDHDJDDKDK",
-     "locationCd": "001",
-     "age": 19,
-     "sex": "0",
-     "tel": "17797173943",
-     "level_cd": "1",
-     "businessUserAttr": [{
-     "attrId":"-1",
-     "specCd":"1001",
-     "value":"01"
-     }]
-     }
+     * 校验用户登录:
+     *
      * @param event
      * @param event
      */
      */
     @Override
     @Override
@@ -79,12 +63,14 @@ public class CheckLoginServiceListener extends AbstractServiceApiDataFlowListene
         try {
         try {
             Map<String, String> claims = AuthenticationFactory.verifyToken(paramObj.getString("token"));
             Map<String, String> claims = AuthenticationFactory.verifyToken(paramObj.getString("token"));
             if(claims == null || claims.isEmpty()){
             if(claims == null || claims.isEmpty()){
-                throw new AuthenticationException("证失败,从token中解析到信息为空");
+                throw new AuthenticationException("证失败,从token中解析到信息为空");
             }
             }
-            responseEntity = new ResponseEntity<String>(JSONObject.toJSONString(claims), HttpStatus.OK);
+            JSONObject resultInfo = new JSONObject();
+            resultInfo.put("userId",claims.get("userId"));
+            responseEntity = new ResponseEntity<String>(resultInfo.toJSONString(), HttpStatus.OK);
         } catch (Exception e) {
         } catch (Exception e) {
             //Invalid signature/claims
             //Invalid signature/claims
-            responseEntity = new ResponseEntity<String>("证失败,不是有效的token", HttpStatus.UNAUTHORIZED);
+            responseEntity = new ResponseEntity<String>("证失败,不是有效的token", HttpStatus.UNAUTHORIZED);
         }
         }
         dataFlowContext.setResponseEntity(responseEntity);
         dataFlowContext.setResponseEntity(responseEntity);
     }
     }

+ 85 - 0
Api/src/main/java/com/java110/api/listener/users/UserLogoutServiceListener.java

@@ -0,0 +1,85 @@
+package com.java110.api.listener.users;
+
+import com.alibaba.fastjson.JSONObject;
+import com.java110.api.listener.AbstractServiceApiDataFlowListener;
+import com.java110.common.constant.ServiceCodeConstant;
+import com.java110.common.util.Assert;
+import com.java110.core.annotation.Java110Listener;
+import com.java110.core.context.DataFlowContext;
+import com.java110.core.factory.AuthenticationFactory;
+import com.java110.entity.center.AppService;
+import com.java110.event.service.api.ServiceDataFlowEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+
+import javax.naming.AuthenticationException;
+import java.util.Map;
+
+/**
+ * 用户退出登录
+ * Created by wuxw on 2018/5/18.
+ */
+@Java110Listener("userLogoutServiceListener")
+public class UserLogoutServiceListener extends AbstractServiceApiDataFlowListener{
+
+    private final static Logger logger = LoggerFactory.getLogger(UserLogoutServiceListener.class);
+
+
+
+    @Override
+    public String getServiceCode() {
+        return ServiceCodeConstant.SERVICE_CODE_USER_SERVICE_LOGOUT;
+    }
+
+
+    @Override
+    public int getOrder() {
+        return 0;
+    }
+
+
+    /**
+     * 校验用户登录:
+     *
+     * @param event
+     */
+    @Override
+    public void soService(ServiceDataFlowEvent event) {
+        //获取数据上下文对象
+        DataFlowContext dataFlowContext = event.getDataFlowContext();
+        String paramIn = dataFlowContext.getReqData();
+        Assert.isJsonObject(paramIn,"用户注册请求参数有误,不是有效的json格式 "+paramIn);
+        Assert.jsonObjectHaveKey(paramIn,"token","请求报文中未包含token 节点请检查");
+        JSONObject paramObj = JSONObject.parseObject(paramIn);
+        ResponseEntity responseEntity= null;
+        try {
+            //删除 token 信息
+            AuthenticationFactory.deleteToken(paramObj.getString("token"));
+            responseEntity = new ResponseEntity<String>("退出登录成功", HttpStatus.OK);
+        } catch (Exception e) {
+            //Invalid signature/claims
+            responseEntity = new ResponseEntity<String>("退出登录失败,请联系管理员", HttpStatus.UNAUTHORIZED);
+        }
+        dataFlowContext.setResponseEntity(responseEntity);
+    }
+
+    /**
+     * 对请求报文处理
+     * @param paramIn
+     * @return
+     */
+    private JSONObject refreshParamIn(String paramIn){
+        JSONObject paramObj = JSONObject.parseObject(paramIn);
+        paramObj.put("userId","-1");
+        paramObj.put("levelCd","0");
+
+        return paramObj;
+    }
+
+
+
+
+
+}

+ 4 - 0
java110-common/src/main/java/com/java110/common/constant/ServiceCodeConstant.java

@@ -178,6 +178,10 @@ public class ServiceCodeConstant {
      */
      */
     public static final String SERVICE_CODE_USER_SERVICE_LOGIN = "user.service.login";
     public static final String SERVICE_CODE_USER_SERVICE_LOGIN = "user.service.login";
 
 
+    /**
+     * 用户退出登录服务处理
+     */
+    public static final String SERVICE_CODE_USER_SERVICE_LOGOUT = "user.service.logout";
     /**
     /**
      * 检查用户登录服务处理
      * 检查用户登录服务处理
      */
      */

+ 22 - 0
java110-core/src/main/java/com/java110/core/factory/AuthenticationFactory.java

@@ -315,6 +315,28 @@ public class AuthenticationFactory {
         return jwt.sign(algorithm);
         return jwt.sign(algorithm);
     }
     }
 
 
+    /**
+     * 删除Token
+     * @param token
+     * @return
+     * @throws Exception
+     */
+    public static void deleteToken(String token) throws Exception{
+        String jwtSecret = MappingCache.getValue(MappingConstant.KEY_JWT_SECRET);
+        if(StringUtil.isNullOrNone(jwtSecret)){
+            jwtSecret = CommonConstant.DEFAULT_JWT_SECRET;
+        }
+        Algorithm algorithm = Algorithm.HMAC256(jwtSecret);
+        JWTVerifier verifier = JWT.require(algorithm).withIssuer("java110").build();
+        DecodedJWT jwt = verifier.verify(token);
+        String jdi = jwt.getId();
+        //保存token Id
+        String userId = JWTCache.getValue(jdi);
+        if(!StringUtil.isNullOrNone(userId)){ //说明redis中jdi 已经失效
+            JWTCache.removeValue(jdi);
+        }
+    }
+
     /**
     /**
      * 校验Token
      * 校验Token
      * @param token
      * @param token