发表于: 2020-08-03 23:41:21
1 1232
配置拦截器
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/u/*"/>
<!--<mvc:exclude-mapping path=""/>-->
<!--配置拦截器对象-->
<bean "com.jnshu.Interceptor.MyInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
public class MyInterceptor1 implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true
private static final Logger logger = LogManager.getLogger(AccountController.class);
@Autowired
private AccountService accountService;
//转到注册页面
//转到登录页面
//注册账户
@RequestMapping(value = "register",method = RequestMethod.POST)
public String register(Account account,Model model){
account.setCreateat(1L);
account.setCreateby("管理员");
account.setUpdateat(1L);
accountService.insert(account);
model.addAttribute("msg","注册成功");
return "login";
}
//登录
@RequestMapping(value = "login")
public String login(String username, String password, Model model){
Account account = new Account();
account.setUsername(username);
account.setPassword(password);
if(accountService.login(username) != null){
model.addAttribute("username",username);
return "index";
}else {
model.addAttribute("error","账号密码错误");
return "login";
}
}
login
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type="submit" value="登录"/>
</form><a href="register">注册</a>
${error}${msg}
</body>
</html>
register
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="register" method="post">
账户名:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type="submit" value="注册"/>
</form>
</body>
</html>
加密
MD5Util
public class MD5Util {
public final static String stringToMD5(String s){
char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
byte[] btInput = s.getBytes();
MessageDigest mdInst = null;
try {
// 获得MD5摘要算法的 MessageDigest 对象
mdInst = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
}
明日计划 继续任务5 完善功能
评论