点击查看详情
| <dependencies> |
| <dependency> |
| <groupId>commons-logging</groupId> |
| <artifactId>commons-logging</artifactId> |
| <version>1.1.3</version> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.shiro</groupId> |
| <artifactId>shiro-core</artifactId> |
| <version>1.3.2</version> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.shiro</groupId> |
| <artifactId>shiro-web</artifactId> |
| <version>1.3.2</version> |
| </dependency> |
| <dependency> |
| <groupId>junit</groupId> |
| <artifactId>junit</artifactId> |
| <version>4.11</version> |
| </dependency> |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>javax.servlet-api</artifactId> |
| <version>3.0.1</version> |
| <scope>provided</scope> |
| </dependency> |
| <dependency> |
| <groupId>jstl</groupId> |
| <artifactId>jstl</artifactId> |
| <version>1.2</version> |
| </dependency> |
| <dependency> |
| <groupId>taglibs</groupId> |
| <artifactId>standard</artifactId> |
| <version>1.1.2</version> |
| </dependency> |
| </dependencies> |
| |
| <build> |
| <plugins> |
| |
| <plugin> |
| <groupId>org.apache.tomcat.maven</groupId> |
| <artifactId>tomcat7-maven-plugin</artifactId> |
| <version>2.2</version> |
| <configuration> |
| <uriEncoding>utf-8</uriEncoding> |
| <port>8080</port> |
| <path>/platform</path> |
| </configuration> |
| </plugin> |
| |
| <plugin> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-compiler-plugin</artifactId> |
| <version>3.1</version> |
| <configuration> |
| <source>8</source> |
| <target>8</target> |
| <showWarnings>true</showWarnings> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
点击查看详情
| <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns="http://java.sun.com/xml/ns/javaee" |
| xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" |
| version="3.0"> |
| <display-name>shiro-day01-07web</display-name> |
| |
| |
| <context-param> |
| <param-name>shiroEnvironmentClass</param-name> |
| <param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value> |
| </context-param> |
| |
| |
| <context-param> |
| <param-name>shiroConfigLocations</param-name> |
| <param-value>classpath:shiro.ini</param-value> |
| </context-param> |
| |
| |
| |
| <listener> |
| <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class> |
| </listener> |
| |
| |
| <filter> |
| <filter-name>shiroFilter</filter-name> |
| <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class> |
| </filter> |
| <filter-mapping> |
| <filter-name>shiroFilter</filter-name> |
| |
| <url-pattern>/*</url-pattern> |
| </filter-mapping> |
| |
| </web-app> |
- Shiro内置了很多默认的过滤器,比如身份验证、授权等相关的。默认过滤器可以参考org.apache.shiro.web.filter.mgt.DefaultFilter中的枚举过滤器
点击查看详情
过滤器 |
过滤器类 |
说明 |
默认 |
authc |
FormAuthenticationFilter |
基于表单的过滤器;如“/**=authc”,如果没有登录会跳到相应的登录页面登录 |
无 |
logout |
LogoutFilter |
退出过滤器,主要属性:redirectUrl:退出成功后重定向的地址,如“/logout=logout” |
/ |
anon |
AnonymousFilter |
匿名过滤器,即不需要登录即可访问;一般用于静态资源过滤;示例“/static/**=anon” |
无 |
过滤器 |
过滤器类 |
说明 |
默认 |
roles |
RolesAuthorizationFilter |
角色授权拦截器,验证用户是否拥有所有角色;主要属性: loginUrl:登录页面地址(/login.jsp);unauthorizedUrl:未授权后重定向的地址;示例“/admin/**=roles[admin]” |
无 |
perms |
PermissionsAuthorizationFilter |
权限授权拦截器,验证用户是否拥有所有权限;属性和roles一样;示例“/user/**=perms["user:create"]” |
无 |
port |
PortFilter |
端口拦截器,主要属性:port(80):可以通过的端口;示例“/test= port[80]”,如果用户访问该页面是非80,将自动将请求端口改为80并重定向到该80端口,其他路径/参数等都一样 |
无 |
rest |
HttpMethodPermissionFilter |
rest风格拦截器,自动根据请求方法构建权限字符串(GET=read, POST=create,PUT=update,DELETE=delete,HEAD=read,TRACE=read,OPTIONS=read, MKCOL=create)构建权限字符串;示例“/users=rest[user]”,会自动拼出“user:read,user:create,user:update,user:delete”权限字符串进行权限匹配(所有都得匹配,isPermittedAll) |
无 |
ssl |
SslFilter |
SSL拦截器,只有请求协议是https才能通过;否则自动跳转会https端口(443);其他和port拦截器一样; |
无 |
点击查看详情
| |
| [main] |
| definitionRealm=com.itheima.shiro.realm.DefinitionRealm |
| securityManager.realms=$definitionRealm |
| |
| logout.redirectUrl=/login.jsp |
| |
| authc.loginUrl = /login.jsp |
| [urls] |
| /login=anon |
| |
| /home= authc |
| |
| /order-list = roles[admin] |
| |
| /order-add = perms["order:add"] |
| |
| /order-del = perms["order:del"] |
| |
| /logout = logout |
点击查看详情
| # 登录和退出登录接口 |
| public interface LoginService { |
| |
| |
| |
| |
| |
| boolean login(UsernamePasswordToken token); |
| |
| |
| |
| void logout(); |
| } |
| |
| # 登录和退出登录实现 |
| public class LoginServiceImpl implements LoginService { |
| @Override |
| public boolean login(UsernamePasswordToken token) { |
| Subject subject = SecurityUtils.getSubject(); |
| try { |
| subject.login(token); |
| }catch (Exception e){ |
| return false; |
| } |
| return subject.isAuthenticated(); |
| } |
| @Override |
| public void logout() { |
| Subject subject = SecurityUtils.getSubject(); |
| subject.logout(); |
| } |
| } |
| |
| # 修改用户权限 |
| public class SecurityServiceImpl implements SecurityService { |
| @Override |
| public Map<String,String> findPasswordByLoginName(String loginName) { |
| return DigestsUtil.entryptPassword("123"); |
| return map; |
| } |
| @Override |
| public List<String> findRoleByloginName(String loginName) { |
| List<String> list = new ArrayList<>(); |
| if ("admin".equals(loginName)){ |
| list.add("admin"); |
| } |
| list.add("dev"); |
| return list; |
| } |
| @Override |
| public List<String> findPermissionByloginName(String loginName) { |
| List<String> list = new ArrayList<>(); |
| if ("jay".equals(loginName)){ |
| list.add("order:list"); |
| list.add("order:add"); |
| list.add("order:del"); |
| } |
| return list; |
| } |
| } |
点击查看详情
| @WebServlet(urlPatterns = "/login") |
| public class LoginServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| doPost(req, resp); |
| } |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| |
| String username = req.getParameter("loginName"); |
| String password = req.getParameter("password"); |
| |
| UsernamePasswordToken token = new UsernamePasswordToken(username, password); |
| LoginService loginService = new LoginServiceImpl(); |
| |
| boolean isLoginSuccess = loginService.login(token); |
| if (!isLoginSuccess) { |
| |
| resp.sendRedirect("login.jsp"); |
| return; |
| } |
| req.getRequestDispatcher("/home").forward(req, resp); |
| } |
| |
| } |
| |
| @WebServlet(urlPatterns = "/home") |
| public class HomeServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| doPost(req, resp); |
| } |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| req.getRequestDispatcher("home.jsp").forward(req, resp); |
| } |
| } |
| |
| @WebServlet(urlPatterns = "/order-add") |
| public class OrderAddServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| doPost(req, resp); |
| } |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| req.getRequestDispatcher("order-add.jsp").forward(req, resp); |
| } |
| |
| } |
| |
| @WebServlet(urlPatterns = "/order-list") |
| public class OrderListServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| doPost(req, resp); |
| } |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| req.getRequestDispatcher("order-list.jsp").forward(req, resp); |
| } |
| } |
| |
| @WebServlet(urlPatterns = "/logout") |
| public class LogoutServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| doPost(req, resp); |
| } |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| LoginService loginService = new LoginServiceImpl(); |
| loginService.logout(); |
| } |
| |
| } |
点击查看详情
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title>Title</title> |
| </head> |
| <body> |
| <form method="post" action="${pageContext.request.contextPath}/login"> |
| <table> |
| <tr> |
| <th>登陆名称</th> |
| <td><input type="text" name="loginName"></td> |
| </tr> |
| <tr> |
| <th>密码</th> |
| <td><input type="password" name="password"></td> |
| </tr> |
| <tr> |
| <td colspan="2"> |
| <input type="submit" value="提交"/> |
| </td> |
| </tr> |
| </table> |
| |
| </form> |
| </body> |
| </html> |
| <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title></title> |
| </head> |
| <body> |
| <h6> |
| <a href="${pageContext.request.contextPath}/logout">退出</a> |
| <a href="${pageContext.request.contextPath}/order-list">列表</a> |
| <a href="${pageContext.request.contextPath}/order-add">添加</a> |
| </h6> |
| </body> |
| </html> |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title>Title</title> |
| </head> |
| <body> |
| 添加页面 |
| </body> |
| </html> |
| |
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <%--导入jstl标签库--%> |
| <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> |
| <html> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title>用户列表jsp页面</title> |
| <style> |
| table {border:1px solid #000000} |
| table th{border:1px solid #000000} |
| table td{border:1px solid #000000} |
| </style> |
| |
| </head> |
| <body> |
| <table cellpadding="0" cellspacing="0" width="80%"> |
| <tr> |
| <th>编号</th> |
| <th>公司名称</th> |
| <th>信息来源</th> |
| <th>所属行业</th> |
| <th>级别</th> |
| <th>联系地址</th> |
| <th>联系电话</th> |
| </tr> |
| <tr> |
| <td>1</td> |
| <td>传智播客</td> |
| <td>网络营销</td> |
| <td>互联网</td> |
| <td>普通客户</td> |
| <td>津安创意园</td> |
| <td>0208888887</td> |
| </tr> |
| <tr> |
| <td>2</td> |
| <td>黑马程序员</td> |
| <td>j2ee</td> |
| <td>互联网</td> |
| <td>VIP客户</td> |
| <td>津安创意园</td> |
| <td>0208888887</td> |
| </tr> |
| <tr> |
| <td>3</td> |
| <td>黑马程序员</td> |
| <td>大数据</td> |
| <td>互联网</td> |
| <td>VIP客户</td> |
| <td>津安创意园</td> |
| <td>0208888887</td> |
| </tr> |
| </table> |
| </body> |
| |
| </html> |
| |
点击查看详情
- 使用admin用户测试,可以查看列表,但没有添加权限


- 使用jay用户测试,拥有添加权限,但不能查看列表


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?