Authored by zhaoyue

add upsoft login inter

1 git add --all src/* 1 git add --all src/*
2 git add push.sh 2 git add push.sh
3 -git commit -m "local run" 3 +git commit -m "add upsoft login inter"
4 git push origin zhaoyue-dev 4 git push origin zhaoyue-dev
5 git status 5 git status
  1 +package com.xkl.authorization.annotation;
  2 +
  3 +import java.lang.annotation.ElementType;
  4 +import java.lang.annotation.Retention;
  5 +import java.lang.annotation.RetentionPolicy;
  6 +import java.lang.annotation.Target;
  7 +
  8 +/**
  9 + * 在Controller的方法参数中使用此注解,该方法在映射时会注入当前登录的Admin对象
  10 + * @see com.xkl.authorization.resolvers.CurrentUserMethodArgumentResolver
  11 + */
  12 +@Target(ElementType.PARAMETER)
  13 +@Retention(RetentionPolicy.RUNTIME)
  14 +public @interface CurrentAdmin {
  15 +}
@@ -7,7 +7,7 @@ import java.lang.annotation.Target; @@ -7,7 +7,7 @@ import java.lang.annotation.Target;
7 7
8 /** 8 /**
9 * 在Controller的方法参数中使用此注解,该方法在映射时会注入当前登录的User对象 9 * 在Controller的方法参数中使用此注解,该方法在映射时会注入当前登录的User对象
10 - * @see com.xkl.authorization.resolvers.CurrentUserMethodArgumentResolver 10 + * @see com.xkl.authorization.resolvers.CurrentAdminMethodArgumentResolver
11 */ 11 */
12 @Target(ElementType.PARAMETER) 12 @Target(ElementType.PARAMETER)
13 @Retention(RetentionPolicy.RUNTIME) 13 @Retention(RetentionPolicy.RUNTIME)
  1 +package com.xkl.authorization.resolvers;
  2 +
  3 +import com.xkl.authorization.annotation.CurrentAdmin;
  4 +import com.xkl.config.Constants;
  5 +import com.xkl.domain.Admin;
  6 +import com.xkl.repository.AdminRepository;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.core.MethodParameter;
  9 +import org.springframework.stereotype.Component;
  10 +import org.springframework.web.bind.support.WebDataBinderFactory;
  11 +import org.springframework.web.context.request.NativeWebRequest;
  12 +import org.springframework.web.context.request.RequestAttributes;
  13 +import org.springframework.web.method.support.HandlerMethodArgumentResolver;
  14 +import org.springframework.web.method.support.ModelAndViewContainer;
  15 +import org.springframework.web.multipart.support.MissingServletRequestPartException;
  16 +
  17 +/**
  18 + * 增加方法注入,将含有CurrentAdmin注解的方法参数注入当前登录用户
  19 + * @see CurrentAdmin
  20 + */
  21 +@Component
  22 +public class CurrentAdminMethodArgumentResolver implements HandlerMethodArgumentResolver {
  23 +
  24 + @Autowired
  25 + private AdminRepository adminRepository;
  26 +
  27 + @Override
  28 + public boolean supportsParameter(MethodParameter parameter) {
  29 + //如果参数类型是Admin并且有CurrentAdmin注解则支持
  30 + if (parameter.getParameterType().isAssignableFrom(Admin.class) &&
  31 + parameter.hasParameterAnnotation(CurrentAdmin.class)) {
  32 + return true;
  33 + }
  34 + return false;
  35 + }
  36 +
  37 + @Override
  38 + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
  39 + //取出鉴权时存入的登录用户Id
  40 + Long currentAdminId = (Long) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST);
  41 + if (currentAdminId != null) {
  42 + //从数据库中查询并返回
  43 + return adminRepository.findOne(currentAdminId);
  44 + }
  45 + throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID);
  46 + }
  47 +}
1 package com.xkl.config; 1 package com.xkl.config;
2 2
3 import com.xkl.authorization.interceptor.AuthorizationInterceptor; 3 import com.xkl.authorization.interceptor.AuthorizationInterceptor;
  4 +import com.xkl.authorization.resolvers.CurrentAdminMethodArgumentResolver;
