1、requestParam
前端搭配(vue):
后端拿值(url)
2、param
2.5 @PathVariable
3、Md5加密
CommonUtils.MD5(xxxx)
package net.xdclass.online_xdclass.utils;
import java.security.MessageDigest;
/**
* 工具类
*/
public class CommonUtils {
/**
* MD5加密工具类
* @param data
* @return
*/
public static String MD5(String data) {
try {
java.security.MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
} catch (Exception exception) {
}
return null;
}
}
4、jwt
package net.xdclass.online_xdclass.utils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import net.xdclass.online_xdclass.model.entity.User;
import java.util.Date;
/**
* Jwt工具类
* 注意点:
* 1、生成的token, 是可以通过base64进行解密出明文信息
* 2、base64进行解密出明文信息,修改再进行编码,则会解密失败
* 3、无法作废已颁布的token,除非改秘钥
*/
public class JWTUtils {
/**
* 过期时间,一周
*/
private static final long EXPIRE = 60000 * 60 * 24 * 7;
//private static final long EXPIRE = 1;
/**
* 加密秘钥
*/
private static final String SECRET = "xdclass.net168";
/**
* 令牌前缀
*/
private static final String TOKEN_PREFIX = "xdclass";
/**
* subject
*/
private static final String SUBJECT = "xdclass";
/**
* 根据用户信息,生成令牌
* @param user
* @return
*/
public static String geneJsonWebToken(User user){
String token = Jwts.builder().setSubject(SUBJECT)
.claim("head_img",user.getHeadImg())
.claim("id",user.getId())
.claim("name",user.getName())
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
.signWith(SignatureAlgorithm.HS256,SECRET).compact();
token = TOKEN_PREFIX + token;
return token;
}
/**
* 校验token的方法
* @param token
* @return
*/
public static Claims checkJWT(String token){
try{
final Claims claims = Jwts.parser().setSigningKey(SECRET)
.parseClaimsJws(token.replace(TOKEN_PREFIX,"")).getBody();
return claims;
}catch (Exception e){
return null;
}
}
}
5、异常处理类
先自定义了一个异常处理类
package net.xdclass.online_xdclass.exception;
/**
* 小滴课堂
* 自定义异常类
*/
public class XDException extends RuntimeException{
private Integer code;
private String msg;
public XDException(Integer code, String msg){
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
然后异常处理:
package net.xdclass.online_xdclass.exception;
import net.xdclass.online_xdclass.utils.JsonData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 异常处理类
*/
@ControllerAdvice
public class CustomExceptionHandler {
private final static Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
@ResponseBody
public JsonData handle(Exception e){
logger.error("[ 系统异常 ]{}",e.getMessage());
if( e instanceof XDException ){
XDException xdException = (XDException) e;
return JsonData.buildError(xdException.getCode(),xdException.getMsg());
}else {
return JsonData.buildError("全局异常,未知错误");
}
}
}
6、拦截器
https://blog.csdn.net/weixin_45678130/article/details/120779795