Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加token验证 #16

Merged
merged 65 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
f537063
feat: add a status code
Macaku Jan 17, 2024
ae18955
EmailIdentifyingException.java
Macaku Jan 17, 2024
490f969
feat: add a definition of exception
Macaku Jan 17, 2024
e35bba2
feat: add a tool about email check
Macaku Jan 17, 2024
37a5c88
feat: add a email check service
Macaku Jan 17, 2024
297ff50
feat: add a Reposiroty class to exchange with Redis
Macaku Jan 17, 2024
549f8e1
feat: definition entity of model html
Macaku Jan 17, 2024
78338d9
feat: add email model html
Macaku Jan 17, 2024
c4e415f
feat: add two api
Macaku Jan 17, 2024
cd062a0
feat: add a default mail against error
Macaku Jan 17, 2024
55affde
feat: add a annotation
Macaku Jan 17, 2024
f0062c1
feat: add a validator
Macaku Jan 17, 2024
5d3e440
feat: add a dependencies
Macaku Jan 17, 2024
c7c77c3
fix: 修复 validate 使用上的一些问题
BanTanger Jan 17, 2024
1a0fdb6
feat: add a definition of exception
Macaku Jan 17, 2024
67b5e9c
feat: add predicate of email pattern
Macaku Jan 17, 2024
d6e7df9
feat: add some status code
Macaku Jan 17, 2024
f71a3b0
feat: close a worthless log property
Macaku Jan 17, 2024
022f027
feat: change some properties
Macaku Jan 17, 2024
6332f8d
Merge branch 'feat-email-validate' of github.com:AchoBeta/AchoBeta-Re…
Macaku Jan 17, 2024
2eef345
fix: adjust some code
Macaku Jan 17, 2024
f880b38
fix: 调整代码位置,更改代码逻辑,还有点待做明天搞
BanTanger Jan 17, 2024
2d5eaed
fix: delete some unrelated code
Macaku Jan 17, 2024
1c0067f
fix: modify the definition of status codes
Macaku Jan 17, 2024
0ebb03a
fix: delete some blank link
Macaku Jan 17, 2024
cf3ad64
fix: adjust the code to separate RedisCache
Macaku Jan 17, 2024
ca49738
fix: delete some sensitive properties
Macaku Jan 17, 2024
2b9e5d7
feat: add some methods
Macaku Jan 17, 2024
632be08
feat: change to a detail name
Macaku Jan 17, 2024
0a155ce
extract email verification service
Macaku Jan 17, 2024
8a19c94
fix: adjust some codes
Macaku Jan 17, 2024
4408398
Merge branch 'feat-email-validate' of github.com:AchoBeta/AchoBeta-Re…
Macaku Jan 17, 2024
19376dc
fix: fix a bug
Macaku Jan 17, 2024
1680de7
feat: add a check opportunities for users
Macaku Jan 17, 2024
76bc9e5
fix: code format and location
BanTanger Jan 18, 2024
287509c
fix: code reformat
BanTanger Jan 18, 2024
e1f1d9b
feat: delete a file
Macaku Jan 18, 2024
0378d43
fix: add timeout
Macaku Jan 18, 2024
b7293cb
Merge branch 'feat-email-validate' of github.com:AchoBeta/AchoBeta-Re…
Macaku Jan 18, 2024
cac8531
feat:add jwt configuration
Jan 18, 2024
2aa6061
feat:change some code
Jan 18, 2024
8b8ccad
feat:add some jwt configuration
Jan 19, 2024
e3dd407
feat:change some code
Jan 19, 2024
e03f792
feat:change some files location
Jan 19, 2024
3572eb1
feat:add the base config of student
Jan 19, 2024
bab6033
feat:change some code
Jan 19, 2024
30439ee
feat:include one error
Jan 19, 2024
88b0a58
feat:add files
Jan 19, 2024
6ee5871
feat:add files#15
Jan 19, 2024
745abba
feat:change some code #15
Jan 20, 2024
ac30f3a
feat:change some code #15
Jan 20, 2024
20b0dc9
feat:change some code #15
Jan 20, 2024
72ea664
fix:change some code #15
Jan 20, 2024
59f8259
fix:change file package #15
Jan 20, 2024
c708214
Merge branch 'feat-cattleyuan-addtoken' of github.com:AchoBeta/AchoBe…
Jan 20, 2024
44a7c31
fix:change some code #15
Jan 20, 2024
5fc6e8a
fix:change some code #15
Jan 20, 2024
cdc6e88
fix:reback the sql file #15
Jan 20, 2024
d15aa15
fix:change the sql file #15
Jan 20, 2024
1777c96
fix:change colume type and a bug #15
Jan 20, 2024
f7318a9
fix:supplement loginInfo with userhelper #15
Jan 20, 2024
b94034b
feat:change some code #15
Jan 20, 2024
6986b71
feat:add a file #15
Jan 20, 2024
5f16550
feat:change some code #15
Jan 22, 2024
3c0a79e
Merge branch 'master' into feat-cattleyuan-addtoken
cattleyuan Jan 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@
<version>1.2.31</version>
</dependency>

