Authored by zhaoyue

Add openid login return token;unbind pwd is necessary

@@ -2,7 +2,7 @@ git pull @@ -2,7 +2,7 @@ git pull
2 git add --all src/main/java/* 2 git add --all src/main/java/*
3 git add push.sh 3 git add push.sh
4 git add pom.xml 4 git add pom.xml
5 -git commit -m "Add hasReport inter" 5 +git commit -m "Add openid login return token;unbind pwd is necessary"
6 #git push origin master 6 #git push origin master
7 git push origin zhaoyue-dev4 7 git push origin zhaoyue-dev4
8 git status 8 git status
@@ -16,6 +16,14 @@ public class TokenModel { @@ -16,6 +16,14 @@ public class TokenModel {
16 //随机生成的uuid 16 //随机生成的uuid
17 private String token; 17 private String token;
18 18
  19 + //使用openId 登录时,会返回该用户openid对应的account。
  20 + private String account;
  21 +
  22 + public TokenModel(String userId, String token) {
  23 + this.userId = userId;
  24 + this.token = token;
  25 + }
  26 +
19 public String getUserId() { 27 public String getUserId() {
20 return userId; 28 return userId;
21 } 29 }
@@ -31,4 +39,12 @@ public class TokenModel { @@ -31,4 +39,12 @@ public class TokenModel {
31 public void setToken(String token) { 39 public void setToken(String token) {
32 this.token = token; 40 this.token = token;
33 } 41 }
  42 +
  43 + public String getAccount() {
  44 + return account;
  45 + }
  46 +
  47 + public void setAccount(String account) {
  48 + this.account = account;
  49 + }
34 } 50 }
1 package com.xkl.controller; 1 package com.xkl.controller;
2 2
  3 +import com.wordnik.swagger.annotations.ApiImplicitParam;
  4 +import com.wordnik.swagger.annotations.ApiImplicitParams;
3 import com.wordnik.swagger.annotations.ApiOperation; 5 import com.wordnik.swagger.annotations.ApiOperation;
4 import com.xkl.authorization.annotation.Authorization; 6 import com.xkl.authorization.annotation.Authorization;
5 import com.xkl.authorization.annotation.CurrentUser; 7 import com.xkl.authorization.annotation.CurrentUser;
@@ -12,10 +14,7 @@ import com.xkl.config.ResultStatus; @@ -12,10 +14,7 @@ import com.xkl.config.ResultStatus;
12 import com.xkl.domain.*; 14 import com.xkl.domain.*;
13 import com.xkl.model.ReportDetailModel; 15 import com.xkl.model.ReportDetailModel;
14 import com.xkl.model.ResultModel; 16 import com.xkl.model.ResultModel;
15 -import com.xkl.repository.XklAmpReportDetailRespository;  
16 -import com.xkl.repository.XklAmpReportHealthScoreRespository;  
17 -import com.xkl.repository.XklAmpReportRespository;  
18 -import com.xkl.repository.XklMemberOpenidRespository; 17 +import com.xkl.repository.*;
19 import com.xkl.security.AntiXSS; 18 import com.xkl.security.AntiXSS;
20 import com.xkl.service.ILoginService; 19 import com.xkl.service.ILoginService;
21 import org.springframework.beans.factory.annotation.Autowired; 20 import org.springframework.beans.factory.annotation.Autowired;
@@ -43,15 +42,17 @@ public class OpenIdController { @@ -43,15 +42,17 @@ public class OpenIdController {
43 private ILoginService loginService; 42 private ILoginService loginService;
44 @Autowired 43 @Autowired
45 private XklMemberOpenidRespository xklMemberOpenidRespository; 44 private XklMemberOpenidRespository xklMemberOpenidRespository;
  45 + @Autowired
  46 + private UserRepository userRepository;
46 47
47 @LogAnnotation 48 @LogAnnotation
48 @AntiXSS 49 @AntiXSS
49 @Sign 50 @Sign
50 @RequestMapping(method = RequestMethod.POST) 51 @RequestMapping(method = RequestMethod.POST)
51 @ApiOperation(value = "OpenId绑定接口") 52 @ApiOperation(value = "OpenId绑定接口")
52 - public ResponseEntity<ResultModel> openIdBind(HttpServletRequest request,@RequestParam String username, @RequestParam String password, @RequestParam String openId, @RequestParam int openIdType,  
53 - @RequestParam String sign,@RequestParam long t,@RequestParam int type) {  
54 - if(!(boolean)request.getAttribute("signAspect")) 53 + public ResponseEntity<ResultModel> openIdBind(HttpServletRequest request, @RequestParam String username, @RequestParam String password, @RequestParam String openId, @RequestParam int openIdType,
  54 + @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
  55 + if (!(boolean) request.getAttribute("signAspect"))
55 return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK); 56 return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
56 57
57 Assert.notNull(username, "username can not be empty"); 58 Assert.notNull(username, "username can not be empty");
@@ -62,14 +63,14 @@ public class OpenIdController { @@ -62,14 +63,14 @@ public class OpenIdController {
62 if (user == null) {//用户,密码错误 63 if (user == null) {//用户,密码错误
63 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK); 64 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
64 } else { 65 } else {
65 - XklMemberOpenidEntity xklMemberOpenid=xklMemberOpenidRespository.findByAccountIdAndType(user.getId(),openIdType);  
66 - if(xklMemberOpenid == null ){ 66 + XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByAccountIdAndType(user.getId(), openIdType);
  67 + if (xklMemberOpenid == null) {
67 xklMemberOpenid = new XklMemberOpenidEntity(); 68 xklMemberOpenid = new XklMemberOpenidEntity();
68 xklMemberOpenid.setType(openIdType); 69 xklMemberOpenid.setType(openIdType);
69 xklMemberOpenid.setAccountId(user.getId()); 70 xklMemberOpenid.setAccountId(user.getId());
70 xklMemberOpenid.setMemberId(user.getMemberId()); 71 xklMemberOpenid.setMemberId(user.getMemberId());
71 xklMemberOpenid.setOpenid(openId); 72 xklMemberOpenid.setOpenid(openId);
72 - }else{//已经存在 73 + } else {//已经存在
73 xklMemberOpenid.setOpenid(openId); 74 xklMemberOpenid.setOpenid(openId);
74 } 75 }
75 xklMemberOpenidRespository.save(xklMemberOpenid); 76 xklMemberOpenidRespository.save(xklMemberOpenid);
@@ -77,53 +78,55 @@ public class OpenIdController { @@ -77,53 +78,55 @@ public class OpenIdController {
77 return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_BIND_SUCCESS), HttpStatus.OK); 78 return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_BIND_SUCCESS), HttpStatus.OK);
78 } 79 }
79 80
  81 +
80 @LogAnnotation 82 @LogAnnotation
81 @AntiXSS 83 @AntiXSS
  84 + @Authorization
82 @Sign 85 @Sign
83 @RequestMapping(method = RequestMethod.DELETE) 86 @RequestMapping(method = RequestMethod.DELETE)
84 @ApiOperation(value = "OpenId解除绑定接口") 87 @ApiOperation(value = "OpenId解除绑定接口")
85 - public ResponseEntity<ResultModel> openIdUnBind(HttpServletRequest request,@RequestParam String username,@RequestParam String password,@RequestParam String openId, @RequestParam int openIdType,  
86 - @RequestParam String sign,@RequestParam long t,@RequestParam int type) {  
87 - if(!(boolean)request.getAttribute("signAspect")) 88 + @ApiImplicitParams({
  89 + @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
  90 + })
  91 + public ResponseEntity<ResultModel> openIdUnBind(HttpServletRequest request, @CurrentUser User user, @RequestParam String openId, @RequestParam int openIdType,
  92 + @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
  93 + if (!(boolean) request.getAttribute("signAspect"))
88 return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK); 94 return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
89 95
90 - Assert.notNull(username, "username can not be empty");  
91 - Assert.notNull(password, "password can not be empty");  
92 -  
93 - User user = loginService.check(username, password);  
94 -  
95 - if (user == null) {//用户,密码错误  
96 - return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);  
97 - } else {  
98 - XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByAccountIdAndTypeAndOpenid(user.getId(), openIdType,openId);  
99 - if(xklMemberOpenid!=null) { 96 + XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByAccountIdAndTypeAndOpenid(user.getId(), openIdType, openId);
  97 + if (xklMemberOpenid != null) {
100 xklMemberOpenidRespository.delete(xklMemberOpenid); 98 xklMemberOpenidRespository.delete(xklMemberOpenid);
101 - }else{ 99 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_UNBIND_SUCESS), HttpStatus.OK);
  100 + } else {
102 return new ResponseEntity<>(ResultModel.error(ResultStatus.OPENID_ERROR), HttpStatus.OK); 101 return new ResponseEntity<>(ResultModel.error(ResultStatus.OPENID_ERROR), HttpStatus.OK);
103 } 102 }
104 } 103 }
105 - return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_UNBIND_SUCESS), HttpStatus.OK);  
106 - } 104 +
107 105
108 @LogAnnotation 106 @LogAnnotation
109 @AntiXSS 107 @AntiXSS
110 @Sign 108 @Sign
111 - @RequestMapping(value="/login",method = RequestMethod.POST) 109 + @RequestMapping(value = "/login", method = RequestMethod.POST)
112 @ApiOperation(value = "OpenId登录接口") 110 @ApiOperation(value = "OpenId登录接口")
113 - public ResponseEntity<ResultModel> openIdLogin(HttpServletRequest request,@RequestParam String openId, @RequestParam int openIdType,  
114 - @RequestParam String sign,@RequestParam long t,@RequestParam int type) {  
115 - if(!(boolean)request.getAttribute("signAspect")) 111 + public ResponseEntity<ResultModel> openIdLogin(HttpServletRequest request, @RequestParam String openId, @RequestParam int openIdType,
  112 + @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
  113 + if (!(boolean) request.getAttribute("signAspect"))
116 return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK); 114 return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
117 115
118 XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByOpenidAndType(openId, openIdType); 116 XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByOpenidAndType(openId, openIdType);
119 117
120 - if(xklMemberOpenid == null){ 118 + if (xklMemberOpenid == null) {
121 return new ResponseEntity<>(ResultModel.error(ResultStatus.OPENID_ERROR), HttpStatus.OK); 119 return new ResponseEntity<>(ResultModel.error(ResultStatus.OPENID_ERROR), HttpStatus.OK);
122 } 120 }
123 long accountId = xklMemberOpenid.getAccountId(); 121 long accountId = xklMemberOpenid.getAccountId();
124 TokenModel model = tokenManager.createToken(String.valueOf(accountId)); 122 TokenModel model = tokenManager.createToken(String.valueOf(accountId));
125 - 123 + User user = userRepository.findOne(accountId);
  124 + if (user.isStatus() == true) {
  125 + model.setAccount(user.getLoginAccount());
126 return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK); 126 return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
  127 + } else {
  128 + return new ResponseEntity<>(ResultModel.error(ResultStatus.USER_NOT_FOUND), HttpStatus.OK);
  129 + }
127 } 130 }
128 131
129 } 132 }
@@ -54,6 +54,7 @@ public class TokenController { @@ -54,6 +54,7 @@ public class TokenController {
54 } 54 }
55 //生成一个token,保存用户登录状态 55 //生成一个token,保存用户登录状态
56 TokenModel model = tokenManager.createToken(String.valueOf(user.getId())); 56 TokenModel model = tokenManager.createToken(String.valueOf(user.getId()));
  57 + model.setAccount(user.getLoginAccount());
57 return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK); 58 return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
58 } 59 }
59 60