mirror of
https://github.com/RemainderTime/spring-boot-base-demo.git
synced 2026-06-10 03:57:41 +08:00
登录接口功能rsa加密实现
This commit is contained in:
29
src/main/java/cn/xf/basedemo/common/model/LoginInfo.java
Normal file
29
src/main/java/cn/xf/basedemo/common/model/LoginInfo.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package cn.xf.basedemo.common.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: xf-boot-base
|
||||||
|
* @ClassName LoginInfo
|
||||||
|
* @description:
|
||||||
|
* @author: xiongfeng
|
||||||
|
* @create: 2022-07-04 14:26
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
public class LoginInfo {
|
||||||
|
|
||||||
|
private String account;
|
||||||
|
|
||||||
|
private String pwd;
|
||||||
|
|
||||||
|
public String check(){
|
||||||
|
if(StringUtils.isEmpty(account)){
|
||||||
|
return "账号不能为空";
|
||||||
|
}
|
||||||
|
if(StringUtils.isEmpty(pwd)){
|
||||||
|
return "密码不能为空";
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,11 +10,13 @@ import lombok.Data;
|
|||||||
* @create: 2022-06-17 14:54
|
* @create: 2022-06-17 14:54
|
||||||
**/
|
**/
|
||||||
@Data
|
@Data
|
||||||
public class LoginUserInfo {
|
public class LoginUser {
|
||||||
|
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
private String account;
|
||||||
|
|
||||||
private String phone;
|
private String phone;
|
||||||
}
|
}
|
||||||
26
src/main/java/cn/xf/basedemo/common/model/RetObj.java
Normal file
26
src/main/java/cn/xf/basedemo/common/model/RetObj.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package cn.xf.basedemo.common.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: xf-boot-base
|
||||||
|
* @ClassName RetObj
|
||||||
|
* @description: 全局统一响应对象
|
||||||
|
* @author: xiongfeng
|
||||||
|
* @create: 2022-07-04 15:57
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
public class RetObj<T> {
|
||||||
|
|
||||||
|
private Integer code;
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
public static <T> RetObj<T> success() {
|
||||||
|
return new RetObj();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import com.auth0.jwt.interfaces.Claim;
|
|||||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -22,8 +23,8 @@ import java.util.Map;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class JwtTokenUtils {
|
public class JwtTokenUtils {
|
||||||
|
|
||||||
private String tokenSecret;
|
private static String tokenSecret;
|
||||||
private int tokenExpire;
|
private static int tokenExpire;
|
||||||
|
|
||||||
public JwtTokenUtils(String tokenSecret, int tokenExpire) {
|
public JwtTokenUtils(String tokenSecret, int tokenExpire) {
|
||||||
this.tokenSecret = StringUtils.isNotEmpty(tokenSecret) ? tokenSecret : "remaindertime";
|
this.tokenSecret = StringUtils.isNotEmpty(tokenSecret) ? tokenSecret : "remaindertime";
|
||||||
@@ -47,7 +48,7 @@ public class JwtTokenUtils {
|
|||||||
*
|
*
|
||||||
* @param userId 用户id
|
* @param userId 用户id
|
||||||
*/
|
*/
|
||||||
public String createToken(int userId) {
|
public static String createToken(int userId) {
|
||||||
try {
|
try {
|
||||||
Date iatDate = new Date();
|
Date iatDate = new Date();
|
||||||
// expire time
|
// expire time
|
||||||
@@ -82,7 +83,7 @@ public class JwtTokenUtils {
|
|||||||
* @return
|
* @return
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public Map<String, Claim> verifyToken(String token) {
|
public static Map<String, Claim> verifyToken(String token) {
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -105,7 +106,7 @@ public class JwtTokenUtils {
|
|||||||
* @param token
|
* @param token
|
||||||
* @return userId
|
* @return userId
|
||||||
*/
|
*/
|
||||||
public Integer getUserId(String token) {
|
public static Integer getUserId(String token) {
|
||||||
Map<String, Claim> claims = verifyToken(token);
|
Map<String, Claim> claims = verifyToken(token);
|
||||||
if (claims != null) {
|
if (claims != null) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -99,12 +99,13 @@ public class RSAUtils {
|
|||||||
public static String publicEncrypt(String data, RSAPublicKey publicKey){
|
public static String publicEncrypt(String data, RSAPublicKey publicKey){
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Cipher cipher = Cipher.getInstance(RSA);
|
Cipher cipher = Cipher.getInstance(RSA);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
|
|
||||||
// byte[] bytes = cipher.doFinal(data.getBytes());
|
byte[] bytes = cipher.doFinal(data.getBytes());
|
||||||
// return Base64.encodeBase64URLSafeString(bytes);
|
return Base64.encodeBase64URLSafeString(bytes);
|
||||||
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
|
// return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
@@ -115,9 +116,9 @@ public class RSAUtils {
|
|||||||
Cipher cipher = Cipher.getInstance(RSA);
|
Cipher cipher = Cipher.getInstance(RSA);
|
||||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||||
|
|
||||||
// byte[] bytes = cipher.doFinal(Base64.decodeBase64URLSafe(data));
|
// byte[] bytes = cipher.doFinal(Base64.decodeBase64(data.getBytes(CHARSET)));
|
||||||
// return new String(bytes);
|
// return new String(bytes);
|
||||||
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64URLSafe(data), privateKey.getModulus().bitLength()), CHARSET);
|
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data.getBytes(CHARSET)), privateKey.getModulus().bitLength()), CHARSET);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
|
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
|
||||||
}
|
}
|
||||||
@@ -166,16 +167,16 @@ public class RSAUtils {
|
|||||||
//
|
//
|
||||||
// System.out.println();
|
// System.out.println();
|
||||||
//
|
//
|
||||||
// String data = "哈哈哈哈哈哈哈";
|
String data = "data";
|
||||||
//加密
|
//加密
|
||||||
try {
|
try {
|
||||||
// String s = publicEncrypt(data, getPublicKey(publicKey));
|
String s = publicEncrypt(data, getPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC_F5UQC1QWsu3QsESQBz9M-GDA9Atm0qVSvwIsy568lyRLi-nq3VvvnmgrlL4yTbngFzyfb2Dn35cNCHsBvIaGuCY3_PpzPqMzVpxr2QlEkhEX9atnJQ1rWexS8QeZtPjpiIwoQrChTzXjD_sYUkDrqSykFplyivf0NSO2WqCBdwIDAQAB"));
|
||||||
// System.out.println("加密后密文:" + s);
|
System.out.println("加密后密文:" + s);
|
||||||
|
|
||||||
String ss = "bPrP3VQpVNj7jxzSvVRQQpOCzg4c9HAMd/Sesda0SOxmWbNzP8SnhayV2H9Jpih2sf26O8dOqiNE7V1u5NPgQBIPi6LqX2QiFTjynVLxQBUmISfmQ2Q6K3sjHBIRIhuZPrXijw7CextUUQwzh4VvEVkjyaUnqlMXVRkUGlgqP7M=";
|
// String ss = "bPrP3VQpVNj7jxzSvVRQQpOCzg4c9HAMd/Sesda0SOxmWbNzP8SnhayV2H9Jpih2sf26O8dOqiNE7V1u5NPgQBIPi6LqX2QiFTjynVLxQBUmISfmQ2Q6K3sjHBIRIhuZPrXijw7CextUUQwzh4VvEVkjyaUnqlMXVRkUGlgqP7M=";
|
||||||
String privateKey1 = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALiJJ6RPMMh-ETrmppOG7JKINPSFaaZoHjzZkyQl3AcfrpKMmH82j_Pxl4mPvvgKtbR20N-88-nJLT4v4aOz9XYVl5ruE59SsJl_T8YqN-i8L8KH8Wptd0_ee7nDhF4-OGEi-o330daFv20eLpboy6nDkWLmLihKC0jEZWK8MLZzAgMBAAECgYAEhO9gmcPjFRtM6vsnX8WJbSaG2oGU3rXm3Zk56Gd0ETWQRzsw2mA6JC-G4etWXcTHb6V75T-_-PpPrJKFFNItEH-WFRS36xneomycxRG1YTfK1SsGLGF0BV3bLVZx8cQz7VsBY4vqbRCSKtcOZBJpnxI6iHAv07i8w34F6qjfsQJBAORnKUuJQ_GsHHBPT1VhMYjXVepAfTrWtCzRQ648KavbHLAGaRIhX10uj-hAhZLafDqQF8Y7T7GHTlasRL9ubWsCQQDO1R3KScJJSR3KDsnSsF0YCw7V28cr_OVAwiPoro90Me6MUz9yKV88gQlTuJkNFMuu_YdPXYKjlzNVg0zFmtUZAkEAoe9mPtDeZD0TmKkSZUVYul1543C_mPTan5_qrWCoZtkd2MtiuWEB3O4DR7ZfPcQ8KcU5pektUn_NEfRndZYUawJBAJfydOoxeawBLQNODfLcYefR59owlYe5SGpktaCw7O596DPqzId_4Vk_qqx4xueXSXOLCabCmcC4yZue0_2vm7ECQQDLrzXL-BpSqxbvtE0gNKcgaSkEUSOh1QmQFPCHERsOBxcflM6ej71STKglB21JD9m6tM2RySgbtUx4TfOuJTek";
|
// String privateKey1 = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALiJJ6RPMMh-ETrmppOG7JKINPSFaaZoHjzZkyQl3AcfrpKMmH82j_Pxl4mPvvgKtbR20N-88-nJLT4v4aOz9XYVl5ruE59SsJl_T8YqN-i8L8KH8Wptd0_ee7nDhF4-OGEi-o330daFv20eLpboy6nDkWLmLihKC0jEZWK8MLZzAgMBAAECgYAEhO9gmcPjFRtM6vsnX8WJbSaG2oGU3rXm3Zk56Gd0ETWQRzsw2mA6JC-G4etWXcTHb6V75T-_-PpPrJKFFNItEH-WFRS36xneomycxRG1YTfK1SsGLGF0BV3bLVZx8cQz7VsBY4vqbRCSKtcOZBJpnxI6iHAv07i8w34F6qjfsQJBAORnKUuJQ_GsHHBPT1VhMYjXVepAfTrWtCzRQ648KavbHLAGaRIhX10uj-hAhZLafDqQF8Y7T7GHTlasRL9ubWsCQQDO1R3KScJJSR3KDsnSsF0YCw7V28cr_OVAwiPoro90Me6MUz9yKV88gQlTuJkNFMuu_YdPXYKjlzNVg0zFmtUZAkEAoe9mPtDeZD0TmKkSZUVYul1543C_mPTan5_qrWCoZtkd2MtiuWEB3O4DR7ZfPcQ8KcU5pektUn_NEfRndZYUawJBAJfydOoxeawBLQNODfLcYefR59owlYe5SGpktaCw7O596DPqzId_4Vk_qqx4xueXSXOLCabCmcC4yZue0_2vm7ECQQDLrzXL-BpSqxbvtE0gNKcgaSkEUSOh1QmQFPCHERsOBxcflM6ej71STKglB21JD9m6tM2RySgbtUx4TfOuJTek";
|
||||||
String s1 = privateDecryption(ss, getPrivateKey(privateKey1));
|
// String s1 = privateDecryption(ss, getPrivateKey(privateKey1));
|
||||||
System.out.println("解密后明文:" + s1);
|
// System.out.println("解密后明文:" + s1);
|
||||||
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cn.xf.basedemo.controller;
|
package cn.xf.basedemo.controller;
|
||||||
|
|
||||||
|
import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||||
import cn.xf.basedemo.service.UserService;
|
import cn.xf.basedemo.service.UserService;
|
||||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
@@ -27,8 +28,8 @@ public class UserController {
|
|||||||
@ApiOperation(value = "用户登录", notes = "用户登录")
|
@ApiOperation(value = "用户登录", notes = "用户登录")
|
||||||
@ApiOperationSupport(order = 1)
|
@ApiOperationSupport(order = 1)
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public String login(@RequestBody String encryptedData){
|
public String login(@RequestBody LoginInfoRes res){
|
||||||
|
|
||||||
return userService.login(encryptedData);
|
return userService.login(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package cn.xf.basedemo.interceptor;
|
package cn.xf.basedemo.interceptor;
|
||||||
|
|
||||||
import cn.xf.basedemo.common.model.LoginUserInfo;
|
import cn.xf.basedemo.common.model.LoginUser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @program: xf-boot-base
|
* @program: xf-boot-base
|
||||||
@@ -11,7 +11,7 @@ import cn.xf.basedemo.common.model.LoginUserInfo;
|
|||||||
**/
|
**/
|
||||||
public class SessionContext {
|
public class SessionContext {
|
||||||
|
|
||||||
private ThreadLocal<LoginUserInfo> threadLocal;
|
private ThreadLocal<LoginUser> threadLocal;
|
||||||
|
|
||||||
private SessionContext() {
|
private SessionContext() {
|
||||||
this.threadLocal = new ThreadLocal<>();
|
this.threadLocal = new ThreadLocal<>();
|
||||||
@@ -28,11 +28,11 @@ public class SessionContext {
|
|||||||
return Context.INSTANCE;
|
return Context.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(LoginUserInfo user) {
|
public void set(LoginUser user) {
|
||||||
this.threadLocal.set(user);
|
this.threadLocal.set(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoginUserInfo get() {
|
public LoginUser get() {
|
||||||
return this.threadLocal.get();
|
return this.threadLocal.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package cn.xf.basedemo.interceptor;
|
package cn.xf.basedemo.interceptor;
|
||||||
|
|
||||||
import cn.xf.basedemo.common.model.LoginUserInfo;
|
import cn.xf.basedemo.common.model.LoginUser;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
@@ -49,7 +48,7 @@ public class TokenInterceptor implements HandlerInterceptor {
|
|||||||
if(StringUtils.isEmpty(value)){
|
if(StringUtils.isEmpty(value)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LoginUserInfo loginUserInfo = objectMapper.convertValue(value, LoginUserInfo.class);
|
LoginUser loginUserInfo = objectMapper.convertValue(value, LoginUser.class);
|
||||||
if(loginUserInfo == null || loginUserInfo.getId() <= 0){
|
if(loginUserInfo == null || loginUserInfo.getId() <= 0){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/main/java/cn/xf/basedemo/mappers/UserMapper.java
Normal file
17
src/main/java/cn/xf/basedemo/mappers/UserMapper.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package cn.xf.basedemo.mappers;
|
||||||
|
|
||||||
|
import cn.xf.basedemo.model.domain.User;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: xf-boot-base
|
||||||
|
* @ClassName UserMapper
|
||||||
|
* @description:
|
||||||
|
* @author: xiongfeng
|
||||||
|
* @create: 2022-07-04 14:42
|
||||||
|
**/
|
||||||
|
@Mapper
|
||||||
|
public interface UserMapper extends BaseMapper<User> {
|
||||||
|
|
||||||
|
}
|
||||||
30
src/main/java/cn/xf/basedemo/model/domain/User.java
Normal file
30
src/main/java/cn/xf/basedemo/model/domain/User.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package cn.xf.basedemo.model.domain;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: xf-boot-base
|
||||||
|
* @ClassName User
|
||||||
|
* @description:
|
||||||
|
* @author: xiongfeng
|
||||||
|
* @create: 2022-07-04 14:39
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
@TableName(value = "xf-user")
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String account;
|
||||||
|
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
private String phone;
|
||||||
|
|
||||||
|
private Date createTime;
|
||||||
|
}
|
||||||
19
src/main/java/cn/xf/basedemo/model/res/LoginInfoRes.java
Normal file
19
src/main/java/cn/xf/basedemo/model/res/LoginInfoRes.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package cn.xf.basedemo.model.res;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: xf-boot-base
|
||||||
|
* @ClassName LoginInfoRes
|
||||||
|
* @description:
|
||||||
|
* @author: xiongfeng
|
||||||
|
* @create: 2022-07-04 11:46
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
public class LoginInfoRes {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录密文
|
||||||
|
*/
|
||||||
|
private String encryptedData;
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package cn.xf.basedemo.service;
|
package cn.xf.basedemo.service;
|
||||||
|
|
||||||
|
import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @program: xf-boot-base
|
* @program: xf-boot-base
|
||||||
* @ClassName UserService
|
* @ClassName UserService
|
||||||
@@ -9,5 +11,5 @@ package cn.xf.basedemo.service;
|
|||||||
**/
|
**/
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
|
|
||||||
String login(String encryptedData);
|
String login(LoginInfoRes res);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,26 @@
|
|||||||
package cn.xf.basedemo.service.impl;
|
package cn.xf.basedemo.service.impl;
|
||||||
|
|
||||||
|
import cn.xf.basedemo.common.model.LoginInfo;
|
||||||
|
import cn.xf.basedemo.common.model.LoginUser;
|
||||||
|
import cn.xf.basedemo.common.utils.JwtTokenUtils;
|
||||||
import cn.xf.basedemo.common.utils.RSAUtils;
|
import cn.xf.basedemo.common.utils.RSAUtils;
|
||||||
import cn.xf.basedemo.config.GlobalConfig;
|
import cn.xf.basedemo.config.GlobalConfig;
|
||||||
|
import cn.xf.basedemo.mappers.UserMapper;
|
||||||
|
import cn.xf.basedemo.model.domain.User;
|
||||||
|
import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||||
import cn.xf.basedemo.service.UserService;
|
import cn.xf.basedemo.service.UserService;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @program: xf-boot-base
|
* @program: xf-boot-base
|
||||||
* @ClassName UserServiceImpl
|
* @ClassName UserServiceImpl
|
||||||
@@ -23,24 +35,50 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private GlobalConfig globalConfig;
|
private GlobalConfig globalConfig;
|
||||||
|
|
||||||
@Override
|
@Autowired
|
||||||
public String login(String encryptedData) {
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
if(StringUtils.isEmpty(encryptedData)){
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String login(LoginInfoRes res) {
|
||||||
|
|
||||||
|
if (Objects.isNull(res) || StringUtils.isEmpty(res.getEncryptedData())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String loginJson = "";
|
String loginJson = "";
|
||||||
try {
|
try {
|
||||||
loginJson = RSAUtils.privateDecryption(encryptedData, RSAUtils.getPrivateKey(globalConfig.getRsaPrivateKey()));
|
loginJson = RSAUtils.privateDecryption(res.getEncryptedData(), RSAUtils.getPrivateKey(globalConfig.getRsaPrivateKey()));
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
log.error("解密失败------》》》");
|
log.error("解密失败------", e);
|
||||||
}
|
}
|
||||||
log.error("解密数据----" + loginJson);
|
LoginInfo loginInfo = objectMapper.convertValue(loginJson, LoginInfo.class);
|
||||||
|
if (StringUtils.isNotBlank(loginInfo.check())) {
|
||||||
|
return loginInfo.check();
|
||||||
|
}
|
||||||
|
//校验登录账号密码
|
||||||
|
QueryWrapper queryWrapper = new QueryWrapper();
|
||||||
|
queryWrapper.eq("account", loginInfo.getAccount());
|
||||||
|
queryWrapper.eq("password", loginInfo.getPwd());
|
||||||
|
User user = userMapper.selectOne(queryWrapper);
|
||||||
|
if (Objects.isNull(user)) {
|
||||||
|
return "账号或密码错误";
|
||||||
|
}
|
||||||
|
LoginUser loginUser = new LoginUser();
|
||||||
|
loginUser.setId(user.getId());
|
||||||
|
loginUser.setAccount(user.getAccount());
|
||||||
|
loginUser.setName(user.getName());
|
||||||
|
loginUser.setPhone(user.getPhone());
|
||||||
|
|
||||||
|
String token = JwtTokenUtils.createToken(user.getId());
|
||||||
|
|
||||||
|
redisTemplate.opsForValue().set("token:" + token, JSONObject.toJSONString(loginUser), 3600, TimeUnit.SECONDS);
|
||||||
|
redisTemplate.opsForValue().set("user_login_token:" + user.getId(), token, 3600, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
return "登录成功";
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,9 +38,13 @@ spring:
|
|||||||
|
|
||||||
|
|
||||||
global:
|
global:
|
||||||
rsaPublicKey: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4iSekTzDIfhE65qaThuySiDT0hW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||||||
# dynamic:
|
# dynamic:
|
||||||
# datasource:
|
# datasource:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
server:
|
server:
|
||||||
port: 8080
|
port: 8088
|
||||||
shutdown: graceful
|
shutdown: graceful
|
||||||
spring:
|
spring:
|
||||||
profiles:
|
profiles:
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>模拟登录</title>
|
<title>模拟登录</title>
|
||||||
</head>
|
</head>
|
||||||
<style>
|
<style>
|
||||||
el-input {
|
el-input {
|
||||||
width: 200px;
|
width: 200px;
|
||||||
}
|
}
|
||||||
#div1{
|
|
||||||
width: 200px;
|
#div1 {
|
||||||
}
|
width: 200px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
<body>
|
<body>
|
||||||
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
|
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
|
||||||
@@ -25,57 +26,55 @@
|
|||||||
|
|
||||||
|
|
||||||
<div id="app">
|
<div id="app">
|
||||||
<div id="div1">
|
<div id="div1">
|
||||||
<label>账号:</label>
|
<label>账号:</label>
|
||||||
<el-input v-model="account" placeholder="请输入账号"></el-input>
|
<el-input v-model="account" placeholder="请输入账号"></el-input>
|
||||||
<label>密码:</label>
|
<label>密码:</label>
|
||||||
<el-input v-model="pwd" placeholder="请输入密码"></el-input>
|
<el-input v-model="pwd" placeholder="请输入密码"></el-input>
|
||||||
<el-button type="primary" @click = "login()">登录</el-button>
|
<el-button type="primary" @click="login()">登录</el-button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
new Vue({
|
new Vue({
|
||||||
el: '#app',
|
el: '#app',
|
||||||
data:{
|
data: {
|
||||||
rsaPublicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4iSekTzDIfhE65qaThuySiDT0hWmmaB482ZMkJdwHH66SjJh_No_z8ZeJj774CrW0dtDfvPPpyS0-L-Gjs_V2FZea7hOfUrCZf0_GKjfovC_Ch_FqbXdP3nu5w4RePjhhIvqN99HWhb9tHi6W6Mupw5Fi5i4oSgtIxGVivDC2cwIDAQAB',
|
rsaPublicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC_F5UQC1QWsu3QsESQBz9M-GDA9Atm0qVSvwIsy568lyRLi-nq3VvvnmgrlL4yTbngFzyfb2Dn35cNCHsBvIaGuCY3_PpzPqMzVpxr2QlEkhEX9atnJQ1rWexS8QeZtPjpiIwoQrChTzXjD_sYUkDrqSykFplyivf0NSO2WqCBdwIDAQAB',
|
||||||
account: '',
|
account: '',
|
||||||
pwd: ''
|
pwd: ''
|
||||||
},
|
},
|
||||||
methods:{
|
methods: {
|
||||||
login: function () {
|
login: function () {
|
||||||
var data = {
|
var data = {
|
||||||
account: this.account,
|
account: this.account,
|
||||||
pwd: this.pwd
|
pwd: this.pwd
|
||||||
};
|
};
|
||||||
var json = JSON.stringify(data);
|
var json = JSON.stringify(data);
|
||||||
var cipher = this.encryptByPublicKey(json);
|
var cipher = this.encryptByPublicKey(json);
|
||||||
console.log("密文 :" + cipher);
|
console.log("密文 :" + cipher);
|
||||||
|
|
||||||
var url = "http://localhost:8080/user/login";
|
var url = "http://localhost:8088/user/login";
|
||||||
axios.post(url, {
|
axios.post(url, {
|
||||||
encryptedData: cipher,
|
encryptedData: cipher,
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
})
|
})
|
||||||
.catch(function (error) {
|
.then(function (response) {
|
||||||
console.log(error);
|
console.log(response);
|
||||||
});
|
})
|
||||||
},
|
.catch(function (error) {
|
||||||
encryptByPublicKey: function (val = '') {
|
console.log(error);
|
||||||
if(val === ''){
|
});
|
||||||
return '';
|
},
|
||||||
|
encryptByPublicKey: function (val = '') {
|
||||||
|
if (val === '') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
let encryptor = new JSEncrypt() // 新建JSEncrypt对象
|
||||||
|
encryptor.setPublicKey(this.rsaPublicKey) // 设置公钥
|
||||||
|
return encryptor.encrypt(val) // 对需要加密的数据进行加密
|
||||||
|
},
|
||||||
}
|
}
|
||||||
let encryptor = new JSEncrypt() // 新建JSEncrypt对象
|
});
|
||||||
encryptor.setPublicKey(this.rsaPublicKey) // 设置公钥
|
|
||||||
return encryptor.encrypt(val) // 对需要加密的数据进行加密
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user