<!--jwt-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>


</dependencies>

<build>
Expand Down
File renamed without changes.
29 changes: 8 additions & 21 deletions sql/student.sql
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
/*
Navicat Premium Data Transfer

Source Server : 本机
Source Server Type : MySQL
Source Server Version : 80026
Source Host : localhost:3306
Source Schema : achobeta_recruitment

Target Server Type : MySQL
Target Server Version : 80026
File Encoding : 65001

Date: 17/01/2024 13:44:06
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- 简历状态comment说明
-- 范围:0~14,简历状态{
# - 0-草稿
Expand All @@ -36,6 +16,13 @@ SET FOREIGN_KEY_CHECKS = 0;
# - 12-终试通过(仅当复试为最后一个流程时显示)
# - 13-待处理(反馈异常/或管理员主动设置为该状态)
# - 14-挂起(管理员可以主动设置该状态)}
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
Expand Down Expand Up @@ -68,6 +55,6 @@ CREATE TABLE `student` (
INDEX `idx_class`(`class` ASC) USING BTREE,
INDEX `idx_major`(`major` ASC) USING BTREE,
INDEX `idx_name`(`name` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '学生用户简历表' ROW_FORMAT = Dynamic;
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '学生用户简历表' ROW_FORMAT = DYNAMIC;

SET FOREIGN_KEY_CHECKS = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
* @date 2024/1/11 15:25
*/
@SpringBootApplication
@MapperScan({"com.achobeta.domain.shortlink.mapper"})
@MapperScan({"com.achobeta.domain.*.model.dao.mapper"})
public class AchobetaRecruitmentApplication {

public static void main(String[] args) {
SpringApplication.run(AchobetaRecruitmentApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.achobeta.domain.users.model.dao;
package com.achobeta.common.base;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.achobeta.domain.users.model.dao;
package com.achobeta.common.base;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Getter;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/achobeta/common/constants/LoginType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.achobeta.common.constants;

public enum LoginType {
cattleyuan marked this conversation as resolved.
Show resolved Hide resolved

LOGINBYEMAIL("邮箱登录",1),
LOGINBYPASSWORD("密码登录",2);

private final Integer LoginCode;
private final String LoginTypeName;

LoginType(String loginTypeName, Integer loginCode) {
LoginTypeName = loginTypeName;
LoginCode = loginCode;
}

public String getLoginTypeName() {
return LoginTypeName;
}

public Integer getLoginCode() {
return LoginCode;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/achobeta/common/constants/RoleKinds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.achobeta.common.constants;

public enum RoleKinds {
USER("user",0),
ADMINER("adminer",1)
;
private final String roleName;
private final Integer roleNumber;

public String getRoleName() {
return roleName;
}

public Integer getRoleNumber() {
return roleNumber;
}

RoleKinds(String roleName, Integer roleNum) {
this.roleName = roleName;
this.roleNumber = roleNum;
}

}
33 changes: 33 additions & 0 deletions src/main/java/com/achobeta/config/WebMvcConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.achobeta.config;


import com.achobeta.interpretor.UserInterpretor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


/**
* @author cattleYuan
* @date 2024/1/18
*/
//配置类,注册用户端;拦截器
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

@Autowired
private UserInterpretor userInterpretor;
@Override
protected void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(userInterpretor)
.addPathPatterns("/api/v1/user/**")
.excludePathPatterns("/api/v1/email/**");

}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.achobeta.domain.email.controller;

import com.achobeta.common.SystemJsonResponse;
import com.achobeta.common.constants.LoginType;
import com.achobeta.domain.email.service.EmailService;
import com.achobeta.domain.email.util.IdentifyingCodeValidator;
import com.achobeta.domain.users.jwt.propertities.JwtProperties;
import com.achobeta.domain.users.model.vo.LoginVO;
import com.achobeta.domain.users.service.StudentService;
import com.achobeta.domain.users.service.login.LoginLauncher;
import jakarta.validation.constraints.Email;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,7 +21,7 @@
@Validated
@RequestMapping("/api/v1/email")
public class EmailController {

private final LoginLauncher loginLauncher;
private final EmailService emailService;

/**
Expand All @@ -42,7 +47,10 @@ public SystemJsonResponse checkCode(@RequestParam("email") @Email String email,
emailService.checkIdentifyingCode(email, code);
// 成功
log.info("email:{}, 验证码:{} 验证成功", email, code);
return SystemJsonResponse.SYSTEM_SUCCESS();
/*LoginVO loginVO =studentService.login(email);*/
LoginVO loginVO = loginLauncher.login(LoginType.LOGINBYEMAIL, email);
log.info("token:{},生成token成功",loginVO.getAccessToken());
return SystemJsonResponse.SYSTEM_SUCCESS(loginVO);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.achobeta.domain.shortlink.mapper;
package com.achobeta.domain.shortlink.model.dao.mapper;

import com.achobeta.domain.shortlink.po.ShortLink;
import com.achobeta.domain.shortlink.model.dao.po.ShortLink;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.achobeta.domain.shortlink.po;
package com.achobeta.domain.shortlink.model.dao.po;

import com.achobeta.domain.users.model.dao.BaseIncrIDEntity;
import com.achobeta.common.base.BaseIncrIDEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.achobeta.domain.shortlink.service;

import com.achobeta.domain.shortlink.po.ShortLink;

import com.achobeta.domain.shortlink.model.dao.po.ShortLink;
import com.baomidou.mybatisplus.extension.service.IService;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.achobeta.domain.shortlink.service.impl;

import com.achobeta.common.constants.GlobalServiceStatusCode;
import com.achobeta.domain.shortlink.mapper.ShortLinkMapper;
import com.achobeta.domain.shortlink.po.ShortLink;

import com.achobeta.domain.shortlink.model.dao.mapper.ShortLinkMapper;

import com.achobeta.domain.shortlink.model.dao.po.ShortLink;
import com.achobeta.domain.shortlink.service.ShortLinkService;
import com.achobeta.domain.shortlink.util.ShortLinkUtils;
import com.achobeta.exception.GlobalServiceException;
Expand Down
cattleyuan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.achobeta.domain.users.context;

import com.achobeta.domain.users.model.po.StudentEntity;
import com.achobeta.domain.users.model.po.UserHelper;

/**
* @author cattleYuan
* @date 2024/1/18
*/
public class BaseContext {

public static ThreadLocal<UserHelper> threadLocal = new ThreadLocal<>();

public static void setCurrentUser(UserHelper userHelper ) {
threadLocal.set(userHelper);
}

public static UserHelper getCurrentUser() {
return threadLocal.get();
}

public static void removeCurrentUser() {
threadLocal.remove();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.achobeta.domain.users.jwt.propertities;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author cattleYuan
* @date 2024/1/18
*/
@Component
@ConfigurationProperties(prefix = "ab.jwt")
@Data
public class JwtProperties {
/**
* 用户端用户生成jwt令牌相关配置
*/
private String userSecretKey;
private long userTtl;
private String userTokenName;

}
96 changes: 96 additions & 0 deletions src/main/java/com/achobeta/domain/users/jwt/util/JwtUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.achobeta.domain.users.jwt.util;

import cn.hutool.http.HttpGlobalConfig;
import com.achobeta.common.constants.GlobalServiceStatusCode;
import com.achobeta.domain.users.jwt.propertities.JwtProperties;
import com.achobeta.exception.GlobalServiceException;

import io.jsonwebtoken.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.validation.constraints.NotNull;
import java.util.Base64;
import java.util.Date;
import java.util.Map;

@RequiredArgsConstructor
@Slf4j
public class JwtUtil {
private final JwtProperties jwtProperties;
//过期时间小于该值就刷新token
private static final long REFRESHTIME = 1000 * 60 * 20;
/**
* 生成jwt
* 使用Hs256算法
*
* @param secretKey jwt秘钥
* @param ttlMillis jwt过期时间(毫秒)
* @param claims 设置的信息
* @return
*/

public static String createJWT(@NotNull SecretKey secretKey, @NotNull long ttlMillis, @NotNull Map<String, Object> claims) {
// 指定签名的时候使用的签名算法,也就是header那部分
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
// 生成JWT的时间
long expMillis = System.currentTimeMillis() + ttlMillis;
Date exp = new Date(expMillis);

// 设置jwt的body
JwtBuilder builder = Jwts.builder()
// 如果有私有声明,一定要先设置这个自己创建的私有的声明,这个是给builder的claim赋值,一旦写在标准的声明赋值之后,就是覆盖了那些标准的声明的
.setClaims(claims)
// 设置签名使用的签名算法和签名使用的秘钥
.signWith(signatureAlgorithm, secretKey)
// 设置过期时间
.setExpiration(exp);

return builder.compact();
}

/**
* Token解密
*
* @param secretKey jwt秘钥
* @param token 加密后的token
* @return
*/
public static Claims parseJWT(@NotNull SecretKey secretKey, @NotNull String token) {
Claims claims = null;
try {
// 得到DefaultJwtParser
claims = Jwts.parser()
// 设置签名的秘钥
.setSigningKey(secretKey)
// 设置需要解析的jwt
.parseClaimsJws(token).getBody();
} catch (JwtException e) {
log.error("无效的token->{}",token);
throw new GlobalServiceException("无效的token格式", GlobalServiceStatusCode.USER_ACCOUNT_USE_BY_OTHERS);
}
return claims;
}
public static Date getTokenExperition(@NotNull SecretKey secretKey, @NotNull String token) {
Claims claims=parseJWT(secretKey,token);
//返回该token的过期时间
return claims.getExpiration();
}
// 通过明文密钥生成加密后的秘钥 secretKey
public static SecretKey generalKey(@NotNull String secretKey) {
byte[] encodedKey = Base64.getDecoder().decode(secretKey);
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
log.info("生成的key->{}",key);
return key;
}

//
public static boolean judgeApproachExpiration(@NotNull String token,@NotNull SecretKey secretKey) {
long cur = System.currentTimeMillis();
long exp = getTokenExperition(secretKey,token).getTime();
return (exp - cur) < REFRESHTIME;
}
}
Loading
Loading