【Shiro】第四章 Web项目集成Shiro(二)

简介: 【Shiro】第四章 Web项目集成Shiro(二)

【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>
相关文章
|
12天前
|
安全 Java API
Java Web 在线商城项目最新技术实操指南帮助开发者高效完成商城项目开发
本项目基于Spring Boot 3.2与Vue 3构建现代化在线商城,涵盖技术选型、核心功能实现、安全控制与容器化部署,助开发者掌握最新Java Web全栈开发实践。
152 1
|
2月前
|
安全 Java 数据库
第16课:Spring Boot中集成 Shiro
第16课:Spring Boot中集成 Shiro
557 0
|
1月前
|
JavaScript Java 微服务
现代化 Java Web 在线商城项目技术方案与实战开发流程及核心功能实现详解
本项目基于Spring Boot 3与Vue 3构建现代化在线商城系统,采用微服务架构,整合Spring Cloud、Redis、MySQL等技术,涵盖用户认证、商品管理、购物车功能,并支持Docker容器化部署与Kubernetes编排。提供完整CI/CD流程,助力高效开发与扩展。
315 63
|
2月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
348 2
|
2月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
156 2
|
2月前
|
物联网 Linux 开发者
快速部署自己私有MQTT-Broker-下载安装到运行不到一分钟,快速简单且易于集成到自己项目中
本文给物联网开发的朋友推荐的是GMQT,让物联网开发者快速拥有合适自己的MQTT-Broker,本文从下载程序到安装部署手把手教大家安装用上私有化MQTT服务器。
865 5
|
2月前
|
安全 JavaScript Java
java Web 项目完整案例实操指南包含从搭建到部署的详细步骤及热门长尾关键词解析的实操指南
本项目为一个完整的JavaWeb应用案例,采用Spring Boot 3、Vue 3、MySQL、Redis等最新技术栈,涵盖前后端分离架构设计、RESTful API开发、JWT安全认证、Docker容器化部署等内容,适合掌握企业级Web项目全流程开发与部署。
149 0
|
11月前
|
Java Maven Docker
gitlab-ci 集成 k3s 部署spring boot 应用
gitlab-ci 集成 k3s 部署spring boot 应用
|
10月前
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
316 0
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
959 6