在一个Nutzboot项目中,引用了shiro的方法注解,一个加了@RequiresAuthentication的方法,调用时如果没有登录,会返回"404-找不到路径"?
按说这是一个检查权限的注解,期望能返回 "401 Unauthorized"
在一个Nutzboot项目中,引用了shiro的方法注解,一个加了@RequiresAuthentication的方法,调用时如果没有登录,会返回"404-找不到路径"?
按说这是一个检查权限的注解,期望能返回 "401 Unauthorized"
这个类么? 怎么理解, 是因为跳转到defaultUrl, 然后没找到"/user/login", 才404的是么?
public class NutShiro {
public static String DefaultLoginURL = "/user/login";
public static NutMap DefaultUnauthorizedAjax = new NutMap().setv("ok", false).setv("msg", "user.require.auth").setv("type", "user.require.auth");
public static NutMap DefaultOtherAjax = new NutMap().setv("ok", false).setv("msg", "user.require.login").setv("type", "user.require.login");
public static NutMap DefaultUnauthenticatedAjax = new NutMap().setv("ok", false).setv("msg", "user.require.unauthorized").setv("type", "user.require.unauthorized");
public static String DefaultNoAuthURL; // 默认与DefaultLoginURL一致
public static String SessionKey = "me";
public static String AjaxEncode = Encoding.UTF8;
public static final String DEFAULT_CAPTCHA_PARAM = "captcha";
public static boolean isAjax(ServletRequest req) {
String value = ((HttpServletRequest)req).getHeader("X-Requested-With");
if (value != null && "XMLHttpRequest".equalsIgnoreCase(value.trim())) {
return true;
}
return false;
}
public static void rendAjaxResp(ServletRequest req, ServletResponse resp, Object re) {
try {
if (AjaxEncode != null)
((HttpServletResponse)resp).setCharacterEncoding(AjaxEncode);
new UTF8JsonView(JsonFormat.compact()).render((HttpServletRequest)req, (HttpServletResponse)resp, re);
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
@SuppressWarnings("unchecked")
public static Set<String>[] scanRolePermissionInPackage(String pkg, boolean publicOnly) {
Set<String> roles = new HashSet<String>();
Set<String> permissions = new HashSet<String>();
for (Class<?> klass : Scans.me().scanPackage(pkg)) {
Method[] methods = publicOnly ? klass.getMethods() : klass.getDeclaredMethods();
for (Method method : methods) {
RequiresRoles rr = method.getAnnotation(RequiresRoles.class);
if (rr != null && rr.value().length > 0) {
for (String role : rr.value()) {
roles.add(role);
}
}
RequiresPermissions pr = method.getAnnotation(RequiresPermissions.class);
if (pr != null && pr.value().length > 0) {
for (String permission : pr.value()) {
permissions.add(permission);
}
}
NutzRequiresPermissions pr2 = method.getAnnotation(NutzRequiresPermissions.class);
if (pr2 != null && pr2.value().length > 0) {
for (String permission : pr2.value()) {
permissions.add(permission);
}
}
}
}
return new Set[]{roles, permissions};
}
}