kaiyu
2020-09-15 68294b3a156d2c0573c04f1ac43940a55e397269
登陆接口测试
4 files added
65 ■■■■■ changed files
src/main/java/com/moral/common/exception/WebAuthException.java 11 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/common/exceptionHandler/WebAuthExceptionHandler.java 22 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/WebTokenService.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/WebTokenServiceImpl.java 25 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/common/exception/WebAuthException.java
New file
@@ -0,0 +1,11 @@
package com.moral.common.exception;
public class WebAuthException extends RuntimeException {
    public WebAuthException() {
        super();
    }
    public WebAuthException(String message) {
        super(message);
    }
}
src/main/java/com/moral/common/exceptionHandler/WebAuthExceptionHandler.java
New file
@@ -0,0 +1,22 @@
package com.moral.common.exceptionHandler;
import com.moral.common.exception.WebAuthException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class WebAuthExceptionHandler {
    @ExceptionHandler(WebAuthException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handlerWebAuthException(WebAuthException e){
        Map<String,Object> result = new HashMap<>();
        result.put("msg",e.getMessage());
        result.put("accountId", -1);
        return result;
    }
}
src/main/java/com/moral/service/WebTokenService.java
New file
@@ -0,0 +1,7 @@
package com.moral.service;
import com.moral.entity.Account;
public interface WebTokenService {
     String getToken(String id);
}
src/main/java/com/moral/service/impl/WebTokenServiceImpl.java
New file
@@ -0,0 +1,25 @@
package com.moral.service.impl;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.moral.service.WebTokenService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class WebTokenServiceImpl implements WebTokenService {
    public final static String SECRET = "QXPC_Screen_B/S";
    @Override
    public String getToken(String id) {
        String token="";
        token= JWT.create()
                .withIssuer("qxpc")//发布者
                .withIssuedAt(new Date())//生成时间
                .withExpiresAt(DateUtils.addHours(new Date(),1))//有效时间
                .withClaim("aid",id)//存放数据
                .sign(Algorithm.HMAC256(SECRET));
        return token;
    }
}