魯春利的工做筆記,好記性不如爛筆頭html
http://shiro.apache.org/web-features.html
java
基於Basic的攔截器身份驗證web
shiro-authc-basic.ini
apache
# 基於Basic的攔截器身份驗證 [main] # 默認是/login.jsp authc.loginUrl=/login authcBasic.applicationName=請登陸 [users] # 用戶名=密碼,角色 lucl=123456,admin wang=123456 [roles] admin=user:*,menu:* [urls] /login=anon /static/**=anon /role=authcBasic,roles[admin] /permission=authcBasic,perms["user:create"] /logout=logout
authcBasic是org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter類型的實例。app
說明:因爲此時的登陸是Filter來實現的,在ini文件中,用戶名=密碼,密碼是明文存儲的,再也不適用/login登陸方式(Servlet中密碼是加密過的)。jsp
基於表單的攔截器身份驗證ide
shiro-form-filter.ini
ui
# 基於Basic的攔截器身份驗證 [main] authc.loginUrl=/loginForm authc.usernameParam=username authc.passwordParam=password authc.successUrl=/shiro/admin/success.jsp authc.failureKeyAttribute=shiroLoginFailure [users] # 用戶名=密碼,角色 lucl=123456,admin wang=123456 [roles] admin=user:*,menu:* [urls] /loginForm=authc /static/**=anon /role=authc,roles[admin] /permission=authc,perms["user:create"] /logout=logout
說明:this
authc 是org.apache.shiro.web.filter.authc.FormAuthenticationFilter 類型的實例,其用於實
現基於表單的身份驗證。加密
經過loginUrl指定當身份驗證時的登陸表單;
usernameParam指定登陸表單提交的用戶名參數名;
passwordParam指定登陸表單提交的密碼參數名;
successUrl指定登陸成功後重定向的默認地址(默認是「/」)(若是有上一個地址會自動重定向帶該地址);
failureKeyAttribute指定登陸失敗時的request屬性key(默認shiroLoginFailure);這樣能夠在登陸表單獲得該錯誤key顯示相應的錯誤消息;
LoginFormServlet
package com.invicme.apps.servlet.form; 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.IncorrectCredentialsException; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.subject.Subject; /** * @author lucl */ @WebServlet("/loginForm") public class LoginFormServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginFormServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String errorClassName = (String)request.getAttribute("shiroLoginFailure"); String msg = ""; Subject subject = SecurityUtils.getSubject(); if(UnknownAccountException.class.getName().equals(errorClassName)) { msg = "用戶名/密碼錯誤"; } else if(IncorrectCredentialsException.class.getName().equals(errorClassName)) { msg = "用戶名/密碼錯誤"; } else if(errorClassName != null) { msg = "未知錯誤:" + errorClassName; } request.setAttribute("msg", msg); request.setAttribute("subject", subject); request.getRequestDispatcher("shiro/admin/loginForm.jsp").forward(request, response); } }
loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陸頁</title> </head> <body> <h1>This is login page.</h1><font color="red">${msg}</font> <form name="loginForm" action="<%=request.getContextPath()%>/loginForm" method="POST"> 用戶名:<input type="text" name="username" value="" /> <br/> 密碼:<input type="password" name="password" value="" /> <br/> <input type="submit" name="sub" value="提交" /> </form> </body> </html>
一、訪問http://localhost:8080/invicme/role
二、跳轉到http://localhost:8080/invicme/loginForm
三、輸入用戶名密碼,跳轉到http://localhost:8080/invicme/role對應頁面