1、使用maven加入shiro的jar包,如圖,找到https://my.oschina.net/u/2427561/blog/edit#相關xml節點放入pom.xml,運行項目便可(這裏選的是最新版本的shiro) 導入jar包以下:java
二在 web.xml配置shirogit
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <listener> <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class> </listener> <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> <filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>com.learnging.system.LearngingConfig</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
三建立shiro.ini文件web
[main] #realm shiroRealm=com.learnging.system.shiro.ShiroRealm securityManager.realm=$shiroRealm authc.loginUrl = /a/login authc.successUrl = /a/index logout.redirectUrl = /a/login [urls] /index = authc /a/logout = logout
(1)securityManager.realm:設置shiro中領域,能夠爲多個apache
(2)authc.loginUrl:設置認證攔截url。此處認證爲登陸,此處url爲登陸時提交的表單method屬性中對應的url。shiro內部自帶的攔截器會去默認攔截。app
(3)authc.successUrl:設置認證成功後的url跳轉路勁。框架
(4)logout.redirectUrl:設置退出登陸時跳轉的url路勁。maven
(5)[urls]:此處配置爲指定url 的權限如 /index = authc:意思爲若是訪問index必需要進行驗證經過其中有不少參數,authc只是其中一個認證權限ide
四建立Realmurl
(1)建立ShiroRealm,並繼承AuthorizingRealm其中並實現相關方法。.net
/** * 獲取登陸用戶擁有的角色和權限 add by xjj */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String)principals.getPrimaryPrincipal();//當前登陸用戶名 SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.addRole("admin"); //將當前用戶名下的角色存入authorizationInfo,暫時寫死 authorizationInfo.addStringPermission("user:create"); //將當前用戶名下的權限存入authorizationInfo,暫時寫死 return authorizationInfo; } /** * 驗證用戶是否合法 add by xjj */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UserService userService = new UserService(); String name = String.valueOf(token.getPrincipal()); //當前登陸用戶名 User user = userService.findByUserName(name); //根據用戶名查找用戶信息 return new SimpleAuthenticationInfo(user, user.getPassword(), getName()); //存入查詢用戶信息
(2)doGetAuthenticationInfo方法驗證登陸用戶是否合法,並將該用戶信息存入SimpleAuthenticationInfo。
(3)在系統認證成功後將會調用doGetAuthorizationInfo方法進行權限獲取,此方法中 principals.getPrimaryPrincipal();獲取剛纔存入的User對象; info.addStringPermission("權限名稱");用戶添加權限此處能夠經過用戶查詢權限表進行查詢。
五加入在網上找的dafei的jfinal整合shiro的Java文件
下載地址:http://git.oschina.net/myaniu/jfinalshiroplugin
六配置shiro攔截器
interceptors.add(new ShiroInterceptor());
七配置插件
me.add(new ShiroPlugin(routes))
八運行試一下
1報以下錯
解決辦法以下,即再pom.xml中加commons-logging的引用
九具體使用,在用戶管理列表方法前加註解,而後就沒權限進入該方法了,至此,最簡單的驗證成形。
總結: 1.web.xml 纔是整個 Web 應用的核心所在,Web 容器(例如:Tomcat)會提供一些監聽器,用於監聽 Web 應用的生命週期事件。
2.EnvironmentLoaderListener是整個 Shiro Web 應用的入口
EnvironmentLoaderListener在容器啓動時建立 WebEnvironment 對象,並由該對象來讀取 Shiro 配置文件,建立WebSecurityManager 與 FilterChainResolver 對象,它們都在後面將要出現的 ShiroFilter 中起到了重要做用。 從 web.xml 中一樣能夠得知,ShiroFilter 是整個 Shiro 框架的門面,由於它攔截了全部的請求,後面是須要 Authentication(認證)仍是須要 Authorization(受權)都由它說了算。 我參考的EnvironmentLoaderListener相關博客: http://www.codeweblog.com/shiro-%E4%B9%8B-%E5%85%A5%E5%8F%A3-environmentloaderlistener/