日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

第12章 与Spring集成

發布時間:2024/8/24 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第12章 与Spring集成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Shiro的組件都是JavaBean/POJO式的組件,所以非常容易使用Spring進行組件管理,可以非常方便的從ini配置遷移到Spring進行管理,且支持JavaSE應用及Web應用的集成。在示例之前,需要導入shiro-spring及spring-context依賴,具體請參考pom.xml。spring-beans.xml配置文件提供了基礎組件如DataSource、DAO、Service組件的配置。

JavaSE應用

spring-shiro.xml提供了普通JavaSE獨立應用的Spring配置:

??? <!-- 緩存管理器 使用Ehcache實現 -->

??? <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">

??????? <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>

??? </bean>

??? <!-- 憑證匹配器 -->

??? <bean id="credentialsMatcher" class="com.github.zhangkaitao.shiro.chapter12.credentials.RetryLimitHashedCredentialsMatcher">

??????? <constructor-arg ref="cacheManager"/>

??????? <property name="hashAlgorithmName" value="md5"/>

??????? <property name="hashIterations" value="2"/>

??????? <property name="storedCredentialsHexEncoded" value="true"/>

??? </bean>

<!-- Realm實現 -->

??? <bean id="userRealm" class="com.github.zhangkaitao.shiro.chapter12.realm.UserRealm">

??????? <property name="userService" ref="userService"/>

??????? <property name="credentialsMatcher" ref="credentialsMatcher"/>

??????? <property name="cachingEnabled" value="true"/>

??????? <property name="authenticationCachingEnabled" value="true"/>

??????? <property name="authenticationCacheName" value="authenticationCache"/>

??????? <property name="authorizationCachingEnabled" value="true"/>

??????? <property name="authorizationCacheName" value="authorizationCache"/>

??? </bean>

<!-- 會話ID生成器 -->

??? <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>

??? <!-- 會話DAO -->

??? <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">

??????? <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>

??????? <property name="sessionIdGenerator" ref="sessionIdGenerator"/>

??? </bean>

<!-- 會話驗證調度器 -->

??? <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">

??????? <property name="sessionValidationInterval" value="1800000"/>

??????? <property name="sessionManager" ref="sessionManager"/>

??? </bean>

<!-- 會話管理器 -->

??? <bean id="sessionManager" class="org.apache.shiro.session.mgt.DefaultSessionManager">

??????? <property name="globalSessionTimeout" value="1800000"/>

??????? <property name="deleteInvalidSessions" value="true"/>

??????? <property name="sessionValidationSchedulerEnabled" value="true"/>

??????? <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>

??? <property name="sessionDAO" ref="sessionDAO"/>

??? </bean>

<!-- 安全管理器 -->

??? <bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">

??????? <property name="realms">

?????????? <list><ref bean="userRealm"/></list>

??????? </property>

??????? <property name="sessionManager" ref="sessionManager"/>

??????? <property name="cacheManager" ref="cacheManager"/>

??? </bean>

??? <!-- 相當于調用SecurityUtils.setSecurityManager(securityManager) -->

??? <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">

??????? <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>

??????? <property name="arguments" ref="securityManager"/>

??? </bean>

<!-- Shiro生命周期處理器-->

??? <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

可以看出,只要把之前的ini配置翻譯為此處的spring xml配置方式即可,無須多解釋。LifecycleBeanPostProcessor用于在實現了Initializable接口的Shiro bean初始化時調用Initializable接口回調,在實現了Destroyable接口的Shiro bean銷毀時調用 Destroyable接口回調。如UserRealm就實現了Initializable,而DefaultSecurityManager實現了Destroyable。具體可以查看它們的繼承關系。

測試用例請參考com.github.zhangkaitao.shiro.chapter12.ShiroTest。

Web應用

Web應用和普通JavaSE應用的某些配置是類似的,此處只提供一些不一樣的配置,詳細配置可以參考spring-shiro-web.xml。

??? <!-- 會話Cookie模板 -->

??? <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">

??????? <constructor-arg value="sid"/>

??????? <property name="httpOnly" value="true"/>

??????? <property name="maxAge" value="180000"/>

??? </bean>

<!-- 會話管理器 -->

??? <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">

??????? <property name="globalSessionTimeout" value="1800000"/>

??????? <property name="deleteInvalidSessions" value="true"/>

??????? <property name="sessionValidationSchedulerEnabled" value="true"/>

??????? <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>

??????? <property name="sessionDAO" ref="sessionDAO"/>

??????? <property name="sessionIdCookieEnabled" value="true"/>

??????? <property name="sessionIdCookie" ref="sessionIdCookie"/>

??? </bean>

<!-- 安全管理器 -->

??? <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

??????? <property name="realm" ref="userRealm"/>

??????? <property name="sessionManager" ref="sessionManager"/>

??????? <property name="cacheManager" ref="cacheManager"/>

??? </bean>

1、sessionIdCookie是用于生產Session ID Cookie的模板;

2、會話管理器使用用于web環境的DefaultWebSessionManager;

3、安全管理器使用用于web環境的DefaultWebSecurityManager。

