NutzCN Logo
问答 @ChainBy(args="mvc/nutzbook-mvc-chain.js",type = MyActionChainMaker.class)不能共存吗?
发布于 2936天前 作者 shusheng 2613 次浏览 复制 上一个帖子 下一个帖子
标签:
@ChainBy(args="mvc/nutzbook-mvc-chain.js",type = MyActionChainMaker.class)

写在一起后报错了

org.nutz.lang.born.BorningException: Fail to born 'net.wendal.nutzbook.mvc.MyActionChainMaker'
 by args: [
  @(mvc/nutzbook-mvc-chain.js)] becasue: No suitable Constructor or Factory Method!!
	at org.nutz.lang.Mirror.born(Mirror.java:991) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.Loadings.evalObj(Loadings.java:322) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.NutLoading.createChainMaker(NutLoading.java:264) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:169) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:108) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4563) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5205) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393) [catalina.jar:9.0.0.M11]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_102]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_102]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_102]
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
29 回复

把type去掉

@wendal

public class ThunderChainMaker extends KerboresActionChainMaker {

	private Log log = Logs.get();

	@Override
	public ActionChain eval(final NutConfig config, final ActionInfo ai) {
		List<Processor> list = normalList();

		List<AuthorizingAnnotationMethodInterceptor> interceptors = new ArrayList<AuthorizingAnnotationMethodInterceptor>();

		interceptors.add(new ThunderPermissionAnnotationMethodInterceptor());
		interceptors.add(new ThunderRoleAnnotationMethodInterceptor());

		addBefore(list, ActionFiltersProcessor.class, new NutShiroProcessor(interceptors, ThunderRequiresPermissions.class, ThunderRequiresRoles.class));

做一个自定义的注解类,该怎么加入到chain中?

args是type的构造方法, 既然ThunderChainMaker只有默认无参钩子方法,自然就不能有args

@wendal

public class MyNutShiroMethodInterceptor extends AnnotationsAuthorizingMethodInterceptor implements MethodInterceptor{

	public MyNutShiroMethodInterceptor(Collection<AuthorizingAnnotationMethodInterceptor> interceptors) {
		super.getMethodInterceptors().addAll(interceptors);
	}

	public MyNutShiroMethodInterceptor() {
		super.getMethodInterceptors().add(new MyAnnotationMethodIntercepter());
	}

在拦截器列表中加入了我自定义的MyAnnotationMethodIntercepter ,为什么没有拦截,还有什么地方需要配置吗?

debug一下NutShiroProcessor

@wendal

public class ForumShiroProcessor extends NutShiroProcessor {

	protected MyNutShiroMethodInterceptor interceptor;
	
	public ForumShiroProcessor(Collection<AuthorizingAnnotationMethodInterceptor> interceptors) {
        super(interceptors);
    }
	public ForumShiroProcessor() {
		interceptor = new MyNutShiroMethodInterceptor();
    }
	@Override
	protected void whenUnauthenticated(ActionContext ac, UnauthenticatedException e) throws Exception {
		ac.getResponse().addHeader("loginStatus", "accessDenied");
		NutMap re = new NutMap().setv("ok", 0);
		NutShiro.rendAjaxResp(ac.getRequest(), ac.getResponse(), re);
	}
}

我继承了NutShiroProcessor,在无参数构造函数中调用了我的拦截器

@wendal 服务器启动时有调用到无参数构造函数

addBefore(list, ActionFiltersProcessor.class, new NutShiroProcessor(interceptors, ThunderRequiresPermissions.class, ThunderRequiresRoles.class));

但你new的是NutShiroProcessor而非ForumShiroProcessor

@wendal 我用的代码是nutzbook的,这段代码在服务器启动时没有执行呀,在mainmodule那里没有调用

public class MyActionChainMaker implements ActionChainMaker {

	public ActionChain eval(NutConfig config, ActionInfo ai) {
		List<Processor> list = normalList();
		
		// 首先,在所有请求之前,插入2个
		list.add(0, new LogTimeProcessor());
		list.add(1, new DailyUniqueUsersProcessor());
		
//		ArrayList<AuthorizingAnnotationMethodInterceptor> temp = new ArrayList<>();
//		temp.add(new MyAnnotationMethodIntercepter());
		addBefore(list, ActionFiltersProcessor.class, new ForumShiroProcessor());
		
		// 最后是专门负责兜底的异常处理器
		Processor error = new FailProcessor();
		return new NutActionChain(list, error, ai);
	}

@ChainBy要关联上啊.

PS: nutzbook里面的MyActionChainMaker并没有实际用上.

@wendal

//@ChainBy(args="mvc/nutzbook-mvc-chain.js")
@ChainBy(type = MyActionChainMaker.class, args = {})

这样嘛?

@wendal

14:24:25.051 WARN  (FailProcessor.java:28) process - Error@/forum/listData :
java.lang.NullPointerException
	at org.apache.tomcat.util.buf.B2CConverter.getCharset(B2CConverter.java:63) ~[tomcat-util.jar:9.0.0.M11]
	at org.apache.catalina.connector.Request.setCharacterEncoding(Request.java:1618) ~[catalina.jar:9.0.0.M11]
	at org.apache.catalina.connector.RequestFacade.setCharacterEncoding(RequestFacade.java:329) ~[catalina.jar:9.0.0.M11]
	at javax.servlet.ServletRequestWrapper.setCharacterEncoding(ServletRequestWrapper.java:106) ~[servlet-api.jar:4.0.EDR-b01]
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:25) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at net.wendal.nutzbook.mvc.DailyUniqueUsersProcessor.process(DailyUniqueUsersProcessor.java:48) ~[classes/:?]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at net.wendal.nutzbook.mvc.LogTimeProcessor.process(LogTimeProcessor.java:19) ~[classes/:?]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at net.wendal.nutzbook.mvc.DailyUniqueUsersProcessor.process(DailyUniqueUsersProcessor.java:48) ~[classes/:?]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) ~[nutz-1.r.59-SNAPSHOT.jar:?]
	at net.wendal.nutzbook.mvc.LogTimeProcessor.process(LogTimeProcessor.java:19) ~[classes/:?]
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:68) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198) [nutz-1.r.59-SNAPSHOT.jar:?]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [catalina.jar:9.0.0.M11]
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) [druid-1.0.25.jar:1.0.25]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [catalina.jar:9.0.0.M11]
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) [shiro-web-1.3.2.jar:1.3.2]
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) [shiro-web-1.3.2.jar:1.3.2]
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) [shiro-core-1.3.2.jar:1.3.2]
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) [shiro-core-1.3.2.jar:1.3.2]
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) [shiro-core-1.3.2.jar:1.3.2]
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) [shiro-web-1.3.2.jar:1.3.2]
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro-web-1.3.2.jar:1.3.2]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:9.0.0.M11]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [catalina.jar:9.0.0.M11]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784) [tomcat-coyote.jar:9.0.0.M11]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.0.M11]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802) [tomcat-coyote.jar:9.0.0.M11]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-coyote.jar:9.0.0.M11]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.0.M11]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_102]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_102]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.0.M11]
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
14:24:25.057 DEBUG (RedisCache.java:43) get - GET name=shiro-activeSessionCache key=6b3vda5ff4io4qu6ju0bjo7n6b
十二月 08, 2016 2:24:25 下午 org.apache.catalina.core.StandardWrapperValve invoke

改了过后,访问服务器就报错了

@wendal

var chain={
	"default" : {
		"ps" : [
		      "net.wendal.nutzbook.mvc.LogTimeProcessor",
		      "net.wendal.nutzbook.mvc.DailyUniqueUsersProcessor",
		      "org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
		      "org.nutz.mvc.impl.processor.EncodingProcessor",
		      "org.nutz.mvc.impl.processor.ModuleProcessor",
		      "net.wendal.nutzbook.shiro.forum.ForumShiroProcessor",
		      "org.nutz.mvc.impl.processor.ActionFiltersProcessor",
		      "org.nutz.mvc.impl.processor.AdaptorProcessor",
		      "org.nutz.mvc.impl.processor.MethodInvokeProcessor",
		      "org.nutz.mvc.impl.processor.ViewProcessor",
		      
		      ],
		"error" : 'org.nutz.mvc.impl.processor.FailProcessor'
	}
};

配置文件中加上我自己的 ForumShiroProcessor后,就是没有效果,debug模式下看到添加了我的拦截器,但是就是在module里面没有拦截到

@Chain没用默认的ChainMaker之后, 这文件没用了

你搞清楚这几个东西关系啊...

@ChainBy有两个参数, type 和 args

type是ChainMaker的实现类, args是ChainMaker实现类的构造方法参数

