Authored by fangyeqing

ADD:git openid bind interface

@@ -5,12 +5,20 @@ package com.xkl.config; @@ -5,12 +5,20 @@ package com.xkl.config;
5 */ 5 */
6 public enum ResultStatus { 6 public enum ResultStatus {
7 SUCCESS(100, "成功"), 7 SUCCESS(100, "成功"),
  8 +
  9 + USER_REGISTER(1000,"用户注册成功"),
  10 + USER_LOGOUT(1001,"退出登录成功"),
  11 + USER_MODPASS_LOGOUT(1002,"修改密码成功,退出登录"),
8 USERNAME_OR_PASSWORD_ERROR(-1001, "用户名或密码错误"), 12 USERNAME_OR_PASSWORD_ERROR(-1001, "用户名或密码错误"),
9 USER_NOT_FOUND(-1002, "用户不存在"), 13 USER_NOT_FOUND(-1002, "用户不存在"),
10 USER_NOT_LOGIN(-1004, "用户未登录"), 14 USER_NOT_LOGIN(-1004, "用户未登录"),
11 USER_IS_EXIT(-1005, "用户已注册"), 15 USER_IS_EXIT(-1005, "用户已注册"),
12 16
13 - USER_LOGOUT(101,"修改密码成功,退出登录"), 17 + //2开头的都是openId相关
  18 + OPENID_BIND_SUCCESS(2001,"OPENID绑定成功"),
  19 + OPENID_UNBIND_SUCESS(2002,"OPENID解除绑定成功"),
  20 + OPENID_ERROR(-2001,"OPENID错误"),
  21 +
14 22
15 // 111开头的都是与amp报告上传软件相关的 23 // 111开头的都是与amp报告上传软件相关的
16 AMP_KEY_ERROR(-11100, "AMP密钥不匹配"), 24 AMP_KEY_ERROR(-11100, "AMP密钥不匹配"),
  1 +package com.xkl.controller;
  2 +
  3 +import com.wordnik.swagger.annotations.ApiOperation;
  4 +import com.xkl.authorization.manager.ITokenManager;
  5 +import com.xkl.config.ResultStatus;
  6 +import com.xkl.domain.User;
  7 +import com.xkl.domain.XklMemberOpenidEntity;
  8 +import com.xkl.model.ResultModel;
  9 +import com.xkl.repository.XklMemberOpenidRespository;
  10 +import com.xkl.security.AntiXSS;
  11 +import com.xkl.service.ILoginService;
  12 +import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.http.HttpStatus;
  14 +import org.springframework.http.ResponseEntity;
  15 +import org.springframework.util.Assert;
  16 +import org.springframework.web.bind.annotation.RequestMapping;
  17 +import org.springframework.web.bind.annotation.RequestMethod;
  18 +import org.springframework.web.bind.annotation.RequestParam;
  19 +import org.springframework.web.bind.annotation.RestController;
  20 +
  21 +import javax.servlet.http.HttpServletRequest;
  22 +
  23 +/**
  24 + * Created by win7 on 2016/10/19.
  25 + */
  26 +@RestController
  27 +@RequestMapping("/openId")
  28 +public class OpenIdController {
  29 + @Autowired
  30 + private ITokenManager tokenManager;
  31 + @Autowired
  32 + private ILoginService loginService;
  33 + @Autowired
  34 + private XklMemberOpenidRespository xklMemberOpenidRespository;
  35 +
  36 + @AntiXSS
  37 + //@Sign
  38 + @RequestMapping(method = RequestMethod.POST)
  39 + @ApiOperation(value = "OpenId绑定接口")
  40 + public ResponseEntity<ResultModel> openIdBind(HttpServletRequest request,@RequestParam String username, @RequestParam String password, @RequestParam String openId, @RequestParam int openIdtype,
  41 + @RequestParam String sign,@RequestParam long t,@RequestParam int type) {
  42 + Assert.notNull(username, "username can not be empty");
  43 + Assert.notNull(password, "password can not be empty");
  44 +
  45 + User user = loginService.check(username, password);
  46 +
  47 + if (user == null) {//用户,密码错误
  48 + return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
  49 + } else {
  50 + XklMemberOpenidEntity xklMemberOpenid=xklMemberOpenidRespository.findByMemberIdAndType(user.getMemberId(),openIdtype);
  51 + if(xklMemberOpenid == null ){
  52 + xklMemberOpenid = new XklMemberOpenidEntity();
  53 + xklMemberOpenid.setType(openIdtype);
  54 + xklMemberOpenid.setMemberId(user.getMemberId());
  55 + xklMemberOpenid.setOpenid(openId);
  56 + }else{//已经存在
  57 + xklMemberOpenid.setOpenid(openId);
  58 + }
  59 + xklMemberOpenidRespository.save(xklMemberOpenid);
  60 + }
  61 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_BIND_SUCCESS), HttpStatus.OK);
  62 + }
  63 +
  64 +
  65 + @AntiXSS
  66 + //@Sign
  67 + @RequestMapping(method = RequestMethod.DELETE)
  68 + @ApiOperation(value = "OpenId解除绑定接口")
  69 + public ResponseEntity<ResultModel> openIdUnBind(HttpServletRequest request,@RequestParam String username,@RequestParam String password,@RequestParam String openId, @RequestParam int openIdtype,
  70 + @RequestParam String sign,@RequestParam long t,@RequestParam int type) {
  71 + Assert.notNull(username, "username can not be empty");
  72 + Assert.notNull(password, "password can not be empty");
  73 +
  74 + User user = loginService.check(username, password);
  75 +
  76 + if (user == null) {//用户,密码错误
  77 + return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
  78 + } else {
  79 + XklMemberOpenidEntity xklMemberOpenid = xklMemberOpenidRespository.findByMemberIdAndTypeAndOpenid(user.getMemberId(), openIdtype,openId);
  80 + if(xklMemberOpenid!=null) {
  81 + xklMemberOpenidRespository.delete(xklMemberOpenid);
  82 + }else{
  83 + return new ResponseEntity<>(ResultModel.error(ResultStatus.OPENID_ERROR), HttpStatus.NOT_FOUND);
  84 + }
  85 + }
  86 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_UNBIND_SUCESS), HttpStatus.OK);
  87 + }
  88 +
  89 +}
