前言:关于HTTP中,基本涉及到几个问题:
- 我们是如何保存用户状态的呢?
- 我们是如何控制用户状态的呢?
- 我们又是如何保证数据安全传递的呢?
- 我们是如何保证防止被篡改数据的呢?
- 我们又是如何控制多端、异地登录的呢?
基本流程图如下:
1.拦截器
@Configuration
public class LoginConfigrable implements WebMvcConfigurer {
@Autowired
UserService userService;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInteceptor(userService))
.addPathPatterns("/**")
.excludePathPatterns("/","/static/**","/login","/css/**","/js/**","/images/**","/fonts/**","/user/login","/regiest.html","/fnsregiest");
}
}
2.JWT工具类
public class JwtUtils {
/**
* 盐机制
*/
private static final String SIGN = "123H12D*&^HE";
public static final String PAYLOAD_CUSTOM ="custom ";
/**
* 获取token
* @param map 动态传入的键值对
* @return 返回token
*/
public static String getToken(Map<String , String> map){
Calendar instance = Calendar.getInstance();
instance.add(Calendar.MINUTE,60);
//创建JWT
JWTCreator.Builder builder = JWT.create();
//遍历设置键值对
map.forEach((k,v)->{
builder.withClaim(k,v);
});
//设置过期时间
builder.withExpiresAt(instance.getTime());
//设置 HMAC256认证 签名 三部分:标记使用什么算法.jwt存放数据.采用加密后的签名 (可使用Base64.decode解密)
String token = builder.sign(Algorithm.HMAC256(SIGN));
return PAYLOAD_CUSTOM + token;
}
/**
* 验证token
* @param token token令牌
* @return 返回token解码后的对象
*/
public static DecodedJWT verity(String token){
//验证签名是否被篡改
return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
}
}
3.MD5工具类
public class MD5Util {
public static final String MD5_SALT = "";
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
"e", "f" };
private Object salt;
private String algorithm;
public MD5Util(Object salt, String algorithm) {
this.salt = salt;
this.algorithm = algorithm;
}
public String encode(String rawPass) {
String result = null;
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
// 加密后的字符串
result = byteArrayToHexString(md.digest(mergePasswordAndSalt(rawPass).getBytes("utf-8")));
} catch (Exception ex) {
}
return result;
}
public boolean isPasswordValid(String encPass, String rawPass) {
String pass1 = "" + encPass;
String pass2 = encode(rawPass);
return pass1.equals(pass2);
}
private String mergePasswordAndSalt(String password) {
if (password == null) {
password = "";
}
if ((salt == null) || "".equals(salt)) {
return password;
} else {
return password + "{" + salt.toString() + "}";
}
}
/**
* 转换字节数组为16进制字串
*
* @param b 字节数组
* @return 16进制字串
*/
private static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
}
4.时间处理工具类
public class CustomDateUtils {
/**
* 日期连接符号 -
*/
public static final String C_SIGN = "-";
/**
* 日期连接符号 /
*/
public static final String C_SLASH = "/";
/**
* 日期连接符号
*/
public static final String C_NULL = "";
/**
* 周一到周日定义
*/
public static final String[][] DAY_OF_WEEK = {{"MONDAY", "一"}, {"TUESDAY", "二"}, {"WEDNESDAY", "三"}, {"THURSDAY", "四"}, {"FRIDAY", "五"}, {"SATURDAY", "六"}, {"SUNDAY", "日"}};
/**
* 日期解析格式定义
*/
private static DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 获得需要的长度的时间
* @param year 年
* @param month 月
* @param day 日
* @param hour 时
* @param min 分
* @param sec 秒
* @param markIndex 使用日期解析类型
* @param isShowText 是否显示标签
* @return
*/
public static String getCurrentTimeStander(boolean year , boolean month , boolean day , boolean hour , boolean min , boolean sec ,int markIndex,boolean isShowText){
//最终解析成的格式
String tmsReg = "";
// 符号
String sign = C_SIGN;
// 是否添加文字标注
String msgText = "";
if(isShowText){
if(markIndex == 2){
sign = C_SLASH;
}
if(markIndex == 3){
sign = C_NULL;
}
if(year){
msgText = "年";
tmsReg+="yyyy"+msgText;
}
if(month){
msgText = "月";
tmsReg = tmsReg + sign+"MM"+msgText;
}
if(day){
msgText = "日";
tmsReg = tmsReg + sign+"dd"+msgText;
}
if(hour){
msgText = "时";
tmsReg+=" HH"+msgText;
}
if(min){
msgText = "分";
tmsReg+=":mm"+msgText;
}
if(sec){
msgText = "秒";
tmsReg+=":ss"+msgText;
}
}else {
if(markIndex == 2){
sign = C_SLASH;
}
if(markIndex == 3){
sign = C_NULL;
}
if(year){
tmsReg+="yyyy"+msgText;
}
if(month){
tmsReg = tmsReg + sign+"MM";
}
if(day){
tmsReg = tmsReg + sign+"dd";
}
if(hour){
tmsReg+=" HH";
}
if(min){
tmsReg+=":mm";
}
if(sec){
tmsReg+=":ss";
}
}
if(!"".equals(tmsReg)){
SimpleDateFormat sdf = null;
sdf = new SimpleDateFormat(tmsReg);
return sdf.format(System.currentTimeMillis());
}else{
return tmsReg;
}
}
/**
* 得到当前时间
* @return
*/
public static String getCurrentTime(){
return LocalDate.now().toString();
}
/**
* 比较两个时间段的 年月日十分秒的输出
* @param startYear 开始年份
* @param startMonth 开始月份
* @param startDay 开始日期
* @param lastYaer 结束年份
* @param lastMonth 结束月份
* @param lastDay 结束日期
* @return 返回相差年份
*/
public static long diffYearTime(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay){
return YEARS.between(LocalDate.of(startYear, startMonth, startDay), LocalDate.of(lastYaer, lastMonth, lastDay));
}
/**
* 比较两个时间段的 年月日十分秒的输出
* @param startYear 开始年份
* @param startMonth 开始月份
* @param startDay 开始日期
* @param lastYaer 结束年份
* @param lastMonth 结束月份
* @param lastDay 结束日期
* @return 返回相差月份
*/
public static long diffMonthTime(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay){
return MONTHS.between(LocalDate.of(startYear, startMonth, startDay), LocalDate.of(lastYaer, lastMonth, lastDay));
}
/**
* 比较两个时间段的 年月日十分秒的输出
* @param startYear 开始年份
* @param startMonth 开始月份
* @param startDay 开始日期
* @param lastYaer 结束年份
* @param lastMonth 结束月份
* @param lastDay 结束日期
* @return 返回相差天数
*/
public static long diffDaysTime(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay){
return DAYS.between(LocalDate.of(startYear, startMonth, startDay), LocalDate.of(lastYaer, lastMonth, lastDay));
}
/**
* 比较两个时间段的 年月日十分秒的输出
* @param startYear 开始年份
* @param startMonth 开始月份
* @param startDay 开始日期
* @param lastYaer 结束年份
* @param lastMonth 结束月份
* @param lastDay 结束日期
* @return 返回相差小时
*/
public static long diffHoursTime(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay){
return HOURS.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalDateTime.of(lastYaer, lastMonth, lastDay,0,0,0));
}
/**
* 比较两个时间段的 年月日十分秒的输出
* @param startYear 开始年份
* @param startMonth 开始月份
* @param startDay 开始日期
* @param lastYaer 结束年份
* @param lastMonth 结束月份
* @param lastDay 结束日期
* @return 返回相差分钟
*/
public static long diffMinuteTime(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay){
return MINUTES.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalDateTime.of(lastYaer, lastMonth, lastDay,0,0,0));
}
/**
* 比较两个时间段的 年月日十分秒的输出
* @param startYear 开始年份
* @param startMonth 开始月份
* @param startDay 开始日期
* @param lastYaer 结束年份
* @param lastMonth 结束月份
* @param lastDay 结束日期
* @return 返回相差秒数
*/
public static long diffSecTime(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay){
return SECONDS.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalDateTime.of(lastYaer, lastMonth, lastDay,0,0,0));
}
/**
* 比较两个时间段的 年月日十分秒的输出
* @param startYear 开始年份
* @param startMonth 开始月份
* @param startDay 开始日期
* @param lastYaer 结束年份
* @param lastMonth 结束月份
* @param lastDay 结束日期
* @return 返回相差毫秒值
*/
public static long diffMillTime(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay){
return MILLIS.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalDateTime.of(lastYaer, lastMonth, lastDay,0,0,0));
}
/**
* 一段时间到当前的时间
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 指定时间到当前时间日期差
*/
public static long diffDaysToNow(int year , int month , int day){
return DAYS.between(LocalDate.of(year, month, day), LocalDate.now());
}
/**
* 一段时间到当前的时间
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 指定时间到当前时间月份差
*/
public static long diffMonthToNow(int year , int month , int day){
return MONTHS.between(LocalDate.of(year, month, day), LocalDate.now());
}
/**
* 一段时间到当前的时间
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 指定时间到当前时间年差
*/
public static long diffYearToNow(int year , int month , int day){
return YEARS.between(LocalDate.of(year, month, day), LocalDate.now());
}
/**
* 一段时间到当前的时间
* @param startYear 指定年
* @param startMonth 指定月
* @param startDay 指定日
* @return 指定时间到当前时间小时差
*/
public static long diffHoursToNow(int startYear,int startMonth , int startDay){
return HOURS.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalDateTime.now());
}
/**
* 一段时间到当前的时间
* @param startYear 指定年
* @param startMonth 指定月
* @param startDay 指定日
* @return 指定时间到当前时间分钟差
*/
public static long diffMinuteToNow(int startYear,int startMonth , int startDay){
return MINUTES.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalTime.now());
}
/**
* 一段时间到当前的时间
* @param startYear 指定年
* @param startMonth 指定月
* @param startDay 指定日
* @return 指定时间到当前时间秒差
*/
public static long diffSecondToNow(int startYear,int startMonth , int startDay ){
return SECONDS.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalTime.now());
}
/**
* 一段时间到当前的时间
* @param startYear 指定年
* @param startMonth 指定月
* @param startDay 指定日
* @return 指定时间到当前时间毫秒差
*/
public static long diffMillToNow(int startYear,int startMonth , int startDay ){
return MILLIS.between(LocalDateTime.of(startYear, startMonth, startDay,0,0,0), LocalTime.now());
}
/**
* 获得今天是周几
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 周几
*/
public static String getDayOfWeek(int year , int month , int day){
LocalDate days = LocalDate.of(year, month, day);
DayOfWeek dayOfWeek = days.getDayOfWeek();
String strDay = "";
for (int i = 0; i < DAY_OF_WEEK.length; i++) {
if(String.valueOf(dayOfWeek).equals(DAY_OF_WEEK[i][0])){
strDay = DAY_OF_WEEK[i][1];
break;
}
}
return "星期"+strDay;
}
/**
* 取得当前日期以后若干天的日期。如果要得到以前的日期,参数用负数。 例如要得到上星期同一天的日期,参数则为-7
* @param day
* @return 添加后的日期
*/
public static String addDays(Integer day) {
LocalDateTime localTime = LocalDateTime.of(LocalDate.now().plusDays(day), LocalTime.MAX);
return localTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* 得到本月第一天
* @return 返回本月第一天
*/
public static String getDayOfFirst(){
LocalDate today = LocalDate.now();
LocalDate firDay = LocalDate.of(today.getYear(), today.getMonth(), 1);
LocalDateTime firstTime = LocalDateTime.of(firDay,LocalTime.MIN);
return String.valueOf(firstTime);
}
/**
* 得到本月最后一天
* @return 返回本月最后一天
*/
public static String getDayOfLast(){
LocalDate today = LocalDate.now();
LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
LocalDateTime lastTime = LocalDateTime.of(lastDay,LocalTime.MAX);
return String.valueOf(lastTime);
}
/**
* 计算季度输入日期是处于哪个季度
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 当前季度
*/
public static String getSeason(int year , int month , int day){
LocalDate days = LocalDate.of(year, month, day);
Month month1 = days.getMonth().firstMonthOfQuarter();
String session = "";
if("JANUARY".equals(String.valueOf(month1))){
session = "第一季度";
}
if("APRIL".equals(String.valueOf(month1))){
session = "第二季度";
}
if("JULY".equals(String.valueOf(month1))){
session = "第三季度";
}
if("OCTOBER".equals(String.valueOf(month1))){
session = "第四季度";
}
return session;
}
/**
* 获取本季度的第一天
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 获取本季度的第一天
*/
public static String getSeasonStart(int year , int month , int day){
LocalDate days = LocalDate.of(year, month, day);
Month month1 = days.getMonth().firstMonthOfQuarter();
days = LocalDate.of(year, month1, 1);
return LocalDateTime.of(days,LocalTime.MIN).format(sdf);
}
/**
* 获取本季度最后一天
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 获取本季度最后一天
*/
public static String getSeasonLast(int year , int month , int day){
//LocalDate 获取当前日期
LocalDate days = LocalDate.of(year, month, day);
Month firstMonthOfQuarter = days.getMonth().firstMonthOfQuarter();
Month endMonth = Month.of(firstMonthOfQuarter.getValue() + 2);
//获得当前日期
days = LocalDate.of(days.getYear(),endMonth,endMonth.length(days.isLeapYear()));
//时间 使用日期+时间
return LocalDateTime.of(days, LocalTime.MAX).format(sdf);
}
/**
* 获取本周第一天
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 获取本周第一天
*/
public static String getWeekFirstDay(int year , int month , int day){
LocalDate today = LocalDate.of(year,month,day);
TemporalField weekFields = WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek();
LocalDate localDate = LocalDate.from(today);
localDate = localDate.with(weekFields, 1);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()) );
}
/**
* 获取本周最后一天
* @param year 指定年
* @param month 指定月
* @param day 指定日
* @return 获取本周最后一天
*/
public static String getWeekEndDay(int year , int month , int day){
LocalDate today = LocalDate.of(year,month,day);
TemporalField weekFields = WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek();
LocalDate localDate = LocalDate.from(today);
//第7天
localDate = localDate.with(weekFields, 7);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()) );
}
/**
* 计算两个日期相差时间(年或月或天或小时或分钟或秒钟或毫秒)
* @param startYear 开始年
* @param startMonth 开始月
* @param startDay 开始日
* @param lastYaer 终止年
* @param lastMonth 终止月
* @param lastDay 终止日
* @param field 返回年月日时分秒
* @return 按照field返回年月日十分秒
*/
public static long diffTimes(int startYear,int startMonth , int startDay , int lastYaer , int lastMonth , int lastDay , int field){
switch (field){
case Calendar.YEAR:
return diffYearTime(startYear,startMonth,startDay,lastYaer,lastMonth,lastDay);
case Calendar.MONTH:
return diffMonthTime(startYear,startMonth,startDay,lastYaer,lastMonth,lastDay);
case Calendar.DATE:
return diffDaysTime(startYear,startMonth,startDay,lastYaer,lastMonth,lastDay);
case Calendar.HOUR:
return diffHoursTime(startYear,startMonth,startDay,lastYaer,lastMonth,lastDay);
case Calendar.MINUTE:
return diffMinuteTime(startYear,startMonth,startDay,lastYaer,lastMonth,lastDay);
case Calendar.SECOND:
return diffSecTime(startYear,startMonth,startDay,lastYaer,lastMonth,lastDay);
case Calendar.MILLISECOND:
return diffMillTime(startYear,startMonth,startDay,lastYaer,lastMonth,lastDay);
default:break;
}
return 0;
}
/**
* 返回当前时间是多少
* 获取时间差方法
* @return 返回离当前时间有多少
*/
public static String diffTimesToNow(Date currentTime,Date oldTime){
//这样得到的差值是微秒级别
long diff = currentTime.getTime() - oldTime.getTime();
//当前系统时间转Calendar类型
Calendar currentTimes =dataToCalendar(currentTime);
//查询的数据时间转Calendar类型
Calendar firstTimes =dataToCalendar(oldTime);
//获取年
int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);
//获取月
int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
//获取日
int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
//判断如果不足一天,月份需要-1,再加上
if (day < 0) {
month -= 1;
currentTimes.add(Calendar.MONTH, -1);
//获取月份中最大日
day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if (month < 0) {
//获取月 ,如果月份相同,则年份应当 - 1 默认是一年的处理
month = (month + 12) % 12;
year--;
}
long days = diff / (1000 * 60 * 60 * 24);
//获取时
long hours = (diff - days*(1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
//获取分钟
long minutes = (diff - days*(1000 * 60 * 60 * 24) - hours*(1000 * 60 * 60))/(1000* 60);
//获取秒
long s=(diff / 1000 - days*24*60*60 - hours*60*60 - minutes*60);
String calcTime=""+year+" 年"+month+" 月"+day+" 天"+hours+" 小时"+minutes+" 分"+s+" 秒";
return calcTime;
}
/**
*
* @param date 日期
* @return 转换成Calendar类
*/
public static Calendar dataToCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
}
5.拦截器基本操作
@Slf4j
public class LoginInteceptor implements HandlerInterceptor {
@Autowired
public LoginService loginService;
public UserService userService;
public LoginInteceptor(UserService userService) {
this.userService = userService;
}
/**
* 核心认证部分
* @param request 请求域
* @param response 响应域
* @param handler 处理域
* @return 是否放行
* @throws Exception 异常处理
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestUri = request.getRequestURI();
log.info("拦截的路径为:{}",requestUri);
Map<String ,String > map = new HashMap<>();
//token 附带在请求头中的验证令牌
String token = (String) request.getSession().getAttribute("token");
//哪端登录+用户名
String u_name = (String) request.getSession().getAttribute("u_name");
log.info("session的信息:{}",token);
log.info("目前登录的状态信息:{}",u_name);
String errMsg = "";
String realToken = "";
//我们自己约定了请求头的前半部分为Custom 我们自己定义的规则
try {
if(token == null){
errMsg = "您还未登录,请先登录!";
throw new RuntimeException(errMsg);
}
if(!token.startsWith("custom ")){
errMsg = "非法仿制token登录!!";
throw new RuntimeException(errMsg);
}
if(!userService.getToken(u_name).equals(token)){
errMsg = "您已经在另一处登录,请重新登录!";
throw new RuntimeException(errMsg);
}
realToken = token.substring(7);
//验证token是否正确
JwtUtils.verity(realToken);
return true;
}catch (Exception e){
log.error("错误信息是:{}",e.toString());
request.setAttribute("msg",errMsg);
request.getRequestDispatcher("/").forward(request,response);
}
return false;
}
}
6.controller层
/**
* 用户登录处理的controller
* * @author yjh
*/
@Slf4j
@Controller
public class UserLoginController {
@Resource
LoginService loginService;
@Resource
UserService userService;
/**
* 转发到时登录视图
* @return 登录视图
*/
@GetMapping(value = {"/","/login"})
public String toUserLogin(){
return "login";
}
/**
* 正常是返回json数据
* @param username 用户名
* @param password 密码
* @param model 转发器
* @return 处理登录请求
*/
@PostMapping("/user/login")
public String userLogin(@RequestParam("username") String username
, @RequestParam("password") String password
, Model model , HttpServletRequest request ,
@RequestHeader("User-Agent") String userAgent ) throws Exception {
//登录成功重定向 失败则返回登录界面
Map<String , Object > map = new HashMap<>();
//检测登录
User loginUser = loginService.checkLogin(username, password);
if(loginUser != null){
log.info("登录成功!!");
//使用JWT认证
Map<String , String > payload = new HashMap<>();
payload.put("name",username);
//token中式不宜存放用户敏感信息的。
payload.put("nickname",username);
payload.put("role","admin");
payload.put("regtime",loginUser.getReg_time().toString());
payload.put("rantime",System.currentTimeMillis()+"");
//单次登录,生成单次的token,存储token
String token = JwtUtils.getToken(payload);
/*
1.获取来自哪端的请求 userAgent
2.更新数据库信息
3.跳转到成功界面
*/
log.error("请求头信息为:{}",userAgent);
String ua = "";
if(userAgent.indexOf("android") != -1 || userAgent.indexOf("Android") != -1){
log.info("Android 设备请求");
ua = "1#"+username;
}else if(userAgent.indexOf("mac") != -1 || userAgent.indexOf("os") != -1 || userAgent.indexOf("Mac") != -1 || userAgent.indexOf("Os") != -1){
log.info("iphone ipad 请求");
ua = "2#"+username;
}else{
log.info("Window phone请求");
ua = "3#"+username;
}
//登记信息 在登记之前,修改token值并且传递过去
userService.jugeInfo(ua,token);
//存一下dbbase的token
request.getSession().setAttribute("token",token);
//哪端登录状态存储
request.getSession().setAttribute("u_name",ua);
return "redirect:/success.html";
}
log.error("登录失败!!");
model.addAttribute("msg","用户名或密码错误");
return "login";
}
/**
* 注册系统
*/
@GetMapping("/regiest.html")
public String toRegiest(){
return "regiest";
}
/**
* 注册用户
* 纯web 开发应当是返回User json数据
*/
@PostMapping("/fnsregiest")
public String regiestUser(@RequestParam("username") String username , @RequestParam("password") String password,Model model ){
User regiest = loginService.regiest(username, password);
if(regiest!=null){
model.addAttribute("msg","注册成功!");
}else{
//重复注册不跳转,继续保持注册
model.addAttribute("msg","重复注册!");
return "/regiest.html";
}
return "redirect:/login";
}
/**
* @return 跳转到成功界面
*/
@GetMapping("/success.html")
public String toSuccess(HttpServletRequest request , Model model) throws ParseException {
String token = (String) request.getSession().getAttribute("token");
String realToken = "";
//解析jwt对象
DecodedJWT user = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
realToken = token.substring(7);
try {
user = JwtUtils.verity(realToken);
model.addAttribute("name",user.getClaim("name").asString());
model.addAttribute("nickname",user.getClaim("nickname").asString());
model.addAttribute("role",user.getClaim("role").asString());
String regtime = user.getClaim("regtime").asString();
Date parse = sdf.parse(regtime);
String toCurrentTime = CustomDateUtils.diffTimesToNow(new Date(System.currentTimeMillis()), parse);
model.addAttribute("regtime",toCurrentTime);
}catch (Exception e){
}
//这里直接被拦截
return "success";
}
@GetMapping("/user/test")
@ResponseBody
public String testToken(){
return "当前端认证通过,并且可以访问~";
}
}
7.配置层
@Configuration
public class LoginConfigrable implements WebMvcConfigurer {
@Autowired
UserService userService;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInteceptor(userService))
.addPathPatterns("/**")
.excludePathPatterns("/","/static/**","/login","/css/**","/js/**","/images/**","/fonts/**","/user/login","/regiest.html","/fnsregiest");
}
}