colspan 网页colspan

第四章 Web项目集成Shiro1、Web集成原理分析【1】web集成的配置

记住,在过去我们没有与WEB环境集成的时候,为了生成SecurityManager对象,我们手动读取配置文件生成工厂对象,然后通过工厂对象获取SecurityManager。如下面的代码所示。

/* * * * @描述登录方法*/private subject Shiro log in(string log in,string password){//导入权限ini文件构建权限工厂< SecurityManager & gtfactory = new IniSecurityManagerFactory(& # 34;类路径:shiro.ini & # 34);//工厂构造安全管理器安全管理器= Factory . getinstance();//使用SecurityUtils工具激活安全管理器SecurityUtils。setSecurityManager(安全管理器);//使用SecurityUtils工具获取subject subject = security tils . get subject();//生成帐户令牌username password token username password token = new username password token(登录名,密码);//登录操作主体。登录(usernamepasswordtoken);返回主题;然而,现在我们说我们想要与WEB集成,首先要做的是将我们的shiro.ini配置文件交付给WEB环境,并如下定义shiro.ini文件。

#声明一个自定义领域,并指定realm[main]definition realm = com。ithe IMA . Shiro . realm . definition realm security manager . realm = $ definition realm[1.1]新项目新web项目Shiro-day 01-07安全管理器的web。其中,境界、服务、资源的内容可以从Shiro-Day01-06认证-境界中复制。

colspan 网页colspan

【1.2】pom.xml配置

& lt?xml版本= & # 34;1.0"编码= & # 34;UTF-8 & # 34;?& gt& ltxmlns项目= & # 34;http://men.apache.org/POM/4.0.0" xmlns:xsi = & # 34;http://www.w3.org/2001/XMLSchema-instance" xsi:schema location = & # 34;http://men.apache.org/POM/4.0.0 http://men.apache.org/xsd/men-4.0.0.xsd" & lt;modelVersion & gt4 . 0 . 0 & lt;/model version & gt;& ltgroupId & gtcom . ithe IMA . Shiro & lt;/groupId & gt;& ltartifactId & gtShiro-day 01-07 web & lt;/artifact id & gt;& lt版本& gt1.0-快照& lt/version & gt;& lt包装& gt战争& lt/包装& gt& lt名称& gt07web Men Webapp & lt/name & gt;& lt!- FIXME把它改成项目& # 39;的网站-& gt;& lturl & gthttp://www . example . com & lt;/URL & gt;& lt依赖关系& gt& lt依赖性& gt& ltgroupId & gtcommons-logging & lt;/groupId & gt;& ltartifactId & gtcommons-logging & lt;/artifact id & gt;& lt版本& gt1 . 1 . 3 & lt;/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtorg . Apache . Shiro & lt;/groupId & gt;& ltartifactId & gtShiro-core & lt;/artifact id & gt;& lt版本& gt1 . 3 . 2 & lt;/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtorg . Apache . Shiro & lt;/groupId & gt;& ltartifactId & gtShiro-web & lt;/artifact id & gt;& lt版本& gt1 . 3 . 2 & lt;/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtjunit & lt/groupId & gt;& ltartifactId & gtjunit & lt/artifact id & gt;& lt版本& gt4.11 & lt/version & gt;& lt/dependency & gt;& lt/dependencies & gt;& lt构建& gt& lt插件& gt& lt!-Tomcat 7插件,命令:mvn Tomcat 7:run-DskipTests->–> & lt;插件& gt& ltgroupId & gtorg . Apache . Tomcat . men & lt;/groupId & gt;& ltartifactId & gtTomcat 7-men-plugin & lt;/artifact id & gt;& lt版本& gt2.2 & lt/version & gt;& lt配置& gt& lturiEncoding & gtutf-8 & lt;/uri encoding & gt;& lt端口& gt8080 & lt/port & gt;& lt路径& gt/平台& lt/path & gt;& lt/configuration & gt;& lt/plugin & gt;& lt!-编译器插件,设置JDK版本-& gt;& lt插件& gt& ltgroupId & gtorg . Apache . men . plugins & lt;/groupId & gt;& ltartifactId & gtmen-编译器-插件& lt/artifact id & gt;& lt版本& gt3.1 & lt/version & gt;& lt配置& gt& ltsource & gt8 & lt/source & gt;& lttarget & gt8 & lt/target & gt;& ltshowWarnings & gttrue & lt/show warnings & gt;& lt/configuration & gt;& lt/plugin & gt;& lt/plugins & gt;& lt/build & gt;& lt/project & gt;【1.3】web.xml配置;& lt上下文参数& gt& ltparam-name & gt;shiroEnvironmentClass & lt/param-name & gt;& lt参数值& gtorg . Apache . Shiro . web . env . ini web environment & lt;/param-value & gt;& lt/context-param & gt;& lt!-指定Shiro的配置文件的位置-& gt;& lt上下文参数& gt& ltparam-name & gt;shiroConfigLocations & lt/param-name & gt;& lt参数值& gtclass path:Shiro . ini & lt;/param-value & gt;& lt/context-param & gt;& lt!-在监控服务器启动时创建shiro的web环境。即加载shiroEnvironmentClass变量指定的IniWebEnvironment类–>;& lt听众& gt& lt侦听器类& gtorg . Apache . Shiro . web . env . environment loader listener & lt;/listener-class & gt;& lt/listener & gt;& lt!- shiro的L过滤门户过滤所有请求-& gt;& lt过滤器& gt& lt过滤器名称& gtshiroFilter & lt/filter-name & gt;& ltfilter-class & gt;org . Apache . Shiro . web . servlet . Shiro filter & lt;/filter-class & gt;& lt/filter & gt;& lt过滤器映射& gt& ltfilter-name & gt;shiroFilter & lt/filter-name & gt;& lt!-过滤所有请求-& gt;& lturl模式& gt/* & lt;/URL-pattern & gt;& lt/filter-mapping & gt;& lt/we B- app & gt;[2] SecurityManager对象创建上面我们将shiro集成到了web项目中。让我们跟踪源代码,看看securitymanager对象是如何创建的。

(1)我启动了服务器,监听器捕捉到了服务器启动事件。我现在所在的地方,在环境装载机的入口处。

(2)输入检查的方法。它根据shiroEnvironmentClass变量org的值初始化shiro环境对象。Apache . Shiro . web . env . iniwebenvironment。

(3)最后创建一个SecurityManager对象,然后绑定到字节码刚刚创建的Shiro环境对象。

当您来到这里时,SecurityManager被初始化。

2、Shiro默认过滤器

Shiro内置了许多默认过滤器,比如身份验证、授权等等。默认过滤器可以参照org中的枚举过滤器。Apache . Shiro . web . filter . mgt . default filter

【1】认证相关【2】授权相关3、Web集成完整案例

基于shiro-day01-07web继续集成。

【1】编写pom.xml

& lt?xml版本= & # 34;1.0"编码= & # 34;UTF-8 & # 34;?& gt& ltxmlns项目= & # 34;http://men.apache.org/POM/4.0.0" xmlns:xsi = & # 34;http://www.w3.org/2001/XMLSchema-instance" xsi:schema location = & # 34;http://men.apache.org/POM/4.0.0 http://men.apache.org/xsd/men-4.0.0.xsd" & lt;modelVersion & gt4 . 0 . 0 & lt;/model version & gt;& ltgroupId & gtcom . ithe IMA . Shiro & lt;/groupId & gt;& ltartifactId & gtShiro-day 01-07 web & lt;/artifact id & gt;& lt版本& gt1.0-快照& lt/version & gt;& lt包装& gt战争& lt/包装& gt& lt名称& gt07web Men Webapp & lt/name & gt;& lt!- FIXME把它改成项目& # 39;的网站-& gt;& lturl & gthttp://www . example . com & lt;/URL & gt;& lt依赖关系& gt& lt依赖性& gt& ltgroupId & gtcommons-logging & lt;/groupId & gt;& ltartifactId & gtcommons-logging & lt;/artifact id & gt;& lt版本& gt1 . 1 . 3 & lt;/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtorg . Apache . Shiro & lt;/groupId & gt;& ltartifactId & gtShiro-core & lt;/artifact id & gt;& lt版本& gt1 . 3 . 2 & lt;/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtorg . Apache . Shiro & lt;/groupId & gt;& ltartifactId & gtShiro-web & lt;/artifact id & gt;& lt版本& gt1 . 3 . 2 & lt;/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtjunit & lt/groupId & gt;& ltartifactId & gtjunit & lt/artifact id & gt;& lt版本& gt4.11 & lt/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtjax.servlet & lt/groupId & gt;& ltartifactId & gtjax.servlet-api/artifact id & gt;& lt版本& gt3 . 0 . 1 & lt;/version & gt;& lt范围& gt提供& lt/scope & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gtjstl & lt/groupId & gt;& ltartifactId & gtjstl & lt/artifact id & gt;& lt版本& gt1.2 & lt/version & gt;& lt/dependency & gt;& lt依赖性& gt& ltgroupId & gttaglibs & lt/groupId & gt;& ltartifactId & gt标准& lt/artifact id & gt;& lt版本& gt1 . 1 . 2 & lt;/version & gt;& lt/dependency & gt;& lt/dependencies & gt;& lt构建& gt& lt插件& gt& lt!-Tomcat 7插件,命令:mvn Tomcat 7:run-DskipTests->–> & lt;插件& gt& ltgroupId & gtorg . Apache . Tomcat . men & lt;/groupId & gt;& ltartifactId & gtTomcat 7-men-plugin & lt;/artifact id & gt;& lt版本& gt2.2 & lt/version & gt;& lt配置& gt& lturiEncoding & gtutf-8 & lt;/uri encoding & gt;& lt端口& gt8080 & lt/port & gt;& lt路径& gt/平台& lt/path & gt;& lt/configuration & gt;& lt/plugin & gt;& lt!-编译器插件,设置JDK版本-& gt;& lt插件& gt& ltgroupId & gtorg . Apache . men . plugins & lt;/groupId & gt;& ltartifactId & gtmen-编译器-插件& lt/artifact id & gt;& lt版本& gt3.1 & lt/version & gt;& lt配置& gt& ltsource & gt8 & lt/source & gt;& lttarget & gt8 & lt/target & gt;& ltshowWarnings & gttrue & lt/show warnings & gt;& lt/configuration & gt;& lt/plugin & gt;& lt/plugins & gt;& lt/build & gt;& lt/project & gt;[2]编写shiro.ini文件#声明自定义realm,并指定realms[main]definition realm = com . ithe IMA . Shiro . realm . realms = $ definition realm #用户退出并跳转到指定的JSP页面logout.redirection =/如果login.jsp#未登录,Authc。log in URL =/log in . JSP[URLs]/log in = anon # Send/home请求要求先登录/home= authc# Send /order/list请求要求先登录/order-list = roles[admin]#提交代码要求order:add permission/order-add = perms。订单:添加& # 34;] #更新代码需要order:del permission/order-del = perms[& # 34;订单:del & # 34] #发送退出请求时,使用exit filter/logout = logout [3]编写loginservicepackage com . ithe IMA . Shiro . service;import org . Apache . Shiro . authc . usernamepasswordtoken;导入Ja . lang . management . lockinfo;/* * * * @ Description:log in service */public interface log in service {/* * @ Description log in method * @ param token log in object * @ return */Boolean log in(username password token);/* * * * @描述注销方法*/void logout();}包com . ithe IMA . Shiro . service . impl;导入com . ithe IMA . Shiro . service . loginservice;导入org . Apache . Shiro . security utils;import org . Apache . Shiro . authc . usernamepasswordtoken;导入org . Apache . Shiro . subject . subject;/* * * * @描述:login service */public类login service实现login service { @ override public boolean log in(username password token){ subject = securit utils . get subject();试试{ subject . log in(token);}catch(异常e){返回false} return subject . is authenticated();} @ Override public void logout(){ Subject Subject = security utils . get Subject();subject . logout();}} [4]编写securityserviceimplpackagecom . ithe IMA . Shiro . service . impl;导入com . ithe IMA . Shiro . service . security service;导入Ja . util . ArrayList;导入Ja . util . hashmap;导入Ja . util . list;导入Ja . util . map;/* * * * @描述:权限服务层*/公共类securityservice实现安全服务{@覆盖公共地图

& lt%@页内容类型= & # 34;文本/html;charset = UTF-8 & # 34;language = & # 34ja & # 34% & gt& lthtml & gt& lthead & gt& ltmeta http-equiv = & # 34;内容类型& # 34;内容= & # 34;文本/html;charset = UTF-8 & # 34;& gt& lttitle & gtTitle & lt/title & gt;& lt/head & gt;& ltbody & gt& lt表单方法= & # 34;邮政& # 34;action = & # 34$ { pagecontext . request . context path }/log in & # 34;& gt& lt表& gt& lttr & gt& ltth & gt登录名

& lt%@ taglib前缀= & # 34;c & # 34uri = & # 34http://ja.sun.com/jsp/jstl/core" % & gt;& lt%@页内容类型= & # 34;文本/html;charset = UTF-8 & # 34;language = & # 34ja & # 34% & gt& lthtml & gt& lthead & gt& lttitle & gt& lt/title & gt;& lt/head & gt;& ltbody & gt& lth6 & gt& lta href = & # 34$ { pagecontext . request . context path }/logout & # 34;& gt出口

& lt%@页内容类型= & # 34;文本/html;charset = UTF-8 & # 34;language = & # 34ja & # 34% & gt& lthtml & gt& lthead & gt& ltmeta http-equiv = & # 34;内容类型& # 34;内容= & # 34;文本/html;charset = UTF-8 & # 34;& gt& lttitle & gtTitle & lt/title & gt;& lt/head & gt;& ltbody & gt添加页面

<%@ page contentType="text/html;charset=UTF-8" language="ja" %><%–导入jstl标签库–%><%@ taglib prefix="c" uri="http://ja.sun.com/jsp/jstl/core" %><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户列表jsp页面</title> <style> table {border:1px solid #000000} table th{border:1px solid #000000} table td{border:1px solid #000000} </style></head><body><table cellpadding="0" cellspacing="0" width="80%"> <tr> <th>编号</th> <th>公司名称</th> <th>信息来源</th> <th>所属行业</th> <th>级别</th> <th>联系地址</th> <th>联系电话</th> </tr> <tr> <td>1</td> <td>传智播客</td> <td>网络营销</td> <td>互联网</td> <td>普通客户</td> <td>津安创意园</td> <td>0208888887</td> </tr> <tr> <td>2</td> <td>黑马程序员</td> <td>j2ee</td> <td>互联网</td> <td>VIP客户</td> <td>津安创意园</td> <td>0208888887</td> </tr> <tr> <td>3</td> <td>黑马程序员</td> <td>大数据</td> <td>互联网</td> <td>VIP客户</td> <td>津安创意园</td> <td>0208888887</td> </tr></table></body></html>

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

发表回复

登录后才能评论