package com.xxxx.crm.exceptions;
public class NoLoginException extends RuntimeException {
private Integer code=300;
private String msg="用户未登录!";
public NoLoginException() {
super("用户未登录!");
}
public NoLoginException(String msg) {
super(msg);
this.msg = msg;
}
public NoLoginException(Integer code) {
super("用户未登录!");
this.code = code;
}
public NoLoginException(Integer code, String msg) {
super(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 com.xxxx.crm.exceptions;
public class ParamsException extends RuntimeException {
private Integer code=300;
private String msg="参数异常!";
public ParamsException() {
super("参数异常!");
}
public ParamsException(String msg) {
super(msg);
this.msg = msg;
}
public ParamsException(Integer code) {
super("参数异常!");
this.code = code;
}
public ParamsException(Integer code, String msg) {
super(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 com.xxxx.crm.utils;
import com.xxxx.crm.exceptions.ParamsException;
public class AssertUtil {
public static void isTrue(Boolean flag,String msg){
if(flag){
throw new ParamsException(msg);
}
}
}
package com.xxxx.crm.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
public class CookieUtil {
public static void setCookie(String key, String value, String domain,
HttpServletResponse response) {
try {
value = URLEncoder.encode(value, "UTF-8");
if (StringUtils.isNotBlank(value)) {
value = value.replaceAll("\\+", "%20");
}
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(-1);
cookie.setPath("/");
cookie.setDomain(domain);
response.addCookie(cookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static void setCookieNoEncode(String key, String value, String domain,
HttpServletResponse response) {
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(-1);
cookie.setPath("/");
cookie.setDomain(domain);
response.addCookie(cookie);
}
public static String getCookieValue(HttpServletRequest request, String key) {
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(key)) {
cookie = cookies[i];
}
}
}
if (cookie != null) {
try {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return null;
}
public static void deleteCookie(String cookieName, HttpServletRequest request,
HttpServletResponse response) {
Cookie[] arr_cookie = request.getCookies();
if (arr_cookie != null && arr_cookie.length > 0) {
for (Cookie cookie : arr_cookie) {
if (cookie.getName().equals(cookieName)) {
cookie.setValue("");
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
}
}
}
}
}
package com.xxxx.crm.utils;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
public class LoginUserUtil {
public static int releaseUserIdFromCookie(HttpServletRequest request) {
String userIdString = CookieUtil.getCookieValue(request, "userIdStr");
if (StringUtils.isBlank(userIdString)) {
return 0;
}
Integer userId = UserIDBase64.decoderUserID(userIdString);
return userId;
}
}
package com.xxxx.crm.utils;
import java.security.MessageDigest;
import java.util.Base64;
public class Md5Util {
public static String encode(String msg){
try {
MessageDigest messageDigest=MessageDigest.getInstance("md5");
return Base64.getEncoder().encodeToString(messageDigest.digest(msg.getBytes())) ;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.println(encode("123456"));
}
}
package com.xxxx.crm.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PhoneUtil {
public static boolean isMobile(String phone){
Pattern p = null;
Matcher m = null;
boolean b = false;
String s2="^[1](([3|5|8][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$";
if(StringUtils.isNotBlank(phone)){
p = Pattern.compile(s2);
m = p.matcher(phone);
b = m.matches();
}
return b;
}
public static void main(String[] args) {
System.out.println(isMobile("19699999999"));
}
}
package com.xxxx.crm.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.Base64;
public class UserIDBase64 {
public static Integer decoderUserID(String encodedUserID) {
if (StringUtils.isBlank(encodedUserID)) {
return null;
}
try {
String reversedString = new StringBuffer(encodedUserID).reverse().toString();
String base64String = reversedString.replaceAll("#", "=");
int userIDPos = base64String.indexOf("==") + 6;
String realBase64UserID = base64String.substring(userIDPos);
String base64Encoded = new String(Base64.getDecoder().decode(realBase64UserID.getBytes()));
return Integer.parseInt(base64Encoded);
} catch (Exception e) {
return null;
}
}
public static String encoderUserID(Integer userID){
String base64UserIDEncoded = Base64.getEncoder().encodeToString((userID + "").getBytes());
String currentStringBase64Encoded = Base64.getEncoder().encodeToString((System.currentTimeMillis() + "").getBytes());
String keyString = currentStringBase64Encoded
+ currentStringBase64Encoded.substring(4, 8) + base64UserIDEncoded;
byte[] codeBytes = keyString.getBytes();
byte[] ordedBytes = new byte[codeBytes.length];
for(int i=0; i<codeBytes.length; i++){
ordedBytes[i] = codeBytes[codeBytes.length-i-1];
}
return new String(ordedBytes).replaceAll("=", "#");
}
public static void main(String[] args) {
System.out.println(encoderUserID(20));
System.out.println(decoderUserID("#AjMzgjM##QN1AjN4gTOzgjM3UTM"));
}
}