【3】编写LoginService
1. package com.itheima.shiro.service; 2. 3. import org.apache.shiro.authc.UsernamePasswordToken; 4. 5. import java.lang.management.LockInfo; 6. 7. /** 8. * @Description:登录服务 9. */ 10. public interface LoginService { 11. 12. /** 13. * @Description 登录方法 14. * @param token 登录对象 15. * @return 16. */ 17. boolean login(UsernamePasswordToken token); 18. 19. /** 20. * @Description 登出方法 21. */ 22. void logout(); 23. }
1. package com.itheima.shiro.service.impl; 2. 3. import com.itheima.shiro.service.LoginService; 4. import org.apache.shiro.SecurityUtils; 5. import org.apache.shiro.authc.UsernamePasswordToken; 6. import org.apache.shiro.subject.Subject; 7. 8. 9. /** 10. * @Description:登录服务 11. */ 12. public class LoginServiceImpl implements LoginService { 13. 14. @Override 15. public boolean login(UsernamePasswordToken token) { 16. Subject subject = SecurityUtils.getSubject(); 17. try { 18. subject.login(token); 19. }catch (Exception e){ 20. return false; 21. } 22. return subject.isAuthenticated(); 23. } 24. 25. @Override 26. public void logout() { 27. Subject subject = SecurityUtils.getSubject(); 28. subject.logout(); 29. } 30. }
【4】编写SecurityServiceImpl
1. package com.itheima.shiro.service.impl; 2. 3. import com.itheima.shiro.service.SecurityService; 4. 5. import java.util.ArrayList; 6. import java.util.HashMap; 7. import java.util.List; 8. import java.util.Map; 9. 10. /** 11. * @Description:权限服务层 12. */ 13. public class SecurityServiceImpl implements SecurityService { 14. 15. @Override 16. public Map<String,String> findPasswordByLoginName(String loginName) { 17. return DigestsUtil.entryptPassword("123"); 18. return map; 19. } 20. 21. @Override 22. public List<String> findRoleByloginName(String loginName) { 23. List<String> list = new ArrayList<>(); 24. if ("admin".equals(loginName)){ 25. list.add("admin"); 26. } 27. list.add("dev"); 28. return list; 29. } 30. 31. @Override 32. public List<String> findPermissionByloginName(String loginName) { 33. List<String> list = new ArrayList<>(); 34. if ("jay".equals(loginName)){ 35. list.add("order:list"); 36. list.add("order:add"); 37. list.add("order:del"); 38. } 39. return list; 40. } 41. }
【5】添加web层内容
【5.1】LoginServlet
1. package com.itheima.shiro.web; 2. 3. import com.itheima.shiro.service.LoginService; 4. import com.itheima.shiro.service.impl.LoginServiceImpl; 5. import org.apache.shiro.authc.UsernamePasswordToken; 6. 7. import javax.servlet.ServletException; 8. import javax.servlet.annotation.WebServlet; 9. import javax.servlet.http.HttpServlet; 10. import javax.servlet.http.HttpServletRequest; 11. import javax.servlet.http.HttpServletResponse; 12. import java.io.IOException; 13. 14. /** 15. * @Description:登录方法 16. */ 17. @WebServlet(urlPatterns = "/login") 18. public class LoginServlet extends HttpServlet { 19. 20. @Override 21. protected void doGet(HttpServletRequest req, HttpServletResponse resp) 22. throws ServletException, IOException { 23. doPost(req, resp); 24. } 25. 26. @Override 27. protected void doPost(HttpServletRequest req, HttpServletResponse resp) 28. throws ServletException, IOException { 29. //获取输入的帐号密码 30. String username = req.getParameter("loginName"); 31. String password = req.getParameter("password"); 32. //封装用户数据,成为Shiro能认识的token标识 33. UsernamePasswordToken token = new UsernamePasswordToken(username, password); 34. LoginService loginService = new LoginServiceImpl(); 35. //将封装用户信息的token进行验证 36. boolean isLoginSuccess = loginService.login(token); 37. if (!isLoginSuccess) { 38. //重定向到未登录成功页面 39. resp.sendRedirect("login.jsp"); 40. return; 41. } 42. req.getRequestDispatcher("/home").forward(req, resp); 43. } 44. 45. }
【5.2】HomeServlet
1. package com.itheima.shiro.web; 2. 3. import javax.servlet.ServletException; 4. import javax.servlet.annotation.WebServlet; 5. import javax.servlet.http.HttpServlet; 6. import javax.servlet.http.HttpServletRequest; 7. import javax.servlet.http.HttpServletResponse; 8. import java.io.IOException; 9. 10. /** 11. * @Description:系统home页面 12. */ 13. @WebServlet(urlPatterns = "/home") 14. public class HomeServlet extends HttpServlet { 15. 16. @Override 17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) 18. throws ServletException, IOException { 19. doPost(req, resp); 20. } 21. 22. @Override 23. protected void doPost(HttpServletRequest req, HttpServletResponse resp) 24. throws ServletException, IOException { 25. req.getRequestDispatcher("home.jsp").forward(req, resp); 26. } 27. }
【5.3】OrderAddServlet
1. package com.itheima.shiro.web; 2. 3. import com.itheima.shiro.service.LoginService; 4. import com.itheima.shiro.service.impl.LoginServiceImpl; 5. import org.apache.shiro.authc.UsernamePasswordToken; 6. 7. import javax.servlet.ServletException; 8. import javax.servlet.annotation.WebServlet; 9. import javax.servlet.http.HttpServlet; 10. import javax.servlet.http.HttpServletRequest; 11. import javax.servlet.http.HttpServletResponse; 12. import java.io.IOException; 13. 14. /** 15. * @Description:添加页码 16. */ 17. @WebServlet(urlPatterns = "/order-add") 18. public class OrderAddServlet extends HttpServlet { 19. 20. @Override 21. protected void doGet(HttpServletRequest req, HttpServletResponse resp) 22. throws ServletException, IOException { 23. doPost(req, resp); 24. } 25. 26. @Override 27. protected void doPost(HttpServletRequest req, HttpServletResponse resp) 28. throws ServletException, IOException { 29. req.getRequestDispatcher("order-add.jsp").forward(req, resp); 30. } 31. 32. }
【5.4】OrderListServlet
1. package com.itheima.shiro.web; 2. 3. import com.itheima.shiro.service.LoginService; 4. import com.itheima.shiro.service.impl.LoginServiceImpl; 5. import org.apache.shiro.authc.UsernamePasswordToken; 6. 7. import javax.servlet.ServletException; 8. import javax.servlet.annotation.WebServlet; 9. import javax.servlet.http.HttpServlet; 10. import javax.servlet.http.HttpServletRequest; 11. import javax.servlet.http.HttpServletResponse; 12. import java.io.IOException; 13. 14. /** 15. * @Description:订单列表 16. */ 17. @WebServlet(urlPatterns = "/order-list") 18. public class OrderListServlet extends HttpServlet { 19. 20. @Override 21. protected void doGet(HttpServletRequest req, HttpServletResponse resp) 22. throws ServletException, IOException { 23. doPost(req, resp); 24. } 25. 26. @Override 27. protected void doPost(HttpServletRequest req, HttpServletResponse resp) 28. throws ServletException, IOException { 29. req.getRequestDispatcher("order-list.jsp").forward(req, resp); 30. } 31. }
【5.5】LogoutServlet
1. package com.itheima.shiro.web; 2. 3. import com.itheima.shiro.service.LoginService; 4. import com.itheima.shiro.service.impl.LoginServiceImpl; 5. import org.apache.shiro.authc.UsernamePasswordToken; 6. 7. import javax.servlet.ServletException; 8. import javax.servlet.annotation.WebServlet; 9. import javax.servlet.http.HttpServlet; 10. import javax.servlet.http.HttpServletRequest; 11. import javax.servlet.http.HttpServletResponse; 12. import java.io.IOException; 13. 14. /** 15. * @Description:登出 16. */ 17. @WebServlet(urlPatterns = "/logout") 18. public class LogoutServlet extends HttpServlet { 19. 20. @Override 21. protected void doGet(HttpServletRequest req, HttpServletResponse resp) 22. throws ServletException, IOException { 23. doPost(req, resp); 24. } 25. 26. @Override 27. protected void doPost(HttpServletRequest req, HttpServletResponse resp) 28. throws ServletException, IOException { 29. LoginService loginService = new LoginServiceImpl(); 30. loginService.logout(); 31. } 32. 33. }
【6】添加JSP
login.jsp登录页面
1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2. <html> 3. <head> 4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5. <title>Title</title> 6. </head> 7. <body> 8. <form method="post" action="${pageContext.request.contextPath}/login"> 9. <table> 10. <tr> 11. <th>登陆名称</th> 12. <td><input type="text" name="loginName"></td> 13. </tr> 14. <tr> 15. <th>密码</th> 16. <td><input type="password" name="password"></td> 17. </tr> 18. <tr> 19. <td colspan="2"> 20. <input type="submit" value="提交"/> 21. </td> 22. </tr> 23. </table> 24. 25. </form> 26. </body> 27. </html>
home.jsp系统页
1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3. <html> 4. <head> 5. <title></title> 6. </head> 7. <body> 8. <h6> 9. <a href="${pageContext.request.contextPath}/logout">退出</a> 10. <a href="${pageContext.request.contextPath}/order-list">列表</a> 11. <a href="${pageContext.request.contextPath}/order-add">添加</a> 12. </h6> 13. </body> 14. </html>
order-add.jsp订单添加(伪代码)
1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2. <html> 3. <head> 4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5. <title>Title</title> 6. </head> 7. <body> 8. 添加页面 9. </body> 10. </html>
order-list.jsp订单列表
1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2. <%--导入jstl标签库--%> 3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4. <html> 5. <head> 6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7. <title>用户列表jsp页面</title> 8. <style> 9. table {border:1px solid #000000} 10. table th{border:1px solid #000000} 11. table td{border:1px solid #000000} 12. </style> 13. 14. </head> 15. <body> 16. <table cellpadding="0" cellspacing="0" width="80%"> 17. <tr> 18. <th>编号</th> 19. <th>公司名称</th> 20. <th>信息来源</th> 21. <th>所属行业</th> 22. <th>级别</th> 23. <th>联系地址</th> 24. <th>联系电话</th> 25. </tr> 26. <tr> 27. <td>1</td> 28. <td>传智播客</td> 29. <td>网络营销</td> 30. <td>互联网</td> 31. <td>普通客户</td> 32. <td>津安创意园</td> 33. <td>0208888887</td> 34. </tr> 35. <tr> 36. <td>2</td> 37. <td>黑马程序员</td> 38. <td>j2ee</td> 39. <td>互联网</td> 40. <td>VIP客户</td> 41. <td>津安创意园</td> 42. <td>0208888887</td> 43. </tr> 44. <tr> 45. <td>3</td> 46. <td>黑马程序员</td> 47. <td>大数据</td> 48. <td>互联网</td> 49. <td>VIP客户</td> 50. <td>津安创意园</td> 51. <td>0208888887</td> 52. </tr> 53. </table> 54. </body> 55. 56. </html>