NutzCN Logo
问答 shiro配置filter怎么貌似没有生效啊。
发布于 2222天前 作者 zp8821138 4961 次浏览 复制 上一个帖子 下一个帖子
标签:

配置文件

[filters]
rememberAuthFilter = com.kanq.shiro.filter.RememberAuthenticationFilter
rememberAuthFilter.loginUrl = /platform/login
logout.redirectUrl = /platform/login

[urls]
/platform/doLogin = anon
/platform/login/captcha = anon
/platform/login/logout = anon
/assets/** = anon
/** = anon
/platform/** = rememberAuthFilter

代码是

public class RememberAuthenticationFilter extends FormAuthenticationFilter {

	@Override
	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        if (isLoginRequest(request, response)) {
            return true;
        } else {
            Subject subject = getSubject(request, response);
            // If principal is not null, then the user is known and should be allowed access.
            return subject.getPrincipal() != null;
        }
    }
	@Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
		System.out.println("###########################################################################");
        saveRequestAndRedirectToLogin(request, response);
        return false;
    }

}

我没登录的时候输入的路径是/platform/home ,代码是

    @At("")
    @Ok("beetl:/home.html")
    @RequiresAuthentication
    public void home(HttpServletRequest req) {
    	
    }

返回到的是登陆页面 但是onAccessDenied貌似没有进入,我的理解应该是要进入这个方法的。。我理解有误? 那onAccessDenied这个什么情况才会触发呢。。
另外我输入/platform/sys/user/add可以进入添加用户页面,代码如下

	@At("/add")
	@Ok("beetl:/base/user/add.html")
	public void add() {
		
	}

是因为没有添加 @RequiresAuthentication标签的原因吗?

13 回复

urls里面的配置是有顺序的,从上到下进行匹配

这个我知道啊 配置的/platform/** = rememberAuthFilter /platform/home这个会被拦截 那/platform/sys/user/add也应该会被拦截把 因为顺序的最后是/platform/** 所以/platform下的所有方法都会被拦截把

你并不知道

[urls]
/platform/doLogin = anon
/platform/login/captcha = anon
/platform/login/logout = anon
/assets/** = anon
/** = anon  ## 这个配置之后的所有配置都没有意义的, 它已经匹配了所有URL
/platform/** = rememberAuthFilter

但是我确实输入的 /platform/home 确实被拦截了,而且直接跳转登陆的url中去了 那是@RequiresAuthentication这个注解的原因? 这个配置了用了哪个过滤器? 他怎么知道要调到/platform/login中的呢?

@RequiresAuthentication是NutShiroProcessor拦截的

好吧 是的,@RequiresAuthentication不是shiro里面的注解吗,shiro应该有默认的拦截实现把 是啥啊。

是。。AnnotationsAuthorizingMethodInterceptor

shiro并没有"默认的拦截实现"

有吧 AnnotationsAuthorizingMethodInterceptor不是默认的吗,你的NutShiroMethodInterceptor不是继承这个来的吗。

那是要继承才能用的, 不是"默认的"

你的意思是假如我只用shiro ,用的默认的配置文件,方法上加@RequiresAuthentication是无效的?

不会用AnnotationsAuthorizingMethodInterceptor这个拦截器进行拦截?

shiro没法直接拦截一个"带shiro注解"的方法的调用, 需要额外代码配合才可以的

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