Apache Shiro学习笔记(五)Web集成扩展
2016-07-31 20:53
615 查看
鲁春利的工作笔记,好记性不如烂笔头
http://shiro.apache.org/web-features.html
基于Basic的拦截器身份验证
shiro-authc-basic.ini
![](http://s4.51cto.com/wyfs02/M01/85/3E/wKiom1ed84CSm1bFAAFvN61mtmc158.jpg)
说明:由于此时的登录是Filter来实现的,在ini文件中,用户名=密码,密码是明文存储的,不再适用/login登录方式(Servlet中密码是加密过的)。
基于表单的拦截器身份验证
shiro-form-filter.ini
authc 是org.apache.shiro.web.filter.authc.FormAuthenticationFilter 类型的实例,其用于实
现基于表单的身份验证。
通过loginUrl指定当身份验证时的登录表单;
usernameParam指定登录表单提交的用户名参数名;
passwordParam指定登录表单提交的密码参数名;
successUrl指定登录成功后重定向的默认地址(默认是“/”)(如果有上一个地址会自动重定向带该地址);
failureKeyAttribute指定登录失败时的request属性key(默认shiroLoginFailure);这样可以在登录表单得到该错误key显示相应的错误消息;
LoginFormServlet
loginForm.jsp
1、访问http://localhost:8080/invicme/role
2、跳转到http://localhost:8080/invicme/loginForm
![](http://s3.51cto.com/wyfs02/M02/85/3E/wKiom1ed9dfi99IIAAEgU6gb5gE342.jpg)
3、输入用户名密码,跳转到http://localhost:8080/invicme/role对应页面
本文出自 “闷葫芦的世界” 博客,请务必保留此出处http://luchunli.blog.51cto.com/2368057/1832556
http://shiro.apache.org/web-features.html
基于Basic的拦截器身份验证
shiro-authc-basic.ini
# 基于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=logoutauthcBasic是org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter类型的实例。
![](http://s4.51cto.com/wyfs02/M01/85/3E/wKiom1ed84CSm1bFAAFvN61mtmc158.jpg)
说明:由于此时的登录是Filter来实现的,在ini文件中,用户名=密码,密码是明文存储的,不再适用/login登录方式(Servlet中密码是加密过的)。
基于表单的拦截器身份验证
shiro-form-filter.ini
# 基于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说明:
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>
1、访问http://localhost:8080/invicme/role
2、跳转到http://localhost:8080/invicme/loginForm
![](http://s3.51cto.com/wyfs02/M02/85/3E/wKiom1ed9dfi99IIAAEgU6gb5gE342.jpg)
3、输入用户名密码,跳转到http://localhost:8080/invicme/role对应页面
本文出自 “闷葫芦的世界” 博客,请务必保留此出处http://luchunli.blog.51cto.com/2368057/1832556
相关文章推荐
- Apache Shiro学习笔记(五)Web集成使用JdbcRealm
- java安全框架-Shiro学习笔记(五)-Shiro集成Web
- apache shiro学习笔记(四) Web Support
- Shiro学习笔记(5)——web集成
- Shiro 学习笔记(7)—— Shiro 集成 Web
- Apache Shiro学习笔记(九)Spring集成
- Apache Shiro学习笔记(五)Web中的应用
- Apache Shiro Web Support学习笔记
- 巴巴运动网学习笔记四之在Web层集成Struts2.3与Spring3.1
- apache-shiro 学习笔记
- 韩顺平PHP学习视频笔记整理025web服务器介绍 apache服务器安装
- 【web++_第四阶_中间学习阶段_Apache Shiro、rest安全、SpringMVC_1】
- [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
- 构建高性能的web站点学习笔记二------数据库扩展
- Apache shiro 笔记整理之web整合二
- Apache Shiro with CAS SSO Server学习笔记
- apache-shiro 学习笔记
- NodeJS学习笔记 - Apache反向代理集成实现
- WebService 学习笔记之 集成Spring和Web容器
- 在 Web 项目中应用 Apache Shiro 并集成 CAS 单点登录