既然实现类换了, 行为就按新的走了

话说, 你直接改nutzbook-mvc-chain.js就可以了嘛... 不需要MyActionChainMaker的.

@wendal 上面那个配置文件就是加了我自己的ForumShiroProcessor,服务器启动的时候,我debug了,在拦截器列表中有我的拦截器,但是在真正访问注解拦截时,发现里面我自己的拦截器没有了,却多了一个NutzPermissionAnnotationMethodInterceptor的拦截器

因为跟父类属性重名了, NutShiroProcessor 里面就有interceptor.

protected MyNutShiroMethodInterceptor interceptor; // 去掉

因为跟父类属性重名了, NutShiroProcessor 里面就有interceptor.

protected MyNutShiroMethodInterceptor interceptor; // 去掉

@wendal

	@MyPermissionAnnotation(value ="aa:bb",descr="aaaaaaaaaaaa")
//	@RequiresPermissions(value="bb:cc")

对我自己定义的注解视而不见,呵呵,是少配置了哪里吧?

@wendal

	@MyPermissionAnnotation(value ="aa:bb",descr="aaaaaaaaaaaa")
//	@RequiresPermissions(value="bb:cc")

对我自己定义的注解视而不见,呵呵,是少配置了哪里吧?

@qq_97f5068e 看清楚父类的那几个构造方法

@wendal

public class MyAnnoHandler extends RoleAnnotationHandler{

	public MyAnnoHandler() {
		setAnnotationClass(MyPermissionAnnotation.class);
	}

	@Override
	public void assertAuthorized(Annotation a) throws AuthorizationException {
		if (!(a instanceof MyPermissionAnnotation))
			return;

		MyPermissionAnnotation rpAnnotation = (MyPermissionAnnotation) a;
		String[] roles_ = rpAnnotation.value();
		Subject subject = getSubject();

//		final String[] roles = new String[roles_.length];

//		Lang.each(roles_, new Each<InstalledRole>() {
//
//			@Override
//			public void invoke(int index, InstalledRole ele, int length) throws ExitLoop, ContinueLoop, LoopException {
//				roles[index] = ele.getName();
//			}
//		});

		if (roles_.length == 1) {
			subject.checkRole(roles_[0]);
			return;
		}
		if (Logical.AND.equals(rpAnnotation.logical())) {
			getSubject().checkRoles(roles_);
			return;
		}
		if (Logical.OR.equals(rpAnnotation.logical())) {
			boolean hasAtLeastOneRoles = false;
			for (String role : roles_)
				if (getSubject().hasRole(role))
					hasAtLeastOneRoles = true;
			if (!hasAtLeastOneRoles)
				getSubject().checkRole(roles_[0]);
		}
	}
}

现在拦截列表里面有我的拦截器了,但是就是不拦截我的注解类MyPermissionAnnotation,不知道什么原因

NutShiroProcessor的子类要为 annotations 属性赋值哦

@wendal

public static boolean match(Method method) {
        if (method.getAnnotation(RequiresRoles.class) != null 
                || method.getAnnotation(RequiresAuthentication.class) != null
                || method.getAnnotation(RequiresGuest.class) != null
                || method.getAnnotation(RequiresPermissions.class) != null
                || method.getAnnotation(RequiresUser.class) != null
                || method.getAnnotation(NutzRequiresPermissions.class) != null) {
            return true;
        }
        return false;
    }

这里一直少了我自定义的类型,呵呵,导致我一直扫描不到

@wendal 可以在NutShiroProcessor里面加一个成员变量,注解的类,必须赋值,这样就不会忘了,扫描时就取注解类进行判断就可以了

添加回复
请先登陆
回到顶部