5

添加@With(Secure.class)到控制器会阻止所有未经身份验证的访问。有没有办法只为某些操作启用它,或者在控制器上启用它后排除某些操作?

4

5 回答 5

9

你不能用安全模块做到这一点。正如 Niels 所说,安全模块与其说是解决方案,不如说是一个例子。您可以使用 @Before 注释构建自己的安全系统。这是一个例子:

public class Admin extends Controller {

@Before(unless={"login", "authenticate", "logout", "otherMethod"})
void checkAccess() {
    // check the cookie
}

public void login() {
    render();
}

public void authenticate(String email, String password) {
    // check the params and set a value in the cookie
}

public void logout() {
    // delete cookie
}

我建议您阅读安全模块的源代码。

于 2010-12-02T10:48:09.823 回答
4

从那以后,我发现我早期的@Public解决方案有些限制,因为它无法解决继承的操作。我改为使用类级注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AllowGuest {

    String[] value();
}

并将此代码添加到Secure.checkAccess()方法的开头:

AllowGuest guest = getControllerInheritedAnnotation(AllowGuest.class);
if (guest != null) {
    for (String action : guest.value()) {
        if (action.equals(request.actionMethod))
            return;
    }
}

可以这样使用:@AllowGuest({"list","view"})

这使得允许访问本地和继承的操作以及查看控制器中的哪些操作是不安全的变得容易。

于 2010-12-17T17:17:06.900 回答
2

删除@With(Secure.class)控制器的注释并将这段代码添加到控制器中。

@Before(unless={"show"})
static void checkAccess() throws Throwable {
    Secure.checkAccess();
}

show您需要公开的操作在哪里。

于 2013-09-25T13:06:29.340 回答
1

为了得到我想要的东西,我复制了Check注释并创建了一个Public注释。

package controllers;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Public {

}

然后我将这两行添加到开头Secure.checkAccess

if (getActionAnnotation(Public.class) != null)
    return;

现在,通过向控制器添加注释,With(Secure.class)可以在不登录的情况下访问控制器中的操作。@Public

于 2010-12-03T17:15:34.320 回答
0

您可以在安全控制器的@Before-Tag 中设置该值,除非或仅。安全模块与其说是解决方案,不如说是一个例子。

于 2010-12-02T07:35:06.857 回答