shiro学习(10):servelet实现权限认证一
生活随笔
收集整理的這篇文章主要介紹了
shiro学习(10):servelet实现权限认证一
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
工具idea
先看看數(shù)據(jù)庫(kù)
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","用戶名或者密碼錯(cuò)誤");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>運(yùn)行結(jié)果 輸入網(wǎng)址
會(huì)跳轉(zhuǎn)到
登錄錯(cuò)誤
登錄成功
總結(jié)
以上是生活随笔為你收集整理的shiro学习(10):servelet实现权限认证一的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 西里尔字符_如何设计西里尔字母Њ(Nje
- 下一篇: 高性能Mysql(一)