Commit 78086f7c by xiaolang850403

项目的结构调整

parent 62a13034
package com.asset.controller;
import com.asset.common.*;
import com.asset.core.jwt.TokenUtil;
import com.asset.domain.Result;
import com.asset.domain.User;
import com.asset.domain.system.Certificate;
import com.asset.domain.system.LoginLog;
import com.asset.enums.CodeEnum;
import com.asset.core.enums.CodeEnum;
import com.asset.service.UserService;
import com.asset.service.system.CertificateService;
import com.asset.service.system.LoginLogService;
......@@ -14,16 +14,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* @author xiaol
......@@ -42,7 +39,7 @@ public class LoginController {
LoginLogService loginLogService;
@RequestMapping("/login")
public Result login(@Validated(value={Login.class}) @RequestBody User user, BindingResult result, HttpSession session, HttpServletRequest request) throws UnsupportedEncodingException {
public Result login(@Validated(value={Login.class}) @RequestBody User user, BindingResult result, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, ParseException {
if (result.hasErrors()) {
return ResultUtil.Validated(result);
}
......@@ -68,7 +65,16 @@ public class LoginController {
loginLog.setIp(Common.getIpAddress(request));
loginLog.setLogin_create_at(Common.getDate());
loginLogService.insertLoginLog(loginLog);
return ResultUtil.success(CodeEnum.SUCCESS);
String token = TokenUtil.sign(mobile);
System.out.println("-------:" + mobile);
//登录后写入新的token入库
User user1 = new User();
user1.setLoginTime(Common.getDate());
user1.setToken(token);
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("id",admin1.getId());
userService.update(user1,userQueryWrapper);
return ResultUtil.success(CodeEnum.SUCCESS,token);
}
@GetMapping("/loginInfo")
......@@ -93,4 +99,21 @@ public class LoginController {
return ResultUtil.success(CodeEnum.SUCCESS);
}
@GetMapping("/newToken")
public Result newToken(HttpServletRequest request) {
//获取旧的token,并销毁
String token = request.getHeader("token");
if(token!=null){
Object mobile = request.getSession().getAttribute("mobile");;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("mobile",mobile);
User user = userService.getOne(queryWrapper);
if(user.getToken().equals(token)){ //比较token是否相等
String newtoken = TokenUtil.sign(mobile.toString());
return ResultUtil.success(CodeEnum.SUCCESS,newtoken);
}
}
return ResultUtil.error(CodeEnum.ERROR);
}
}
package com.asset.controller;
import com.aliyun.oss.OSS;
import com.asset.common.*;
import com.asset.domain.Result;
import com.asset.domain.User;
import com.asset.domain.system.Captcha;
import com.asset.domain.system.Certificate;
import com.asset.domain.system.Code;
import com.asset.domain.system.LoginLog;
import com.asset.enums.CodeEnum;
import com.asset.core.enums.CodeEnum;
import com.asset.service.UserService;
import com.asset.service.system.CaptchaService;
import com.asset.service.system.CertificateService;
......@@ -16,16 +14,13 @@ import com.asset.service.system.CodeService;
import com.asset.service.system.LoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.ParseException;
import java.util.*;
......@@ -53,6 +48,9 @@ public class UserController {
@Autowired
CodeService codeService;
@Value("${refexpire}")
private String refexpire;
@RequestMapping("/register")
public Result register(@Validated(value={Insert.class}) @RequestBody User user, BindingResult result) throws UnsupportedEncodingException {
if (result.hasErrors()) {
......@@ -61,6 +59,7 @@ public class UserController {
user.setCreateAt(Common.getDate());
String pass = MD5Util.getEncryption(user.getPassword());
user.setPassword(pass);
user.setRefexpire(refexpire);
userService.save(user);
if(user.getId() != null){
//组合写入凭证表的数据
......
package com.asset.filter;
package com.asset.core.enums.filter;
import org.springframework.stereotype.Component;
......
package com.asset.core.jwt;
import com.asset.core.filter.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class JWTConfiguration implements WebMvcConfigurer {
private TokenInterceptor tokenInterceptor;
//构造方法
public JWTConfiguration(TokenInterceptor tokenInterceptor){
this.tokenInterceptor = tokenInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry){
System.out.println("*****");
List<String> excludePath = new ArrayList<>();
//对以下资源请求不进行拦截和验证
excludePath.add("/login"); //登录
registry.addInterceptor(tokenInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(excludePath);
WebMvcConfigurer.super.addInterceptors(registry);
}
}
package com.asset.core.jwt;
import java.util.Date;
import com.asset.domain.User;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
public class TokenUtil {
//过期时间
private static final long EXPIRE_TIME = 1 * 60 * 1000;
//使用静态的字符密文或者key来获取算法器,token秘钥,请勿泄露,请勿随便修改
private static final String TOKEN_SECRET = "expert"; //密钥盐
/**
* 签名生成
* @param mobile
* @return
*/
public static String sign(String mobile) {
String token = null;
try {
Date expiresAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
token = JWT.create()
.withIssuer("auth0")
.withClaim("mobile", mobile)
.withExpiresAt(expiresAt)
// 使用了HMAC256加密算法。
.sign(Algorithm.HMAC256(TOKEN_SECRET));
System.out.println(token);
} catch (Exception e) {
e.printStackTrace();
}
return token;
}
/**
* 签名验证
* @param token
* @return
*/
public static boolean verify(String token) {
try {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
DecodedJWT jwt = verifier.verify(token);
System.out.println("认证通过:");
System.out.println("mobile:" + jwt.getClaim("mobile").asString());
System.out.println("过期时间:" + jwt.getExpiresAt());
return true;
} catch (Exception e) {
return false;
}
}
}
package com.asset.utils;
package com.asset.core.utils;
import com.asset.common.ResultUtil;
import com.asset.domain.Result;
import com.asset.enums.CodeEnum;
import com.asset.core.enums.CodeEnum;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment