詳しくはspringとshiroを解して集めます。
Shroのコンポーネントは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構成を提供する:
テストケースはcomp.github.zhangkaitao.shiro.chapter 12.Shro Testを参照してください。
Webアプリケーション
Webアプリケーションと一般的なJavaSEアプリケーションのいくつかの構成は似ていますが、ここではいくつかの異なる構成のみを提供しています。詳細な構成はspring-shiro-web.xmlを参照することができます。
2、会話マネージャはウェブ環境用のDefault WebSession Managerを使用する。
3、セキュリティマネージャはウェブ環境用のDefault WebSecurityManagerを使用します。
2、shiroFilter:ここでShroFilter FactoryBeanを使ってShroFilterフィルタを作成します。filters属性は、自分のフィルタ、すなわちini構成における「filters」部分を定義するために使用される。filterChanDefinitionsは、urlとfilterの関係、すなわちini構成における「urls」の部分を宣言するために使用されます。
次に、web.xmlにおいて、次のような構成が必要である。
Shro権限コメント
Shroは対応する注釈を権限制御に提供しています。これらの注釈を使用すると、Spring AOPのようなAOPの機能を使用して判断する必要があります。Shroは、Spring AOP統合のための権限注釈の解析と検証を提供する。
テストのために、ここでSpring MVCを使って、Shroコメントをテストします。もちろんShroコメントはweb環境だけではなく、独立したJavaSEでも使用できます。ここではwebだけを例にしています。
spring-mvc.xmlプロファイルにShro Spring AOP権限コメントのサポートを追加します。
次に、対応するコントローラ(AnnotationController)において、以下のように注釈することができる。
検証に失敗した場合、Unauthorized Exception異常を投げます。SpringのException Handler(Default Exception Handler)を使ってブロッキング処理を行うことができます。
締め括りをつける
以上は小编が皆さんに绍介した详しい解springとshiro集积です。皆さんに助けてほしいです。もし何か疑问があれば、メッセージをください。小编はすぐに皆さんに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。
例を挙げる前に、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インターフェースを実現したShrobean初期化時にInitializableインターフェースのフィードバックを呼び出し、Destroyableインターフェースを実現したShrobean廃棄時に呼び出します。 Destroyableインターフェースのフィードバック。UserRealmのようにInitializableを実現し、Default SecurityManagerがDestroyableを実現しました。具体的にはそれらの継承関係を見ることができます。 テストケースはcomp.github.zhangkaitao.shiro.chapter 12.Shro Testを参照してください。
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、会話マネージャはウェブ環境用のDefault WebSession Managerを使用する。
3、セキュリティマネージャはウェブ環境用のDefault WebSecurityManagerを使用します。
<!-- 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、フォームAuthentication FilterはFormフォームに基づくアイデンティティ検証フィルタである。ここで自分のFilter bean定義を追加できます。2、shiroFilter:ここでShroFilter FactoryBeanを使ってShroFilterフィルタを作成します。filters属性は、自分のフィルタ、すなわちini構成における「filters」部分を定義するために使用される。filterChanDefinitionsは、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>
コンテントxt Loader Listenerによって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の要求をそれに渡して処理します。Shro権限コメント
Shroは対応する注釈を権限制御に提供しています。これらの注釈を使用すると、Spring AOPのようなAOPの機能を使用して判断する必要があります。Shroは、Spring AOP統合のための権限注釈の解析と検証を提供する。
テストのために、ここでSpring MVCを使って、Shroコメントをテストします。もちろんShroコメントはweb環境だけではなく、独立したJavaSEでも使用できます。ここではwebだけを例にしています。
spring-mvc.xmlプロファイルにShro 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>
上述のように、Shro Spring AOP権限注釈を開くためのサポートを設定します。<aop:config proxy-target-class="true">
は代理クラスを表します。次に、対応するコントローラ(AnnotationController)において、以下のように注釈することができる。
@RequiresRoles("admin")
@RequestMapping("/hello2")
public String hello2() {
return "success";
}
ハロー2メソッドにアクセスする前提は、現在のユーザーにadminキャラクターがあることです。 検証に失敗した場合、Unauthorized Exception異常を投げます。SpringのException Handler(Default Exception Handler)を使ってブロッキング処理を行うことができます。
@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;
}
権限コメント @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。締め括りをつける
以上は小编が皆さんに绍介した详しい解springとshiro集积です。皆さんに助けてほしいです。もし何か疑问があれば、メッセージをください。小编はすぐに皆さんに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。