多平台统一管理软件接口,如何实现多平台统一管理软件接口
264
2022-12-16
JAVA记住密码功能的实现代码
准备:SSM框架,mysql数据库
用户表 user
实体类:
public class User {
/**
* 主键id
*/
private Integer userId;
/**
* 账号
*/
private String username;
/**
* 密码
*/
private String password;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserMapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from s_user where username=#{username}
mapper层
public interface UserMapper {
User getUserByname(String username);
}
service层
public interface UserService {
User getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);
}
impl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public ResultVO getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
String remember = httpServletRequest.getParameter("remember");
if (username!= null && username!= 0){
User user = userMapper.getUserByname(username);
if (user != null && user.getPassword().equals(password)){
session.setAttribute("user", user);
if (remember != null){
Cookie cookieUser = new Cookie("username", username);
Cookie cookiePass = new Cookie("password", password);
cookieUser.setMaxAge(60 * 60 * 24);
cookiePass.setMaxAge(60 * 60 * 24);
httpServletResponse.addCookie(cookieUser);
httpServletResponse.addCookie(cookiePass);
return ResultVO.success(user);
}
return ResultVO.error(1, "用户名或密码错误");
}
return ResultVO.error(3, "用户名或密码不能为空");
}
}
Controller
@RestController
public class UserController {
@Autowired
private UserService userService;
//登录
@RequestMapping("/getUserByname")
public ResultVO getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
return userService.getUserByname(username, password, session, httpServletRequest, httpServletResponse);
}
}
前端代码(jsP)
html的自己修改下,样式是BootStrap的
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
body{
background-color: #fafafa;
}
.nice{
position: relative;
display: flex;
align-items: center;
justify-content: center;
}http://
.container{
margin-top: 100px;
display: flex;
justify-content: space-between;
border: #b3b7bb 2px solid;
border-radius: 5px;
}
#loginForm{
width: 300px;
background-color: rgba(255,255,255,0.7);
margin-top: 30px;
border: #b3b7bb 1px solid;
border-radius: 5px;
}
<%--验证码--%>
欢迎来到:
教务综合信息服务平台
$(function () {
$(".login").click(function () {
//发送ajax请求
$.ajax({
url:'getUserByname',
type:'post',
data:$("#loginForm").serialize(),
success:function (res) {
console.log(res);
if (res.status === 0){
if(res.data.role === 0) {
location.href = "overview";
}else if (res.data.role === 1){
location.href = "teacher";
}else if (res.data.role === 2){
location.href = "teacher";
}else if (res.data.role === 3){
location.href = "student";
}
} else {
$(".nice").html("
}
}
});
});
});
总结
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~