dev
hgx 1 year ago
parent b2496067a7
commit 95740e2349

@ -3,10 +3,12 @@ package com.mashibing.apipassenger.Service;
import com.mashibing.apipassenger.remote.ServicePassengerUserClient; import com.mashibing.apipassenger.remote.ServicePassengerUserClient;
import com.mashibing.apipassenger.remote.ServiceVerificationCodeClient; import com.mashibing.apipassenger.remote.ServiceVerificationCodeClient;
import com.mashibing.internal.common.constant.CommonStatusEnum; import com.mashibing.internal.common.constant.CommonStatusEnum;
import com.mashibing.internal.common.constant.IdentityConstant;
import com.mashibing.internal.common.dto.ResponseResult; import com.mashibing.internal.common.dto.ResponseResult;
import com.mashibing.internal.common.request.VerificationCodeDTO; import com.mashibing.internal.common.request.VerificationCodeDTO;
import com.mashibing.internal.common.response.NumberCodeResponse; import com.mashibing.internal.common.response.NumberCodeResponse;
import com.mashibing.internal.common.response.TokenResponse; import com.mashibing.internal.common.response.TokenResponse;
import com.mashibing.internal.common.util.JwtUtils;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -102,12 +104,12 @@ public class VerificationCodeService {
verificationCodeDTO.setPassengerPhone(passengerPhone); verificationCodeDTO.setPassengerPhone(passengerPhone);
servicePassengerUserClient.loginOrRegister(verificationCodeDTO); servicePassengerUserClient.loginOrRegister(verificationCodeDTO);
// 颁发令牌 // 颁发令牌,不应用魔法值,用常量
System.out.println("颁发令牌"); String token = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY);
//响应 //响应
TokenResponse tokenResponse = new TokenResponse(); TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setToken("token value"); tokenResponse.setToken(token);
return ResponseResult.success(tokenResponse); return ResponseResult.success(tokenResponse);
} }
} }

@ -1,5 +1,6 @@
package com.mashibing.apipassenger.controller; package com.mashibing.apipassenger.controller;
import com.mashibing.internal.common.dto.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -9,4 +10,23 @@ public class TestController {
public String test(){ public String test(){
return "test api passenger"; return "test api passenger";
} }
/**
* token
* @return
*/
@GetMapping("/authTest")
public ResponseResult authTest(){
return ResponseResult.success("auth test");
}
/**
* tokentoken访
* @return
*/
@GetMapping("/noAuthTest")
public ResponseResult noAuthTest(){
return ResponseResult.success("no auth test");
}
} }

@ -0,0 +1,16 @@
package com.mashibing.apipassenger.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new JwtInterceptor())
// 拦截的路径
.addPathPatterns("/**")
// 不拦截的路径
.excludePathPatterns("/noAuthTest");
}
}

@ -0,0 +1,47 @@
package com.mashibing.apipassenger.interceptor;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.mashibing.internal.common.dto.ResponseResult;
import com.mashibing.internal.common.util.JwtUtils;
import net.sf.json.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class JwtInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
boolean result = true;
String resultString = "";
String token = request.getHeader("Authorization");
try {
JwtUtils.parseToken(token);
}catch(SignatureVerificationException e){
resultString = "token sign error";
result = false;
}catch(TokenExpiredException e){
resultString = "token time out";
result = false;
}catch(AlgorithmMismatchException e){
resultString = "token AlgorithmMismatchException";
result = false;
}catch(Exception e){
resultString = "token invalid";
result = false;
}
if(!result){
PrintWriter out = response.getWriter();
out.println(JSONObject.fromObject(ResponseResult.fail(resultString)).toString());
}
return result;
}
}

