日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shiro学习(10):servelet实现权限认证一

發(fā)布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shiro学习(10):servelet实现权限认证一 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

工具idea

先看看數(shù)據(jù)庫

shiro_role_permission

數(shù)據(jù)

shiro_user

shiro_user_role

數(shù)據(jù)

在pom.xml里面添加

<dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.2.3</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency>

看看目錄結(jié)構(gòu)

shiro-web.ini

[users] root = secret,admin guest = guest,guest test = 123456,guest,test[roles] admin = * guest=user:list test=menu:list,menu:add[urls] /login.html=anon /index.html=authc /role=authc,roles[admin] /menu/**=authc,roles[admin],perms[menu:*]

com.javaweb

IndexServlet

package com.javaweb;import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "indexServlet",urlPatterns = "/index.html") public class IndexServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.getRequestDispatcher("/index.jsp").forward(req, resp);} }

LoginSevlet

package com.javaweb;import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet(name = "loginServlet",urlPatterns = "/login.html") public class LoginServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username=req.getParameter("username");String password=req.getParameter("password");Subject subject= SecurityUtils.getSubject();UsernamePasswordToken token=new UsernamePasswordToken(username,password);try {subject.login(token);resp.sendRedirect("/index.html");}catch (AuthenticationException e){e.printStackTrace();req.setAttribute("error","用戶名或者密碼錯誤");req.getRequestDispatcher("/login.jsp").forward(req,resp);}} }

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h2>Hello World!</h2> </body> </html>

login.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/29Time: 21:09To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> <form action="login.html" method="post">用戶名:<input type="text" name="username"/><br/>密碼:<input type="password" name="password"/><br/><input type="submit" value="登錄"/>${error} </form> </body> </html>

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param><param-name>shiroEnvirinmentClass</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-web.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>*.html</url-pattern></filter-mapping> </web-app>

運行結(jié)果 輸入網(wǎng)址

會跳轉(zhuǎn)到

登錄錯誤

登錄成功

總結(jié)

以上是生活随笔為你收集整理的shiro学习(10):servelet实现权限认证一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。