4 import com.xkl.authorization.resolvers.CurrentUserMethodArgumentResolver; 5 import com.xkl.authorization.resolvers.CurrentUserMethodArgumentResolver;
5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Configuration;
@@ -24,6 +25,8 @@ public class MvcConfig extends WebMvcConfigurerAdapter { @@ -24,6 +25,8 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
24 @Autowired 25 @Autowired
25 private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver; 26 private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;
26 27
  28 + @Autowired
  29 + private CurrentAdminMethodArgumentResolver currentAdminMethodArgumentResolver;
27 @Override 30 @Override
28 public void addInterceptors(InterceptorRegistry registry) { 31 public void addInterceptors(InterceptorRegistry registry) {
29 registry.addInterceptor(authorizationInterceptor); 32 registry.addInterceptor(authorizationInterceptor);
@@ -32,5 +35,6 @@ public class MvcConfig extends WebMvcConfigurerAdapter { @@ -32,5 +35,6 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
32 @Override 35 @Override
33 public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { 36 public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
34 argumentResolvers.add(currentUserMethodArgumentResolver); 37 argumentResolvers.add(currentUserMethodArgumentResolver);
  38 + argumentResolvers.add(currentAdminMethodArgumentResolver);
35 } 39 }
36 } 40 }
  1 +package com.xkl.controller.uploadsoft;
  2 +
  3 +import com.wordnik.swagger.annotations.ApiOperation;
  4 +
  5 +import com.xkl.domain.UpSoftVersion;
  6 +import com.xkl.model.ResultModel;
  7 +
  8 +import com.xkl.repository.UpSoftVersionRepository;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.http.HttpStatus;
  11 +import org.springframework.http.ResponseEntity;
  12 +
  13 +import org.springframework.web.bind.annotation.RequestMapping;
  14 +import org.springframework.web.bind.annotation.RequestMethod;
  15 +import org.springframework.web.bind.annotation.RestController;
  16 +
  17 +import java.util.ArrayList;
  18 +import java.util.List;
  19 +
  20 +/**
  21 + * AMP报告上传软件客户端获取最新软件版本。
  22 + */
  23 +@RestController
  24 +@RequestMapping("/version")
  25 +public class SoftVersionController {
  26 +
  27 + @Autowired
  28 + private UpSoftVersionRepository upSoftVersionRepository;
  29 +
  30 +
  31 + @RequestMapping(method = RequestMethod.GET)
  32 + @ApiOperation(value = "获取最新软件版本信息")
  33 + public ResponseEntity<ResultModel> getVersionInfo() {
  34 + List<UpSoftVersion> versionList = upSoftVersionRepository.findAllVersion();
  35 + UpSoftVersion version = versionList.get(versionList.size() - 1);
  36 + return new ResponseEntity<>(ResultModel.ok(version), HttpStatus.OK);
  37 + }
  38 +
  39 +}
1 -package com.xkl.controller.uploadsoft; 1 +package com.xkl.controller.uploadsoft;
2 2
3 import com.wordnik.swagger.annotations.ApiImplicitParam; 3 import com.wordnik.swagger.annotations.ApiImplicitParam;
4 import com.wordnik.swagger.annotations.ApiImplicitParams; 4 import com.wordnik.swagger.annotations.ApiImplicitParams;
5 import com.wordnik.swagger.annotations.ApiOperation; 5 import com.wordnik.swagger.annotations.ApiOperation;
6 import com.xkl.authorization.annotation.Authorization; 6 import com.xkl.authorization.annotation.Authorization;
  7 +import com.xkl.authorization.annotation.CurrentAdmin;
7 import com.xkl.authorization.annotation.CurrentUser; 8 import com.xkl.authorization.annotation.CurrentUser;
8 import com.xkl.authorization.manager.ITokenManager; 9 import com.xkl.authorization.manager.ITokenManager;
9 import com.xkl.authorization.model.TokenModel; 10 import com.xkl.authorization.model.TokenModel;
@@ -48,14 +49,16 @@ public class UploadSoftwareAccountController { @@ -48,14 +49,16 @@ public class UploadSoftwareAccountController {
48 49
49 AMPMachine ampMachine = ampMachineRepository.findBySecretKey(ampkey.trim()); 50 AMPMachine ampMachine = ampMachineRepository.findBySecretKey(ampkey.trim());
50 if (ampMachine == null ||// 未找到密钥所对应的机器 51 if (ampMachine == null ||// 未找到密钥所对应的机器
51 - !ampMachine.getAMPSerial().equals(ampserial)) { 52 + !ampMachine.getAMPSerial().equals(ampserial) ||//amp序号不符合
  53 + ampMachine.getStatus() != 1) {//用户无效
52 return new ResponseEntity<>(ResultModel.error(ResultStatus.AMP_KEY_ERROR), HttpStatus.NOT_FOUND); 54 return new ResponseEntity<>(ResultModel.error(ResultStatus.AMP_KEY_ERROR), HttpStatus.NOT_FOUND);
53 55
54 } 56 }
55 57
56 Admin admin = adminRepository.findByAccount(account); 58 Admin admin = adminRepository.findByAccount(account);
57 if (admin == null || //未注册 59 if (admin == null || //未注册
58 - !admin.getPwd().equals(password)) { //密码错误 60 + !admin.getPwd().equals(password) ||//密码错误
  61 + admin.getStatus() != 1) {//用户无效
59 //提示用户名或密码错误 62 //提示用户名或密码错误
60 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND); 63 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
61 } 64 }
@@ -67,13 +70,26 @@ public class UploadSoftwareAccountController { @@ -67,13 +70,26 @@ public class UploadSoftwareAccountController {
67 70
68 @RequestMapping(method = RequestMethod.DELETE) 71 @RequestMapping(method = RequestMethod.DELETE)
69 @Authorization 72 @Authorization
70 - @ApiOperation(value = "退出登录") 73 + @ApiOperation(value = "报告上传软件退出登录")
71 @ApiImplicitParams({ 74 @ApiImplicitParams({
72 @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"), 75 @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
73 }) 76 })
74 - public ResponseEntity<ResultModel> logout(@CurrentUser Admin admin) { 77 + public ResponseEntity<ResultModel> logout(@CurrentAdmin Admin admin) {
75 tokenManager.deleteToken(admin.getId()); 78 tokenManager.deleteToken(admin.getId());
76 return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK); 79 return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
77 } 80 }
78 81
  82 + @RequestMapping(value = "/modpwd", method = RequestMethod.PUT)
  83 + @Authorization
  84 + @ApiOperation(value = "报告上传软件修改用户密码")
  85 + @ApiImplicitParams({
  86 + @ApiImplicitParam(name = "authorization", value = "请以如下格式输入登录返回信息:adminId_tokens", required = true, dataType = "string", paramType = "header"),
  87 + })
  88 + public ResponseEntity<ResultModel> modpwd(@CurrentAdmin Admin admin, @RequestParam String newpwd) {
  89 +
  90 + admin = adminRepository.findById(admin.getId());
  91 + admin.setPwd(newpwd);
  92 + adminRepository.save(admin);
  93 + return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
  94 + }
79 } 95 }
@@ -33,9 +33,14 @@ public class Admin { @@ -33,9 +33,14 @@ public class Admin {
33 @Column(name = "coid") 33 @Column(name = "coid")
34 private int coid; 34 private int coid;
35 35
  36 + //备注
  37 + @Column(name = "note")
  38 + private String note;
  39 +
  40 +
36 //状态 41 //状态
37 - @Column(name = "state")  
38 - private int state; 42 + @Column(name = "status")
  43 + private int status;
39 44
40 public long getId() { 45 public long getId() {
41 return id; 46 return id;
@@ -77,11 +82,19 @@ public class Admin { @@ -77,11 +82,19 @@ public class Admin {
77 this.coid = coid; 82 this.coid = coid;
78 } 83 }
79 84
80 - public int getState() {  
81 - return state; 85 + public String getNote() {
  86 + return note;
  87 + }
  88 +
  89 + public void setNote(String note) {
  90 + this.note = note;
  91 + }
  92 +
  93 + public int getStatus() {
  94 + return status;
82 } 95 }
83 96
84 - public void setState(int state) {  
85 - this.state = state; 97 + public void setStatus(int status) {
  98 + this.status = status;
86 } 99 }
87 } 100 }
  1 +package com.xkl.domain;
  2 +
  3 +import javax.persistence.Column;
  4 +import javax.persistence.Entity;
  5 +import javax.persistence.Id;
  6 +import javax.persistence.Table;
  7 +
  8 +/**
  9 + * 用户数据的domain类
  10 + */
  11 +@Entity
  12 +@Table(name = "xkl_upsoft_version")
  13 +public class UpSoftVersion {
  14 + //用户id
  15 + @Id
  16 + @Column(name = "id")
  17 + private long id;
  18 +
  19 + //版本号
  20 + @Column(name = "version_num")
  21 + private int version_num;
  22 +
  23 + //版本名称
  24 + @Column(name = "name")
  25 + private String name;
  26 +
  27 + //版本详情
  28 + @Column(name = "note")
  29 + private String note;
  30 +
  31 + public long getId() {
  32 + return id;
  33 + }
  34 +
  35 + public void setId(long id) {
  36 + this.id = id;
  37 + }
  38 +
  39 + public int getVersion_num() {
  40 + return version_num;
  41 + }
  42 +
  43 + public void setVersion_num(int version_num) {
  44 + this.version_num = version_num;
  45 + }
  46 +
  47 + public String getName() {
  48 + return name;
  49 + }
  50 +
  51 + public void setName(String name) {
  52 + this.name = name;
  53 + }
  54 +
  55 + public String getNote() {
  56 + return note;
  57 + }
  58 +
  59 + public void setNote(String note) {
  60 + this.note = note;
  61 + }
  62 +}
@@ -10,4 +10,6 @@ import org.springframework.data.repository.CrudRepository; @@ -10,4 +10,6 @@ import org.springframework.data.repository.CrudRepository;
10 public interface AdminRepository extends CrudRepository<Admin, Long> { 10 public interface AdminRepository extends CrudRepository<Admin, Long> {
11 11
12 public Admin findByAccount(String account); 12 public Admin findByAccount(String account);
13 - } 13 + public Admin findById(long id);
  14 +
  15 +}
  1 +package com.xkl.repository;
  2 +
  3 +import com.xkl.domain.UpSoftVersion;
  4 +import org.springframework.data.jpa.repository.Query;
  5 +import org.springframework.data.repository.CrudRepository;
  6 +
  7 +import java.util.List;
  8 +
  9 +/**
  10 + * User类的CRUD操作
  11 + *
  12 + * @see UpSoftVersion
  13 + */
  14 +public interface UpSoftVersionRepository extends CrudRepository<UpSoftVersion, Long> {
  15 + @Query("select upsoft from UpSoftVersion upsoft")
  16 + public List<UpSoftVersion> findAllVersion();
  17 +}
@@ -5,13 +5,14 @@ server.port=8090 @@ -5,13 +5,14 @@ server.port=8090
5 #server.ssl.key-password = xkl2016 5 #server.ssl.key-password = xkl2016
6 6
7 #MySQL 7 #MySQL
8 -spring.datasource.url=jdbc:mysql://localhost:3306/hanhe_test?useUnicode=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=round&autoReconnect=true  
9 -spring.datasource.username=root  
10 -spring.datasource.password=fyqmysql 8 +spring.datasource.url=jdbc:mysql://db.hanhezy.com:4096/hanhe_test?useUnicode=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=round&autoReconnect=true
  9 +spring.datasource.username=hanhe
  10 +spring.datasource.password=HANhetest2016
  11 +
11 12
12 #Redis 13 #Redis
13 spring.redis.host=127.0.0.1 14 spring.redis.host=127.0.0.1
14 -spring.redis.password=foobared 15 +#spring.redis.password=foobared
15 #spring.redis.host=r-m5e7cedd3124afd4.redis.rds.aliyuncs.com 16 #spring.redis.host=r-m5e7cedd3124afd4.redis.rds.aliyuncs.com
16 #spring.redis.password=r-m5e7cedd3124afd4:XIkaiLURedis2016 17 #spring.redis.password=r-m5e7cedd3124afd4:XIkaiLURedis2016
17 18
@@ -27,7 +27,7 @@ @@ -27,7 +27,7 @@
27 if (url && url.length > 1) { 27 if (url && url.length > 1) {
28 url = decodeURIComponent(url[1]); 28 url = decodeURIComponent(url[1]);
29 } else { 29 } else {
30 - url = "http://localhost:8090/api-docs"; 30 + url = "http://127.0.0.1:8090/api-docs";
31 } 31 }
32 window.swaggerUi = new SwaggerUi({ 32 window.swaggerUi = new SwaggerUi({
33 url: url, 33 url: url,