[shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39933993
shiro官網:?http://shiro.apache.org/
shiro中文手冊:http://wenku.baidu.com/link?url=ZnnwOHFP20LTyX5ILKpd_P94hICe9Ga154KLj_3cCDXpJWhw5Evxt7sfr0B5QSZYXOKqG_FtHeD-RwQvI5ozyTBrMAalhH8nfxNzyoOW21K
本文作者:sushengmiyan
------------------------------------------------------------------------------------------------------------------------------------
一。新建java webproject 這里取名為shirodemo
二。加入依賴的jar包。例如以下:
三。加入web對shiro的支持
如第一篇文章所述,在此基礎上添加webs.xml部署描寫敘述:
<listener><listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class></listener><filter><filter-name>shiro</filter-name><filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class></filter><filter-mapping><filter-name>shiro</filter-name><url-pattern>/*</url-pattern></filter-mapping>四。加入jsp頁面登陸button以及標簽支持:
<%String user = request.getParameter("username");String pwd = request.getParameter("password"); if(user != null && pwd != null){Subject sub = SecurityUtils.getSubject();String context = request.getContextPath();try{sub.login(new UsernamePasswordToken(user.toUpperCase(),pwd));out.println("登錄成功");}catch(IncorrectCredentialsException e){out.println("{success:false,msg:'username和password不對!'}");}catch(UnknownAccountException e){out.println("{success:false,msg:'用戶名不存在。'}");}return; } %>在jsp頁面中添加username與password登陸框。
五。新建realm實現
六。shiro.ini文件內容添加對realm的支持。
# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # # ============================================================================= # Quickstart INI Realm configuration # # For those that might not understand the references in this file, the # definitions are all based on the classic Mel Brooks' film "Spaceballs". ;) # =============================================================================# ----------------------------------------------------------------------------- # Users and their assigned roles # # Each line conforms to the format defined in the # org.apache.shiro.realm.text.TextConfigurationRealm#setUserDefinitions JavaDoc # -----------------------------------------------------------------------------#realm myRealm = com.susheng.shiro.ShiroDbRealm securityManager.realm = $myRealm[users] # user 'root' with password 'secret' and the 'admin' role root = secret, admin # user 'guest' with the password 'guest' and the 'guest' role guest = guest, guest # user 'presidentskroob' with password '12345' ("That's the same combination on # my luggage!!!" ;)), and role 'president' presidentskroob = 12345, president # user 'darkhelmet' with password 'ludicrousspeed' and roles 'darklord' and 'schwartz' darkhelmet = ludicrousspeed, darklord, schwartz # user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz' lonestarr = vespa, goodguy, schwartz# ----------------------------------------------------------------------------- # Roles with assigned permissions # # Each line conforms to the format defined in the # org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc # ----------------------------------------------------------------------------- [roles] # 'admin' role has all permissions, indicated by the wildcard '*' admin = * # The 'schwartz' role can do anything (*) with any lightsaber: schwartz = lightsaber:* # The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with # license plate 'eagle5' (instance specific id) goodguy = winnebago:drive:eagle5[urls] /login.jsp = anon /index.html = user /index.jsp = user /homePageDebug.jsp = user /module/** = user七。tomcat添加對這個應用的部署。啟動tomcat,輸入相應的url。
查看實現效果:
登錄界面的顯示
點擊登錄之后,插入了shiro的實現。
臨時沒有進行實質認證。僅僅是大概搭建的shiro環境。
自己插入自己的realm實現就能夠了。
OK。如今。以及實現了對web的支持。
代碼下載地址:http://download.csdn.net/detail/sushengmiyan/8022503
轉載于:https://www.cnblogs.com/yfceshi/p/6934510.html
總結
以上是生活随笔為你收集整理的[shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bzoj1051: [HAOI2006]
- 下一篇: linux 特殊shell变量