今天就跟大家聊聊有關(guān)Apache Shiro怎么入門,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、忻城網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、成都做商城網(wǎng)站、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為忻城等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
項目權(quán)限 表
導入 shiro-all-1.4.1.jar 包
配置 web.xml
<filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
spring配置文件 applicationContext.xml
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"></property> <!-- 配置登錄頁面 --> <property name="loginUrl" value="/login.jsp"></property> <!-- url攔截規(guī)則 --> <property name="filterChainDefinitions"> <value> /validatecode.jsp* = anon /userAction_login = anon /* = authc </value> </property> </bean> <!-- 配置shiro的安全管理者 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="realm"></property> </bean> <!-- extends AuthorizingRealm --> <bean id="realm" class="com.gwl.bos.web.realm.BosRealm"></bean>
anon:例子/admins/**=anon 沒有參數(shù),表示可以匿名使用。
authc:例如/admins/user/**=authc表示需要認證(登錄)才能使用,沒有參數(shù)
roles:例子/admins/user/**=roles[admin],參數(shù)可以寫多個,多個時必須加上引號,并且參數(shù)之間用逗號分割,當有多個參數(shù)時,例如admins/user/**=roles["admin,guest"],每個參數(shù)通過才算通過,相當于hasAllRoles()方法。
perms:例子/admins/user/**=perms[user:add:*],參數(shù)可以寫多個,多個時必須加上引號,并且參數(shù)之間用逗號分割
例如/admins/user/**=perms["user:add:*,user:modify:*"],當有多個參數(shù)時必須每個參數(shù)都通過才通過,相當于isPermitedAll()方法。
rest:例子/admins/user/**=rest[user],根據(jù)請求的方法,相當于/admins/user/**=perms[user:method] ,其中method為post,get,delete等。
port:例子/admins/user/**=port[8081],當請求的url的端口不是8081是跳轉(zhuǎn)到schemal://serverName:8081?queryString,其中schmal是協(xié)議http或https等,serverName是你訪問的host,8081是url配置里port的端口,queryString是你訪問的url里的?后面的參數(shù)。
authcBasic:例如/admins/user/**=authcBasic沒有參數(shù)表示httpBasic認證
ssl:例子/admins/user/**=ssl沒有參數(shù),表示安全的url請求,協(xié)議為https
user:例如/admins/user/**=user沒有參數(shù)表示必須存在用戶,當?shù)侨氩僮鲿r不做檢查
注:anon,authcBasic,auchc,user是認證過濾器,perms,roles,ssl,rest,port是授權(quán)過濾器
BosRealm
package com.gwl.bos.web.realm; import com.gwl.bos.dao.UserDao; import com.gwl.bos.model.User; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; public class BosRealm extends AuthorizingRealm { /** * 權(quán)限 與角色相關(guān) */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { return null; } @Autowired private UserDao userDao; /** * 登錄認證 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; User user = userDao.findByUsername(token.getUsername()); if (user != null) { /** * Object principal 數(shù)據(jù)庫查詢的對象 * Object credentials 查詢出來的密碼,自動驗證 * String realmName 當前類名 */ SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), this .getClass().getSimpleName()); return info; } return null; } }
userAction
package com.gwl.bos.web.action; public class UserAction extends BaseAction<User> { public String login() { String username = getModel().getUsername(); String password = getModel().getPassword(); HttpServletRequest request = ServletActionContext.getRequest(); String serviceCheckCode = (String) request.getSession().getAttribute("key"); String clienCheckCode = request.getParameter("checkcode"); if (serviceCheckCode.equalsIgnoreCase(clienCheckCode)) { //使用shiro驗證登錄 Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, MD5Utils.text2md5(password)); try { subject.login(token); User loginUser = (User) subject.getPrincipal(); subject.getSession().setAttribute("loginUser", loginUser); return "home"; } catch (AuthenticationException e) { e.printStackTrace(); System.out.println("登錄失敗,用戶名密碼不正確"); } } else { System.out.println("驗證碼不正確"); } return "loginfailure"; } }
<!-- 配置全局的結(jié)果視圖 --> <global-results> <result name="unauthorizedUrl" type="redirect">/authorizing.jsp</result> </global-results> <!-- shiro拋出具體的異常來到的頁面 --> <global-exception-mappings> <!-- 權(quán)限 --> <exception-mapping exception="org.apache.shiro.authz.UnauthorizedException" result="unauthorizedUrl"></exception-mapping> <!-- 登錄 --> <exception-mapping exception="org.apache.shiro.authz.AuthorizationException" result="unauthorizedUrl"></exception-mapping> </global-exception-mappings>
spring配置文件 applicationContext.xml
<property name="filterChainDefinitions"> <value> /page_base_staff = perms["staff"] <!-- /page_base_staff = roles["staff"] --> </value> </property>
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { //根據(jù)數(shù)據(jù)庫查詢的角色權(quán)限賦予不同權(quán)限 SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addStringPermission("staff"); // info.addRole("staff"); return info; }
spring配置文件 applicationContext.xml
<!-- 開啟shiro注解 --> <bean id="defaultAdvisorAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> <property name="proxyTargetClass" value="true"></property> </bean> <!-- 切面類 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"></bean> <!-- 解決泛型問題 --> <bean class="com.gwl.bos.web.action.StaffAction" scope="prototype"></bean>
@RequiresPermissions
@RequiresPermissions("staff") @Override public String save() { staffService.save(getModel()); return SUCCESS; }
在jsp頁面引入shiro標簽
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
根據(jù)當前用戶的權(quán)限動態(tài)展示頁面元素有無
<shiro:hasPermission name="staff"> <a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a> </shiro:hasPermission>
@Override public String save() { SecurityUtils.getSubject().checkPermission("staff"); staffService.save(getModel()); return SUCCESS; }
看完上述內(nèi)容,你們對Apache Shiro怎么入門有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
新聞名稱:ApacheShiro怎么入門
本文鏈接:http://www.rwnh.cn/article36/ghcosg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、面包屑導航、小程序開發(fā)、電子商務(wù)、云服務(wù)器、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)