<!-- 基于Form表單的身份驗證過濾器 -->

??? <bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">

??????? <property name="usernameParam" value="username"/>

??????? <property name="passwordParam" value="password"/>

??? <property name="loginUrl" value="/login.jsp"/>

??? </bean>

<!-- Shiro的Web過濾器 -->

??? <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

??????? <property name="securityManager" ref="securityManager"/>

??????? <property name="loginUrl" value="/login.jsp"/>

??????? <property name="unauthorizedUrl" value="/unauthorized.jsp"/>

??? <property name="filters">

??? <util:map>

??????? <entry key="authc" value-ref="formAuthenticationFilter"/>

??? </util:map>

??? </property>

??? <property name="filterChainDefinitions">

??? <value>

??????? /index.jsp = anon

??????? /unauthorized.jsp = anon

??????? /login.jsp = authc

??????? /logout = logout

??????? /** = user

??? </value>

??? </property>

??? </bean>

1、formAuthenticationFilter為基于Form表單的身份驗證過濾器;此處可以再添加自己的Filter bean定義;

2、shiroFilter:此處使用ShiroFilterFactoryBean來創建ShiroFilter過濾器;filters屬性用于定義自己的過濾器,即ini配置中的[filters]部分;filterChainDefinitions用于聲明url和filter的關系,即ini配置中的[urls]部分。

接著需要在web.xml中進行如下配置:

??? <context-param>

??????? <param-name>contextConfigLocation</param-name>

?????? <param-value>

??????????? classpath:spring-beans.xml,

??????????? classpath:spring-shiro-web.xml

??????? </param-value>

??? </context-param>

??? <listener>

??? <listener-class>

??????? org.springframework.web.context.ContextLoaderListener

??? </listener-class>

??? </listener>

通過ContextLoaderListener加載contextConfigLocation指定的Spring配置文件。

??? <filter>

??????? <filter-name>shiroFilter</filter-name>

??????? <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

??? <init-param>

??????? <param-name>targetFilterLifecycle</param-name>

??????? <param-value>true</param-value>

??????? </init-param>

??? </filter>

??? <filter-mapping>

??????? <filter-name>shiroFilter</filter-name>

??????? <url-pattern>/*</url-pattern>

??? </filter-mapping>

??? DelegatingFilterProxy會自動到Spring容器中查找名字為shiroFilter的bean并把filter請求交給它處理。

??? 其他配置請參考源代碼。

Shiro權限注解

??? Shiro提供了相應的注解用于權限控制,如果使用這些注解就需要使用AOP的功能來進行判斷,如Spring AOP;Shiro提供了Spring AOP集成用于權限注解的解析和驗證。

??? 為了測試,此處使用了Spring MVC來測試Shiro注解,當然Shiro注解不僅僅可以在web環境使用,在獨立的JavaSE中也是可以用的,此處只是以web為例了。

??? 在spring-mvc.xml配置文件添加Shiro Spring AOP權限注解的支持:

??? <aop:config proxy-target-class="true"></aop:config>

??? <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">

??????? <property name="securityManager" ref="securityManager"/>

??? </bean>

??? 如上配置用于開啟Shiro Spring AOP權限注解的支持;<aop:config proxy-target-class="true">表示代理類。

??? 接著就可以在相應的控制器(AnnotationController)中使用如下方式進行注解:

??????? @RequiresRoles("admin")

??????? @RequestMapping("/hello2")

??????? public String hello2() {

??????? return "success";

??????? }

??? 訪問hello2方法的前提是當前用戶有admin角色。

??? 當驗證失敗,其會拋出UnauthorizedException異常,此時可以使用Spring的ExceptionHandler(DefaultExceptionHandler)來進行攔截處理:

??????? @ExceptionHandler({UnauthorizedException.class})

??????? @ResponseStatus(HttpStatus.UNAUTHORIZED)

??????? public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {

??????????? ModelAndView mv = new ModelAndView();

??????????? mv.addObject("exception", e);

??????????? mv.setViewName("unauthorized");

??????????? return mv;

??????????? }

??? 如果集成Struts2,需要注意《Shiro+Struts2+Spring3 加上@RequiresPermissions 后@Autowired失效》問題:http://jinnianshilongnian.iteye.com/blog/1850425

權限注解

??? @RequiresAuthentication??? 表示當前Subject已經通過login進行了身份驗證;即Subject. isAuthenticated()返回true。

?? @RequiresUser? 表示當前Subject已經身份驗證或者通過記住我登錄的。

?? @RequiresGuest? 表示當前Subject沒有身份驗證或通過記住我登錄過,即是游客身份。

?? @RequiresRoles(value={“admin”, “user”}, logical= Logical.AND) 表示當前Subject需要角色admin和user。

? @RequiresPermissions (value={“user:a”, “user:b”}, logical= Logical.OR) 表示當前Subject需要權限user:a或user:b。

轉載于:https://www.cnblogs.com/pyrmkj/p/4499860.html

總結

以上是生活随笔為你收集整理的第12章 与Spring集成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。