Authored by zhaoyue

add upsoft login inter

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