Apache shiro 筆記整理之web整合一

來源:互聯網
上載者:User

標籤:catch   ons   std   日誌管理   https   success   pos   isp   驗證   

下面內容是在看了濤哥的《跟我一起學shiro》 和 視頻《一頭紮入進shiro》 後整理出來備忘和方便自己和其它人學習。

個人首頁:http://www.itit123.cn/ 很多其它乾貨等你來拿

第一步:建立maven版web項目:http://blog.csdn.net/qq_19558705/article/details/49887717

建立好後須要: 右擊項目 ----> build path ----> config build path ----> add library ----> server runtime ----> 選擇合適的就可以 避免出現 “The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path” 錯誤 


第二步:匯入相關的jar

<!-- 日誌管理 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!-- shiro --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.2.4</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.2.4</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.12</version></dependency>

第三步:配置web.xml檔案(該配置方法是載入shiro.ini方法,實際開發中不是這樣,能夠看官網文檔)

<!-- shiro 監聽 --><listener><listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class></listener><!-- shiro 攔截 --><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>

第四步:身分識別驗證

shiro.ini檔案:

[main]#使用者登入路徑authc.loginUrl=/login[users]ITDragon=123456,admin[urls]#該路徑為匿名登入/login=anon#身分識別驗證後才幹登入/admin=authc


login.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登入頁面</title></head><body><form action="login" method="post">userName:<input type="text" name="userName" /><br /> password:<input type="password" name="password" /><br /> <input type="submit" value="Submit" /></form></body></html>


LoginServlet:

package com.shiro.servlet;import java.io.IOException;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 org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.subject.Subject;@WebServlet("/login")public class LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {System.out.println("login doGet ... ");request.getRequestDispatcher("login.jsp").forward(request, response);}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {System.out.println("login doPost ... ");String userName = request.getParameter("userName");String password = request.getParameter("password");Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken(userName,password);try {subject.login(token);response.sendRedirect("success.jsp");} catch (Exception e) {e.printStackTrace();request.setAttribute("errorInfo", "username或者密碼錯誤");request.getRequestDispatcher("login.jsp").forward(request, response);}}}

通過瀏覽器訪問發現:未登入的訪問/admin會跳轉到登入頁面,若登入後在訪問就可以進入成功頁面,說明身分識別驗證成功。


第五步:許可權認證

shiro檔案:

[main]#使用者登入路徑authc.loginUrl=/login#角色驗證roles.unauthorizedUrl=/unauthorized.jsp#許可權驗證perms.unauthorizedUrl=/unauthorized.jsp[users]ITDragon=123456,adminteacher1=123456,teacherstudent1=123456[roles]admin=user:*teacher=student:*[urls]#該路徑為匿名登入/login=anon#身分識別驗證後才幹登入/admin=authc#該路徑驗證是否擁有teacher角色/student=roles[teacher]#該路徑驗證該角色是否擁有許可權/teacher=perms["admin:delete"]

unauthorized.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>錯誤頁面</title></head><body>對不起。您不具備該許可權。</body></html>

在瀏覽器中訪問/student。會先跳到登入頁面進行身分識別驗證。然後在推斷該使用者是否擁護teacher角色許可權

在瀏覽器中訪問/teacher,推斷使用者是否擁有該許可權。

由於沒有準備相應的servlet,所以正確情況會顯示404,若沒有許可權則會跳到 unauthorized.jsp 頁面。


這樣就完畢了web中shiro的HelloWorld,之後會具體記錄筆記。


原始碼下載路徑:http://download.csdn.net/detail/qq_19558705/9449892



Apache shiro 筆記整理之web整合一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.