javaweb不同角色登录到不同页面

EmployeeService层:
package com.comple.service;

import com.comple.entity.Employee;
import jakarta.servlet.http.HttpSession;

public interface EmployeeService {
boolean auth(String username, String password, HttpSession session);
String getRole(String username, String password);

}
EmployeeServiceImpl层:
package com.comple.service;

import com.comple.dao.EmployeeMapper;
import com.comple.entity.Employee;
import com.comple.service.EmployeeService;
import com.comple.utils.MybatisUtil;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;

public class EmployeeServiceImpl implements EmployeeService {
@Override
public boolean auth(String username, String password, HttpSession session) {
try (SqlSession sqlSession = MybatisUtil.getSession()) {
// 获取 EmployeeMapper 实例
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
// 调用实例方法
Employee employee = mapper.getEmployee(username, password);
if (employee == null) return false;
session.setAttribute("employee", employee);
return true;
}
}

@Override
public String getRole(String username, String password) {
    try (SqlSession sqlSession = MybatisUtil.getSession()) {
        // 获取 EmployeeMapper 实例
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        // 调用实例方法
        Employee employee = mapper.getEmployee(username, password);

        // 如果员工存在,返回角色,否则返回 null
        return employee != null ? employee.getRole() : null;
    }
}

}
LoginServlet层:
package com.comple.servlet;
import com.comple.dao.EmployeeMapper;
import com.comple.entity.Employee;
import com.comple.service.EmployeeService;
import com.comple.service.EmployeeServiceImpl;

import com.comple.utils.MybatisUtil;
import com.comple.utils.ThymeleafUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

private EmployeeService service;

@Override
public void init() throws ServletException {
    service = new EmployeeServiceImpl();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Context context = new Context();

    // 如果登录失败,向模板传递一个失败标志
    if (req.getSession().getAttribute("login-failure") != null) {
        context.setVariable("failure", true);
        req.getSession().removeAttribute("login-failure");
    }

    // 如果用户已登录,直接跳转到首页
    if (req.getSession().getAttribute("employee") != null) {
        resp.sendRedirect("indexservlet");
        return;
    }

    ThymeleafUtil.process("login.html", context, resp.getWriter());
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    String remember = req.getParameter("remember-me");

    // 校验用户名和密码
    if (service.auth(username, password, req.getSession())) {
        // 用户验证成功,设置 cookies 如果勾选了 "记住我"
        if (remember != null) {
            Cookie cookieUsername = new Cookie("username", username);
            cookieUsername.setMaxAge(60 * 60 * 24 * 7); // 7 天
            Cookie cookiePassword = new Cookie("password", password);
            cookiePassword.setMaxAge(60 * 60 * 24 * 7); // 7 天
            resp.addCookie(cookieUsername);
            resp.addCookie(cookiePassword);
        }

        // 获取用户角色并重定向到相应页面
        String role = service.getRole(username, password);
        switch (role) {
            case "普通职员":
                resp.sendRedirect("indexservlet");
                break;
            case "部门经理":
                resp.sendRedirect("addservlet");
                break;
            case "总经理":
                resp.sendRedirect("/general-manager-dashboard");
                break;
            case "财务人员":
                resp.sendRedirect("/finance-dashboard");
                break;
            default:
                resp.sendRedirect("/login"); // 如果角色不匹配,重定向回登录页
                break;
        }
    } else {
        // 登录失败,设置登录失败标志
        req.getSession().setAttribute("login-failure", new Object());
        this.doGet(req, resp);
    }
}

}

posted @ 2024-12-17 21:43  雨花阁  阅读(9)  评论(0编辑  收藏  举报