@@ -12,6 +12,7 @@ import com.wordnik.swagger.annotations.ApiImplicitParam; @@ -12,6 +12,7 @@ import com.wordnik.swagger.annotations.ApiImplicitParam;
12 import com.wordnik.swagger.annotations.ApiImplicitParams; 12 import com.wordnik.swagger.annotations.ApiImplicitParams;
13 import com.wordnik.swagger.annotations.ApiOperation; 13 import com.wordnik.swagger.annotations.ApiOperation;
14 import com.xkl.security.SecurityTool; 14 import com.xkl.security.SecurityTool;
  15 +import com.xkl.service.ILoginService;
15 import org.springframework.beans.factory.annotation.Autowired; 16 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.http.HttpStatus; 17 import org.springframework.http.HttpStatus;
17 import org.springframework.http.ResponseEntity; 18 import org.springframework.http.ResponseEntity;
@@ -29,10 +30,8 @@ import javax.servlet.http.HttpServletRequest; @@ -29,10 +30,8 @@ import javax.servlet.http.HttpServletRequest;
29 @RestController 30 @RestController
30 @RequestMapping("/token") 31 @RequestMapping("/token")
31 public class TokenController { 32 public class TokenController {
32 -  
33 @Autowired 33 @Autowired
34 - private UserRepository userRepository;  
35 - 34 + private ILoginService loginService;
36 @Autowired 35 @Autowired
37 private ITokenManager tokenManager; 36 private ITokenManager tokenManager;
38 37
@@ -44,18 +43,10 @@ public class TokenController { @@ -44,18 +43,10 @@ public class TokenController {
44 Assert.notNull(username, "username can not be empty"); 43 Assert.notNull(username, "username can not be empty");
45 Assert.notNull(password, "password can not be empty"); 44 Assert.notNull(password, "password can not be empty");
46 45
47 - User user = userRepository.findByLoginAccount(username); 46 + User user = loginService.check(username, password);
48 47
49 - if (user == null) { //用户不存在 48 + if (user == null) {//用户,密码错误
50 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);
51 - } else {  
52 - String salt = user.getSalt();  
53 - String pass_in_db = user.getLoginPwd();  
54 - String pass = SecurityTool.getPassword(username, password, salt);  
55 - if (!pass.equals(pass_in_db))  
56 - // TODO: 2016/11/26 use pwd with salt  
57 - // if(!password.equals(pass_in_db)) // for test  
58 - return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);  
59 } 50 }
60 //生成一个token,保存用户登录状态 51 //生成一个token,保存用户登录状态
61 TokenModel model = tokenManager.createToken(String.valueOf(user.getId())); 52 TokenModel model = tokenManager.createToken(String.valueOf(user.getId()));
@@ -72,7 +63,7 @@ public class TokenController { @@ -72,7 +63,7 @@ public class TokenController {
72 }) 63 })
73 public ResponseEntity<ResultModel> logout(@CurrentUser User user) { 64 public ResponseEntity<ResultModel> logout(@CurrentUser User user) {
74 tokenManager.deleteToken(String.valueOf(user.getId())); 65 tokenManager.deleteToken(String.valueOf(user.getId()));
75 - return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK); 66 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_LOGOUT), HttpStatus.OK);
76 } 67 }
77 68
78 } 69 }
@@ -25,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestParam; @@ -25,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestParam;
25 import org.springframework.web.bind.annotation.RestController; 25 import org.springframework.web.bind.annotation.RestController;
26 26
27 import javax.servlet.http.HttpServletRequest; 27 import javax.servlet.http.HttpServletRequest;
  28 +import javax.xml.transform.Result;
