Shro設定スキップ権限検証
需要
開発環境では、シロの権限検証をスキップする場合がありますので、簡単な構成でシロの権限検証をスキップしたいです.権限検証をスキップする原理は、書き換えます.
@RequiresPermissions処理クラス
org.apaache.shiro.authz.aop.Permission AnnotationHandlerでこの注釈を処理します.これは私達が書き換えるタイプです.ロゴログログに交換したいです.
クラック
実現した方法が見つかったら、自分の実現クラスを一つ入れれば、シオの権限をスキップできます.しかし、テストと開発環境だけで解決するためには、構成を使って実現する必要があります.
1.サイロスイッチのスキップ設定
まず、springの配置にspring.profiles.activeを入れ、同時にxfs.shiro.skyipShroをtrueとして配置します.解読する時、現在の運行環境とskyShroによって、shiroをスキップするかどうかを判断します.
ログにロゴを書いて、深刻な生産問題を防止します.
Annotations AuthorigingMethodInterceptrという抽象的なクラスの実現には、注釈に対するブロッキングが追加されています.
環境を判断して、Shro権限検証をスキップして、テストと開発環境のみで有効になります.また、配置をオープンする必要があります.
開発環境では、シロの権限検証をスキップする場合がありますので、簡単な構成でシロの権限検証をスキップしたいです.権限検証をスキップする原理は、書き換えます.
@RequiresPermissions処理クラス
org.apaache.shiro.authz.aop.Permission AnnotationHandlerでこの注釈を処理します.これは私達が書き換えるタイプです.ロゴログログに交換したいです.
/**
* @{@link org.apache.shiro.authz.annotation。
* , , , Subject
* 。
*
* @since 0.9.0
*/
public class PermissionAnnotationHandler extends AuthorizingAnnotationHandler {
/**
* Subject
, ,
* AuthorizingException
。
*
*/
public void assertAuthorized(Annotation a) throws AuthorizationException {
if (!(a instanceof RequiresPermissions)) return;
RequiresPermissions rpAnnotation = (RequiresPermissions) a;
//
String[] perms = getAnnotationValue(a);
//
Subject subject = getSubject();
//
if (perms.length == 1) {
subject.checkPermission(perms[0]);
return;
}
//
if (Logical.AND.equals(rpAnnotation.logical())) {
getSubject().checkPermissions(perms);
return;
}
//
if (Logical.OR.equals(rpAnnotation.logical())) {
// Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
boolean hasAtLeastOnePermission = false;
for (String permission : perms) if (getSubject().isPermitted(permission)) hasAtLeastOnePermission = true;
// Cause the exception if none of the role match, note that the exception message will be a bit misleading
if (!hasAtLeastOnePermission) getSubject().checkPermission(perms[0]);
}
}
}
AopAllianceAnnotations AuthorizingMethodInterceptorを通じてブロック処理に参加し、AuthortionAttributeSourceAdvisourcerをSpring Bernに注入するように配置しました.Authoriatization AttributeSourceAisorによってStation Portistor AdtMartication AdcutMadtMadtMadtMadtMadtMadtMadtMadtMadtPortitititititititクラック
実現した方法が見つかったら、自分の実現クラスを一つ入れれば、シオの権限をスキップできます.しかし、テストと開発環境だけで解決するためには、構成を使って実現する必要があります.
1.サイロスイッチのスキップ設定
まず、springの配置にspring.profiles.activeを入れ、同時にxfs.shiro.skyipShroをtrueとして配置します.解読する時、現在の運行環境とskyShroによって、shiroをスキップするかどうかを判断します.
spring:
# dev|test|prod
profiles:
active: dev
application:
name: system
xfs:
shiro:
skipShiro: true # , shiro , ,
2.自分の@RequiresPermissionsの処理方法を書き直します.ログにロゴを書いて、深刻な生産問題を防止します.
package cn.lanhi.auth.starter.interceptor;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.aop.PermissionAnnotationHandler;
import java.lang.annotation.Annotation;
/**
*
*
* @author : snx [email protected]
* @date : 2020-06-16 11:39
*/
@Slf4j
public class ShiroPermissionHandler extends PermissionAnnotationHandler {
public ShiroPermissionHandler() {
super();
log.warn(" PermissionHandler, , ");
}
/**
* , log,
*
* @param a
* @throws AuthorizationException
*/
@Override
public void assertAuthorized(Annotation a) throws AuthorizationException {
if (!(a instanceof RequiresPermissions)) return;
// , , json
log.warn(" !! :{}", JSON.toJSONString(getAnnotationValue(a)));
}
}
3.コメントプロセッサの設定Annotations AuthorigingMethodInterceptrという抽象的なクラスの実現には、注釈に対するブロッキングが追加されています.
package org.apache.shiro.spring.security.interceptor;
public class AopAllianceAnnotationsAuthorizingMethodInterceptor
extends AnnotationsAuthorizingMethodInterceptor implements MethodInterceptor {
//Shiro
public AopAllianceAnnotationsAuthorizingMethodInterceptor() {
List<AuthorizingAnnotationMethodInterceptor> interceptors =
new ArrayList<AuthorizingAnnotationMethodInterceptor>(5);
AnnotationResolver resolver = new SpringAnnotationResolver();
interceptors.add(new RoleAnnotationMethodInterceptor(resolver));
// ,
interceptors.add(new PermissionAnnotationMethodInterceptor(resolver));
interceptors.add(new AuthenticatedAnnotationMethodInterceptor(resolver));
interceptors.add(new UserAnnotationMethodInterceptor(resolver));
interceptors.add(new GuestAnnotationMethodInterceptor(resolver));
setMethodInterceptors(interceptors);
}
........
}
では、自分でAopAlliance Annotations AuthorigingMethodInterceptorを継承して、自分の属性を取得して、値を修正します.package cn.lanhi.auth.starter.shiro;
import com.xfs.auth.starter.interceptor.ShiroPermissionHandler;
import org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor;
import org.apache.shiro.authz.aop.PermissionAnnotationMethodInterceptor;
import org.apache.shiro.spring.security.interceptor.AopAllianceAnnotationsAuthorizingMethodInterceptor;
import java.util.List;
import java.util.stream.Collectors;
/**
* shiro
*
* @author : snx [email protected]
* @date : 2020-06-16 11:34
*/
public class ShiroMethodInterceptor extends AopAllianceAnnotationsAuthorizingMethodInterceptor {
public ShiroMethodInterceptor() {
super();
}
/**
* shiro RequirePremissions
*/
public ShiroMethodInterceptor skipPremissionHandler() {
List<AuthorizingAnnotationMethodInterceptor> interceptors = this.getMethodInterceptors().stream()
.filter(authorizingAnnotationMethodInterceptor ->
!(authorizingAnnotationMethodInterceptor instanceof PermissionAnnotationMethodInterceptor))
.collect(Collectors.toList());
PermissionAnnotationMethodInterceptor interceptor = new PermissionAnnotationMethodInterceptor();
// !
interceptor.setHandler(new ShiroPermissionHandler());
interceptors.add(interceptor);
setMethodInterceptors(interceptors);
return this;
}
}
4.shiroAopを書き換えるpackage org.apache.shiro.spring.config;
/**
* shiro AOP
* @since 1.4.0
*/
@Configuration
public class ShiroAnnotationProcessorConfiguration extends AbstractShiroAnnotationProcessorConfiguration{
@Bean
@DependsOn("lifecycleBeanPostProcessor")
protected DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return super.defaultAdvisorAutoProxyCreator();
}
@Bean
protected AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
return super.authorizationAttributeSourceAdvisor(securityManager);
}
}
--------------------
--------------------
--------------------
AuthorizationAttributeSourceAdvisor AOP . ,
/**
* Create a new AuthorizationAttributeSourceAdvisor.
*/
public AuthorizationAttributeSourceAdvisor() {
setAdvice(new AopAllianceAnnotationsAuthorizingMethodInterceptor());
}
--------------------
--------------------
--------------------
5.カバーShro Bean環境を判断して、Shro権限検証をスキップして、テストと開発環境のみで有効になります.また、配置をオープンする必要があります.
/**
* Shiro ,
*
* @param securityManager
* @return
*/
@Bean("authorizationAttributeSourceAdvisor")
@Primary
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
SecurityManager securityManager,
Environment environment,
@Value("${xfs.shiro.skipShiro:false}") boolean skipShiro) {
//
String profile = environment.getRequiredProperty("spring.profiles.active");
// Bean
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
// shiro
if (skipShiro && ("dev".equals(profile) || "test".equals(profile))) {
// shiro
advisor.setAdvice(new ShiroMethodInterceptor().skipPremissionHandler());
}
return advisor;
}
ここまでで、大成功です.転載は出所を明記してください.