春のブートのサポートを追加史郎

プロジェクト内の依存関係を追加します。

       <! -史郎春。- > 
	<依存> 
		<groupIdを> org.apache.shiro </ groupIdを> 
		<たartifactId>史郎スプリング</たartifactId> 
		<バージョン> 1.4.0 </バージョン> 
	</依存関係> 
	<! -史朗コア- > 
	<依存性> 
		<のgroupId> org.apache.shiro </のgroupId> 
		<たartifactId>シロコア</たartifactId> 
		<バージョン> 1.4.0 </バージョン> 
	</依存>

プロファイルディレクトリの下に新しい春のフォルダ、春-shiro.xmlファイルに新しいフォルダを作成します

<?xmlのバージョン= "1.0"エンコード= "UTF-8"> 
<豆のxmlns = "http://www.springframework.org/schema/beans"のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス"のxmlns:AOP = "http://www.springframework.org/schema/aop"のxmlns:TX = "http://www.springframework.org/schema/tx"のxmlns:UTIL =" http://www.springframework.org/schema/util "のxmlns:コンテキスト= "http://www.springframework.org/schema/context"のxsi:schemaLocationの=" 
       http://www.springframework.org/schema/豆http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx .xsd 
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop。XSD 
       http://www.springframework.org/schema/utilます。http://www.springframework。ORG /スキーマ/ utilに/春-util.xsd 
       http://www.springframework.org/schema/context HTTP:// WWW .springframework.org /スキーマ/コンテキスト/春context.xsd "> 



	SecurityManagerの"クラス=" org.apache.shiro.web.mgt.DefaultWebSecurityManager「> <豆言及ID =上記の" 
		<! -私たちが書きrefの対応レルムMYREALM - > 
		<プロパティ名= "王国" REF = "AuthRealm" /> 
		<! -次キャッシュマネージャを使用して設定- > 
		<! - <プロパティ名= "のCacheManager" REF = "shiroEncacheManager" /> - - > 
	</豆> 




	<! -安全な認証フィルタ- > 
	<豆ID = "shiroFilter"クラス= "org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<! - -我々は権利Managerの設定を呼び出す> 
		</プロパティ名= "SecurityManagerの" REF = "SecurityManagerの">
		<! - -ログイン設定我々はアドレスを要求した>  
		<プロパティ名="loginUrl「値=」/ toLogin「/> 
		<! -あなたはアドレスにアクセスする場合は、ログインページで私達のログインに成功するが、非/ログインは、あなたが訪問アドレスにスキップジャンプアドレス後の設定- >
		<プロパティ名=「successUrl」値=「/」/> 
		<! -あなたがリソースを要求した場合は、もはや自分の理解の範囲ではない、/ 403の要求に行きますアドレス- > 
		<プロパティ名= "unauthorizedUrl"値= "/ HTML / 403.html" /> 
		<プロパティ名= "filterChainDefinitions"> 
			<値> 
			<! -アノンが反対authcは通過を許可された- > 
				/静的に/アノン= ** 
				/ログインアノン= 
				/ ** = authcは
			</ value>の
		</プロパティ> 
	</豆> 


	<! -史郎豆内部ライフサイクル機能の実現を確保するために実行- > 
	<豆ID = "lifecycleBeanPostProcessor"クラス= "org.apache.shiro.spring.LifecycleBeanPostProcessor" /> 

	<! - AOPのアプローチを確認するレベルの権限- > 
	<Beanクラスは、= "org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" 依存-に= "lifecycleBeanPostProcessor">
		<プロパティ名= "proxyTargetClass"値= "TRUE" />値= "true"を/> 
	</豆>
	<beanクラス= "org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> 
		<プロパティ名= "SecurityManagerの" REF = "SecurityManagerの" /> 
	</豆> 




</豆>

一入口ばねshiro.xmlをロード

@ImportResource({ "クラスパス:春/スプリングshiro.xml"})

