Authored by fangyeqing

ADD:add openid login interface

@@ -2,6 +2,7 @@ package com.xkl.controller; @@ -2,6 +2,7 @@ package com.xkl.controller;
2 2
3 import com.wordnik.swagger.annotations.ApiOperation; 3 import com.wordnik.swagger.annotations.ApiOperation;
4 import com.xkl.authorization.manager.ITokenManager; 4 import com.xkl.authorization.manager.ITokenManager;
  5 +import com.xkl.authorization.model.TokenModel;
5 import com.xkl.config.ResultStatus; 6 import com.xkl.config.ResultStatus;
6 import com.xkl.domain.User; 7 import com.xkl.domain.User;
7 import com.xkl.domain.XklMemberOpenidEntity; 8 import com.xkl.domain.XklMemberOpenidEntity;
@@ -47,10 +48,11 @@ public class OpenIdController { @@ -47,10 +48,11 @@ public class OpenIdController {
47 if (user == null) {//用户,密码错误 48 if (user == null) {//用户,密码错误
48 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND); 49 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
49 } else { 50 } else {
50 - XklMemberOpenidEntity xklMemberOpenid=xklMemberOpenidRespository.findByMemberIdAndType(user.getMemberId(),openIdtype); 51 + XklMemberOpenidEntity xklMemberOpenid=xklMemberOpenidRespository.findByAccountIdAndType(user.getId(),openIdtype);
51 if(xklMemberOpenid == null ){ 52 if(xklMemberOpenid == null ){
52 xklMemberOpenid = new XklMemberOpenidEntity(); 53 xklMemberOpenid = new XklMemberOpenidEntity();
53 xklMemberOpenid.setType(openIdtype); 54 xklMemberOpenid.setType(openIdtype);
  55 + xklMemberOpenid.setAccountId(user.getId());
54 xklMemberOpenid.setMemberId(user.getMemberId()); 56 xklMemberOpenid.setMemberId(user.getMemberId());
55 xklMemberOpenid.setOpenid(openId); 57 xklMemberOpenid.setOpenid(openId);
56 }else{//已经存在 58 }else{//已经存在
@@ -76,7 +78,7 @@ public class OpenIdController { @@ -76,7 +78,7 @@ public class OpenIdController {
76 if (user == null) {//用户,密码错误 78 if (user == null) {//用户,密码错误
77 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND); 79 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
78 } else { 80 } else {
79 - XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByMemberIdAndTypeAndOpenid(user.getMemberId(), openIdtype,openId); 81 + XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByAccountIdAndTypeAndOpenid(user.getId(), openIdtype,openId);
80 if(xklMemberOpenid!=null) { 82 if(xklMemberOpenid!=null) {
81 xklMemberOpenidRespository.delete(xklMemberOpenid); 83 xklMemberOpenidRespository.delete(xklMemberOpenid);
82 }else{ 84 }else{
@@ -86,4 +88,24 @@ public class OpenIdController { @@ -86,4 +88,24 @@ public class OpenIdController {
86 return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_UNBIND_SUCESS), HttpStatus.OK); 88 return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_UNBIND_SUCESS), HttpStatus.OK);
87 } 89 }
88 90
  91 + @AntiXSS
  92 + //@Sign
  93 + @RequestMapping(value="/login",method = RequestMethod.POST)
  94 + @ApiOperation(value = "OpenId登录接口")
  95 + public ResponseEntity<ResultModel> openIdLogin(HttpServletRequest request,@RequestParam String openId, @RequestParam int openIdtype,
  96 + @RequestParam String sign,@RequestParam long t,@RequestParam int type) {
  97 + XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByOpenidAndType(openId, openIdtype);
  98 +
  99 + if(xklMemberOpenid == null){
  100 + return new ResponseEntity<>(ResultModel.error(ResultStatus.OPENID_ERROR), HttpStatus.NOT_FOUND);
  101 + }
  102 + long accountId = xklMemberOpenid.getId();
  103 + TokenModel model = tokenManager.createToken(String.valueOf(accountId));
  104 +
  105 + return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
  106 + }
  107 +
  108 +
  109 +
  110 +
89 } 111 }
@@ -91,7 +91,7 @@ public class UserInfoController { @@ -91,7 +91,7 @@ public class UserInfoController {
91 user.setSalt(salt); 91 user.setSalt(salt);
92 userRepository.save(user); 92 userRepository.save(user);
93 tokenManager.deleteToken(String.valueOf(user.getId()));//退出登录 93 tokenManager.deleteToken(String.valueOf(user.getId()));//退出登录
94 - return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_MODPASS_LOGOUT), HttpStatus.OK); 94 + return new ResponseEntity<>(new ResultModel(ResultStatus.USER_MODPASS_LOGOUT), HttpStatus.OK);
95 } 95 }
96 96
97 @RequestMapping(method = RequestMethod.GET) 97 @RequestMapping(method = RequestMethod.GET)
@@ -16,6 +16,9 @@ public class XklMemberOpenidEntity { @@ -16,6 +16,9 @@ public class XklMemberOpenidEntity {
16 @Column(name = "id") 16 @Column(name = "id")
17 private long id; 17 private long id;
18 @Basic 18 @Basic
  19 + @Column(name = "account_id")
  20 + private long accountId;
  21 + @Basic
19 @Column(name = "member_id") 22 @Column(name = "member_id")
20 private long memberId; 23 private long memberId;
21 @Basic 24 @Basic
@@ -66,6 +66,10 @@ public class ResultModel { @@ -66,6 +66,10 @@ public class ResultModel {
66 return new ResultModel(ResultStatus.SUCCESS); 66 return new ResultModel(ResultStatus.SUCCESS);
67 } 67 }
68 68
  69 + public static ResultModel ok(ResultStatus ok) {
  70 + return new ResultModel(ok);
  71 + }
  72 +
69 public static ResultModel error(ResultStatus error) { 73 public static ResultModel error(ResultStatus error) {
70 return new ResultModel(error); 74 return new ResultModel(error);
71 } 75 }
@@ -7,7 +7,9 @@ import org.springframework.data.repository.CrudRepository; @@ -7,7 +7,9 @@ import org.springframework.data.repository.CrudRepository;
7 * Created by win7 on 2016/11/20. 7 * Created by win7 on 2016/11/20.
8 */ 8 */
9 public interface XklMemberOpenidRespository extends CrudRepository<XklMemberOpenidEntity, Long> { 9 public interface XklMemberOpenidRespository extends CrudRepository<XklMemberOpenidEntity, Long> {
10 - public XklMemberOpenidEntity findByMemberIdAndType(long memberId, int type); 10 + public XklMemberOpenidEntity findByAccountIdAndType(long accountId, int type);
11 11
12 - public XklMemberOpenidEntity findByMemberIdAndTypeAndOpenid(long memberId, int type, String openid); 12 + public XklMemberOpenidEntity findByAccountIdAndTypeAndOpenid(long accountId, int type, String openid);
  13 +
  14 + public XklMemberOpenidEntity findByOpenidAndType(String openid,int type);
13 } 15 }