Authored by zhaoyue

local run

... ... @@ -9,7 +9,11 @@ public enum ResultStatus {
USER_NOT_FOUND(-1002, "用户不存在"),
USER_NOT_LOGIN(-1004, "用户未登录"),
USER_IS_EXIT(-1005, "用户已注册"),
USER_LOGOUT(101,"修改密码成功,退出登录");
USER_LOGOUT(101,"修改密码成功,退出登录"),
// 111开头的都是与amp报告上传软件相关的
AMP_KEY_ERROR(-11100, "AMP密钥不匹配");
/**
* 返回码
... ...
package com.xkl.controller.uploadsoft;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentUser;
import com.xkl.authorization.manager.ITokenManager;
import com.xkl.authorization.model.TokenModel;
import com.xkl.config.ResultStatus;
import com.xkl.domain.AMPMachine;
import com.xkl.domain.Admin;
import com.xkl.model.ResultModel;
import com.xkl.repository.AMPMachineRepository;
import com.xkl.repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* AMP报告上传软件客户端登录及退出接口。
* 获取和删除token的请求地址,在Restful设计中其实就对应着登录和退出登录的资源映射
*/
@RestController
@RequestMapping("/uploadsoftwareaccount")
public class UploadSoftwareAccountController {
@Autowired
private AdminRepository adminRepository;
@Autowired
private AMPMachineRepository ampMachineRepository;
@Autowired
private ITokenManager tokenManager;
@RequestMapping(method = RequestMethod.POST)
@ApiOperation(value = "报告上传软件登录")
public ResponseEntity<ResultModel> login(@RequestParam String account, @RequestParam String password
, @RequestParam String ampserial, @RequestParam String ampkey) {
Assert.notNull(account, "account can not be empty");
Assert.notNull(password, "password can not be empty");
Assert.notNull(ampserial, "ampserial can not be empty");
Assert.notNull(ampkey, "ampkey can not be empty");
AMPMachine ampMachine = ampMachineRepository.findBySecretKey(ampkey.trim());
if (ampMachine == null ||// 未找到密钥所对应的机器
!ampMachine.getAMPSerial().equals(ampserial)) {
return new ResponseEntity<>(ResultModel.error(ResultStatus.AMP_KEY_ERROR), HttpStatus.NOT_FOUND);
}
Admin admin = adminRepository.findByAccount(account);
if (admin == null || //未注册
!admin.getPwd().equals(password)) { //密码错误
//提示用户名或密码错误
return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
}
//生成一个token,保存用户登录状态
TokenModel model = tokenManager.createToken(admin.getId());
return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE)
@Authorization
@ApiOperation(value = "退出登录")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> logout(@CurrentUser Admin admin) {
tokenManager.deleteToken(admin.getId());
return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
}
}
... ...
package com.xkl.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;
/**
* 用户数据的domain类
*/
@Entity
@Table(name = "xkl_amp_machine")
public class AMPMachine {
//machine id
@Id
@Column(name = "id")
private long id;
//AMP序列号或机器硬件码
@Column(name = "AMPSerial")
private String AMPSerial;
//上传软件密钥(明文存储)
@Column(name = "secret_key")
private String secretKey;
//所属公司id
@Column(name = "company_id")
private int companyId;
//创建时间
@Column(name = "create_time")
private Timestamp createTime;
//状态(0,不可用;1可用)
@Column(name = "status")
private int status;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAMPSerial() {
return AMPSerial;
}
public void setAMPSerial(String AMPSerial) {
this.AMPSerial = AMPSerial;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public int getCompanyId() {
return companyId;
}
public void setCompanyId(int companyId) {
this.companyId = companyId;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
... ...
package com.xkl.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* 用户数据的domain类
*/
@Entity
@Table(name = "xkl_admin")
public class Admin {
//用户id
@Id
@Column(name = "id")
private long id;
//账号
@Column(name = "account")
private String account;
//密码
@Column(name = "pwd")
private String pwd;
//账号类型
@Column(name = "type")
private int type;
//公司id
@Column(name = "coid")
private int coid;
//状态
@Column(name = "state")
private int state;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getCoid() {
return coid;
}
public void setCoid(int coid) {
this.coid = coid;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
... ...
... ... @@ -35,4 +35,52 @@ public class User {
@Column(name = "member_id")
private String member_id;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public String getMember_id() {
return member_id;
}
public void setMember_id(String member_id) {
this.member_id = member_id;
}
}
... ...
package com.xkl.repository;
import com.xkl.domain.AMPMachine;
import org.springframework.data.repository.CrudRepository;
/**
* AMPMachine 类的CRUD操作
*
* @see AMPMachine
*/
public interface AMPMachineRepository extends CrudRepository<AMPMachine, Long> {
public AMPMachine findBySecretKey(String secretKey);
}
... ...
package com.xkl.repository;
import com.xkl.domain.Admin;
import org.springframework.data.repository.CrudRepository;
/**
* Admin类的CRUD操作
* @see Admin
*/
public interface AdminRepository extends CrudRepository<Admin, Long> {
public Admin findByAccount(String account);
}
... ...