コントローラを変更するには、ログイン

		//ログインパラメータ設定済み
		UsernamePasswordToken新しい新しいトークン= UsernamePasswordToken(名前、PWD); 
		試み{ 
			取り扱いに//レルム
			SecurityUtils.getSubject()ログイン(トークン);. 
		}キャッチ(UnknownAccountException UAE){ 
			map.put( "MSG" "不明なユーザー"); 
			リターン"ログイン"; 
		}キャッチ(IncorrectCredentialsExceptionのICE){ 
			map.put( "MSG"、 "パスワードエラー"); 
			リターン"ログイン"; 
		}キャッチ(含むAuthenticationException AE){ 
			//条件条件のために予期しませんか? ?エラー
			map.put( "MSG"、 "サーバービジー"); 
			リターン"ログイン"; 
		} 
	 
		リターン"リダイレクト:/ toIndexの";

私たちは、クラスのレルムを持っているので、5行は、レルムのクラスに対処するために知っているだろう参照します

doGetAuthenticationInfo(ログインロジック)を書き換え、新しいクラスと継承AuthRealm AuthorizingRealmを作成するためにスキャンすることができ、メインエントランスには、上書きdoGetAuthorizationInfo(認可ロジック)

輸入org.apache.shiro.authc.AuthenticationException。
輸入org.apache.shiro.authc.AuthenticationInfo; 
輸入org.apache.shiro.authc.AuthenticationToken; 
輸入org.apache.shiro.authz.AuthorizationInfo; 
輸入org.apache.shiro.realm.AuthorizingRealm; 
輸入org.apache.shiro.subject.PrincipalCollection。

パブリッククラスAuthRealmがAuthorizingRealm {延び

	@Overrideは
	AuthenticationInfo doGetAuthenticationInfo保護(AuthenticationTokenトークン)含むAuthenticationException {スロー
		// TODO自動生成方法スタブ
		戻りNULLと、
	} 

	@Overrideは
	{AuthorizationInfo doGetAuthorizationInfo(PrincipalCollectionプリンシパル)を保護しました
		// TODO自動生成されたメソッドスタブの
		リターンはnull; 
	} 

}

ログイン認証方式はdoGetAuthenticationInfoで書かれています

		//取得トークン
		トークンUsernamePasswordToken token2 =(UsernamePasswordToken); 
		//ユーザー名を取得
		文字列のuserName = token2.getUsernameを(); 
		//パスワードの取得
		文字列のPWD =新しい新しいString(token2.getPasswordを()); 
		//ここで私が使用していますpuls3.0-MyBatisの
		//クエリでは、オブジェクト
		queryWrapper <ユーザー> queryWrapper新しい新しいqueryWrapper =(); 
		//クエリユーザー
		queryWrapper.eq( "名前"、userNameに) .OR()EQ( "電話"、ユーザー名); 
		//クエリ
		ユーザーユーザー= iUserService.getOne(queryWrapper); 
		//チェックバックオブジェクトである空の
		IF(CommonUtil.isBlank(ユーザ)){ 
			//未知のスロー異常アカウント
			スロー新しい新しいUnknownAccountException(); 
		} 
		//チェックバックオブジェクトコードとパスワードが等しくない
		(!CommonUtil.isEquals(user.getPwd()場合 、PWD)){ 
			//スロー文書正しくない例外
			スロー新しい新IncorrectCredentialsException(); 
		}
		//ユーザーに渡され、上述のが存在し、パスワードに等しい
		検証が成功し// 
		SecurityUtils.getSubject()のgetSession()のsetAttribute(Constant.SESSION_USER_KEY、ユーザ); ... 
		//戻り、ユーザ情報シロ
		オーバーパストークン//パスワードは、パスワードで同じ情報の検証パスを使用する必要があり、暗号化されたパスワードは、上のパスを暗号化する必要があります

		リターン新しいSimpleAuthenticationInfo(ユーザー、user.getPwd( )、のgetName());

あなたは、対応する方法プラスControllerfでは、権限を設定したい場合

@RequiresPermissions( "/システム/ユーザー/リスト")

書き込みにdoGetAuthorizationInfoの道の中で

//は、単純な認証情報オブジェクト作成
)SimpleAuthorizationInfo simpleAuthorizationInfo =新しい新しいSimpleAuthorizationInfoを(; 
//付与権限
simpleAuthorizationInfo.addStringPermission( "/システム/ユーザー/リスト"); 

リターンsimpleAuthorizationInfo。

  

不正コントローラエラーにアクセスする場合、すべてのコントローラは、@RequiresPermissionsノートの後に追加された場合。

おすすめ

転載: www.cnblogs.com/mowen120/p/11908061.html