由于我们学校的hustoj版本久远,业务和视图混在一起,可扩展性较差,正好缺一个账号找回功能,然后我就写了一个另外的web项目挂在了自己的服务器上以解决这个问题。因为mysql中的密码是经过几次加密写进去的,不可以直接用数据库修改,然后我就想到了用htmlunit模拟登陆我自己(管理员),模拟使用changepassword功能,同理使用这个功能核对要找回的账号和写的邮箱(邮箱只有自己知道),然后使用JavaMail对账号的邮箱发送随机的验证码对邮箱进行验证,输入对了就可以进行修改。验证码的写法还比较简单,将验证码获得时间写入数据库(只要设置default update就行了,不用代码处理),每次有人核对成功要获得验证码时,检查该账户是否已经获取过验证码了。如果获取过,取出时间,和当前的时间做一个减法,如果时差大于2分钟就判定验证码失效,删掉原来记录重新写入一个新的,没获取过就直接插入信息,或者获取过但是还在2分钟有效期内,就发出提示信息并不允许再获得验证码。同理核对验证码正确性也要做一个验证码的时效检查,验证码一旦使用过就直接删掉。
效果图:
数据库脚本:
DROP DATABASE server ;
CREATE DATABASE IF NOT EXISTS server DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
use server
create table user(
id varchar(20) primary key not null,
code varchar(20) not null,
mail varchar(30) not null,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)engine=innodb default charset=utf8;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这是一条分界线,就在我前几天准备写这篇博客的时候,我惊奇得发现我把项目代码给删了。。然后从服务器端传回来,反编译一下,一堆报错和乱七八糟的注释。。绝望得我只好重新写了这个项目,正好优化一下,这次我用的
是springboot+mybatis+JavaMail+HtmlUnit,项目写好后测试响应时间大概是1s,多的时候是3.5s,其中有3s是模拟管理员登陆花费的时间。。。没办法优化了,只能以后发现oj的加密方法直接对数据库来操作应该会快很多。
ps:由于服务器运行内存只有1g,经常出现卡死状态qaq。
重写效果图:
核心代码:
项目框架:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wust</groupId>
<artifactId>servlet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>servlet</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--数据库相关-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--spring操作数据库jpa 用于将数据存入数据库的类和方法的集-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.33</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- JDBC连接池依赖 -->
<dependency><groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- 添加对 mybatis 的依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 添加对 JDBC 的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主类(要标注各种包的扫描情况):
package com.wust.servlet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@SpringBootApplication
@ComponentScan("controller")
@ComponentScan("entity")
@ComponentScan("service")
@ComponentScan("conf")
@MapperScan("mapper")
@EnableAutoConfiguration
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
SpringBoot的配置文件,,里面写一些数据库配置相关的信息:
spring.thymeleaf.cache=false
# 设置数据库相关
spring.datasource