@ -6,7 +6,7 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class test02 { public class Test02 {
@Test @Test
public void test01() { public void test01() {
int[] arr = {421, 240, 115, 532, 305, 430, 124}; int[] arr = {421, 240, 115, 532, 305, 430, 124};

@ -0,0 +1,174 @@
package test;
import org.junit.Test;
public class Test03 {
@Test
public void test01(){
/**
*n
*
*
*
*/
System.out.println(f1(10));
}
public int f1(int n){
if(n == 1){
return 1;
}
return n * f1(n-1);
}
@Test
public void test02(){
/**
* 1-10;
*/
f2(1,10);
}
void f2(int i,int j){
if(i>j){
return;
}
System.out.println(i);
f2(i+1,j);
}
int f3(int[] arr,int begin){
if(begin == arr.length -1){
return arr[begin];
}
return arr[begin]+f3(arr,begin+1);
}
@Test
public void test03(){
int res = f3(new int[]{1,2,3,4,5},0);
System.out.println(res);
}
/**
*
*/
String reverse(String src,int end){
if(end ==0){
return ""+src.charAt(0);
}
return src.charAt(end) + reverse(src,end-1);
}
@Test
public void test04(){
String str = reverse("abcd",3);
System.out.println(str);
}
int f5(int n){
if(n==1 || n==2){
return 1;
}
return f5(n-1)+f5(n-2);
}
@Test
public void test05(){
int result = f5(10);
System.out.println(result);
}
/**
*
*
* 0-
*
*/
void insertSort(int[] arr,int k){
if(k==0){
return;
}
//对前k-1个元素排序
insertSort(arr,k-1);
//把位置k的元素插入到前面的部分
int x= arr[k];
int index = k-1;
while(x<arr[index]){
arr[index+1] = arr[index];
index--;
}
arr[index] =x;
}
/**
*
* 1.
* 2.
*
*
*
*
* 1-NABC
*
* 1.1~N-1ACB
* 2.NAB
* 3.1-N-1CBA
*/
void printHanoiTower(int N,String from,String to,String help){
if(N==1){
System.out.println("move"+N+"from"+from+"to"+to);
return;
}else{
printHanoiTower(N-1,from,help,to);//先把前N-1个盘子挪到辅助空间上去
System.out.println("move"+N+"from"+from+"to"+to);//N可以顺利到达target
printHanoiTower(N-1,help,to,from);//让N-1从辅助空间同到原空间上去
}
}
@Test
public void test06(){
printHanoiTower(3,"A","B","C");
}
/**
*
*
*
*
*
*
*/
int binarySearch(int[] arr,int low,int high,int key){
if(low > high){
return -1;
}
int mid = low + (high -low)>>1; // (low+high)>>1,防止溢出,移位也更高效
int midVal = arr[mid];
if(midVal <key){
return binarySearch(arr,mid+1,high,key);
}else if(midVal > key){
return binarySearch(arr,low,high-1,key);
}else {
return mid;
}
}
@Test
public void test07(){
int search = binarySearch(new int[]{3, 5, 4, 7, 8, 1}, 0, 5, 4);
System.out.println(search);
}
void shellSort(int[] arr){
//不断地缩小增量
for(int interval = arr.length/2;interval>0;interval=interval/2){
//增量为1的插入排序
for(int i=interval;i<arr.length;i=i+interval){
int target = arr[i];
int j = i-interval;
while(j>-1 && target < arr[j]){
arr[j+interval] =arr[j];
j-=interval;
}
arr[j+interval] =target;
}
}
}
}

@ -0,0 +1,268 @@
package test;
import org.junit.Test;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;
public class Test04 {
/**
* 1.
* numstarget,
* 使
*/
@Test
public void test01(){
int[] result = new int[2];
int[] str = new int[]{2,7,8,69};
int target =9;
for(int i=0;i<str.length -1;i++){
for(int j=i+1;j< str.length;j++){
if(str[i]+str[j]==target){
result[0] =i;
result[j] =j;
}
}
}
System.out.println(result[0]+ ","+result[1]);
}
int get(int m,int n){
if(n==0){
return m;
}
return get(n,m%n);
}
@Test
public void test02(){
System.out.println(get(8, 2));
}
@Test
public void test03(){
int[] result = new int[2];
int[] str = new int[]{2,7,8,69};
int target =9;
Map<Integer,Integer> map = new Hashtable<>();
for(int i=0;i<str.length;i++){
map.put(str[i],i);
}
for(int j=0;j<str.length;j++){
int diff = target - str[j];
if(map.containsKey(diff) && map.get(diff) != j){
result[0] =j;
result[1] = map.get(diff);
System.out.println(result[0]+ ","+result[1]);
return;
}
}
System.out.println(result[0]+ ","+result[1]);
}
/**
* n访f(n)
* f(n) = n
* f(n) = n^2 f(n) = 2n^2 + n + 5
* O
* O(g(n)) = { f(n):cn0,使
* n > n0, 0< f(n) <= cg(n)}
* O(n) = 2n^2 + n + 5
* O(g(n)),g(n),f(n)g(n)
*/
/**
* 2.
*
*
* 00
*
*/
void print(int num){
for(int i =31;i>= 0;i--){
System.out.print((num & (1<<i)) == 0 ? "0":"1");
}
System.out.println();
}
@Test
public void test04(){
int num = 4;
print(num);
}
@Test
public void test05(){
System.out.println(f5(10));
}
long f5(int N){
long ans = 0;
for(int i =1;i<= N;i++){
ans += factorial(i);
}
return ans;
}
long factorial(int n){
long sum =1;
for(int i=1;i<= n; i++){
sum *= i;
}
return sum;
}
@Test
public void test06(){
System.out.println(f6(10));
}
long f6(int n){
long ans = 0;
long cur = 1;
for(int i =1;i<= n;i++){
cur = cur * i;
ans += cur;
}
return ans;
}
void printArray(int[] arr){
for(int i =0;i<arr.length;i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
void selectSort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
int n =arr.length;
for(int i=0;i<n;i++){
// 0-n-1
// 1-n-
// 2-n-1
//i-n-1
int minValueIndex =i;
for(int j= i+1;j<n;j++){
minValueIndex = arr[j] <arr[minValueIndex] ? j : minValueIndex;
}
swap(arr,i,minValueIndex);
}
}
void swap(int[] arr,int i,int j){
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
@Test
public void test07(){
int[] arr= {7,1,3,5,1,6,8,1,3,5,7,5,6};
printArray(arr);
selectSort(arr);
printArray(arr);
}
@Test
public void test08(){
int[] arr= {7,1,3,5,1,6,8,1,3,5,7,5,6};
printArray(arr);
bubbleSort(arr);
printArray(arr);
}
void bubbleSort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
// 0-n-1
// 0~n-2
//0~n-3
int n = arr.length;
for(int end = n-1;end >= 0;end--){
// 0~ end 干一陀事情
// 0 1 1 2 2 3 3 4 4 5 end-1 end
for(int second =1;second <= end;second++){
if(arr[second -1]> arr[second]){
swap(arr,second -1,second);
}
}
}
}
@Test
public void test09(){
int[] arr= {7,1,3,5,1,6,8,1,3,5,7,5,6};
printArray(arr);
insertSort(arr);
printArray(arr);
}
void insertSort(int[] arr) {
if(arr == null || arr.length <2){
return;
}
// 0~0完成
//0~1
//0~2
//0~3
//0~n-1
int n = arr.length;
for(int end = 1;end <n;end++){
int newNumIndex = end;
while(newNumIndex -1 >=0 && arr[newNumIndex-1]>arr[newNumIndex]){
swap(arr,newNumIndex -1,newNumIndex);
newNumIndex--;
}
}
}
@Test
public void test10(){
int[] arr= {7,1,3,5,1,6,8,1,3,5,7,5,6};
printArray(arr);
insertSort1(arr);
printArray(arr);
}
void insertSort1(int[] arr) {
if(arr == null || arr.length <2){
return;
}
// 0~0完成
//0~1
//0~2
//0~3
//0~n-1
int n = arr.length;
for(int end = 1;end <n;end++){
//pre新数的前一个位置
for(int pre = end-1;pre>=0 && arr[pre]>arr[pre+1];pre--){
swap(arr,pre,pre+1);
}
}
}
@Test
public void test11(){
new RangeSum(new int[]{4,5,6,7,7,8});
System.out.println(RangeSum.rangeSum(1, 3));
}
}
class RangeSum{
private static int[] preSum;
public RangeSum(int[] array){
int n = array.length;
preSum = new int[n];
preSum[0] = array[0];
for(int i=1;i<n;i++) {
preSum[i] = preSum[i - 1] + array[i];
}
}
static int rangeSum(int L,int R){
return L == 0 ? preSum[R] : preSum[R]-preSum[L-1];
}
}

@ -0,0 +1,8 @@
package com.mashibing.internal.common.constant;
public class IdentityConstant {
//乘客身份
public static final String PASSENGER_IDENTITY = "1";
//司机身份
public static final String DRIVER_IDENTITY = "2";
}

@ -0,0 +1,11 @@
package com.mashibing.internal.common.dto;
import lombok.Data;
@Data
public class TokenResult {
private String phone;
private String identity;
}

@ -3,6 +3,9 @@ package com.mashibing.internal.common.util;
import com.auth0.jwt.JWT; import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator; import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mashibing.internal.common.dto.TokenResult;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -13,8 +16,15 @@ public class JwtUtils {
//盐 //盐
private static final String SIGN = "CPFmsb!@#$$"; private static final String SIGN = "CPFmsb!@#$$";
private static final String JWT_KEY_PHONE = "Phone";
//乘客是1司机是2
private static final String JWT_KEY_IDENTITY = "identity";
//生成token //生成token
public static String generatorToken(Map<String,String> map){ public static String generatorToken(String passengerPhone,String identity){
Map<String,String> map =new HashMap<>();
map.put(JWT_KEY_PHONE,passengerPhone);
map.put(JWT_KEY_IDENTITY,identity);
//token过期时间 //token过期时间
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,1); calendar.add(Calendar.DATE,1);
@ -32,12 +42,23 @@ public class JwtUtils {
} }
//解析token //解析token
public static TokenResult parseToken(String token){
DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
String phone = verify.getClaim(JWT_KEY_PHONE).toString();
String identity = verify.getClaim(JWT_KEY_IDENTITY).toString();
TokenResult tokenResult = new TokenResult();
tokenResult.setPhone(phone);
tokenResult.setIdentity(identity);
return tokenResult;
}
public static void main(String[] args) { public static void main(String[] args) {
Map<String,String> map = new HashMap<>(); String s = generatorToken("13571876296","1");
map.put("name","zhangsan");
map.put("age","18");
String s = generatorToken(map);
System.out.println("生成的token:"+s); System.out.println("生成的token:"+s);
TokenResult tokenResult = parseToken(s);
System.out.println("手机号:"+tokenResult.getPhone());
System.out.println("身份:"+tokenResult.getIdentity());
} }
} }

Loading…
Cancel
Save