28 29
29 /** 30 /**
30 * Created by win7 on 2016/10/19. 31 * Created by win7 on 2016/10/19.
@@ -69,7 +70,7 @@ public class UserInfoController { @@ -69,7 +70,7 @@ public class UserInfoController {
69 user.setStatus(true); 70 user.setStatus(true);
70 userRepository.save(user); 71 userRepository.save(user);
71 } 72 }
72 - return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK); 73 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_REGISTER), HttpStatus.OK);
73 } 74 }
74 75
75 76
@@ -90,7 +91,7 @@ public class UserInfoController { @@ -90,7 +91,7 @@ public class UserInfoController {
90 user.setSalt(salt); 91 user.setSalt(salt);
91 userRepository.save(user); 92 userRepository.save(user);
92 tokenManager.deleteToken(String.valueOf(user.getId()));//退出登录 93 tokenManager.deleteToken(String.valueOf(user.getId()));//退出登录
93 - return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_LOGOUT), HttpStatus.OK); 94 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_MODPASS_LOGOUT), HttpStatus.OK);
94 } 95 }
95 96
96 @RequestMapping(method = RequestMethod.GET) 97 @RequestMapping(method = RequestMethod.GET)
  1 +package com.xkl.domain;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import javax.persistence.*;
  6 +
  7 +/**
  8 + * Created by win7 on 2016/12/18.
  9 + */
  10 +@Data
  11 +@Entity
  12 +@Table(name = "xkl_member_openid", schema = "hanhe_test", catalog = "")
  13 +public class XklMemberOpenidEntity {
  14 + @Id
  15 + @GeneratedValue
  16 + @Column(name = "id")
  17 + private long id;
  18 + @Basic
  19 + @Column(name = "member_id")
  20 + private long memberId;
  21 + @Basic
  22 + @Column(name = "openid")
  23 + private String openid;
  24 + @Basic
  25 + @Column(name = "type")
  26 + private int type;
  27 +}
  1 +package com.xkl.repository;
  2 +
  3 +import com.xkl.domain.XklMemberOpenidEntity;
  4 +import org.springframework.data.repository.CrudRepository;
  5 +
  6 +/**
  7 + * Created by win7 on 2016/11/20.
  8 + */
  9 +public interface XklMemberOpenidRespository extends CrudRepository<XklMemberOpenidEntity, Long> {
  10 + public XklMemberOpenidEntity findByMemberIdAndType(long memberId, int type);
  11 +
  12 + public XklMemberOpenidEntity findByMemberIdAndTypeAndOpenid(long memberId, int type, String openid);
  13 +}
  1 +package com.xkl.service;
  2 +
  3 +import com.xkl.domain.User;
  4 +
  5 +/**
  6 + * Created by win7 on 2016/12/18.
  7 + */
  8 +public interface ILoginService {
  9 + public User check(String username, String password);
  10 +}
  1 +package com.xkl.service;
  2 +
  3 +import com.xkl.domain.User;
  4 +import com.xkl.repository.UserRepository;
  5 +import com.xkl.security.SecurityTool;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.stereotype.Service;
  8 +
  9 +/**
  10 + * Created by win7 on 2016/12/18.
  11 + */
  12 +@Service
  13 +public class LoginServiceImpl implements ILoginService{
  14 + @Autowired
  15 + private UserRepository userRepository;
  16 + @Override
  17 + public User check(String username, String password) {
  18 + User user = userRepository.findByLoginAccount(username);
  19 +
  20 + if (user == null) { //用户不存在
  21 + return null;
  22 + } else {
  23 + String salt = user.getSalt();
  24 + String pass_in_db = user.getLoginPwd();
  25 + String pass = SecurityTool.getPassword(username, password, salt);
  26 + if (!pass.equals(pass_in_db))//密码错误
  27 + return null;
  28 + }
  29 + return user;
  30 + }
  31 +}