我需要在我的 Web 应用程序中使用 https 连接器发出某些请求。假设我的 CustomerController
中有两种方法需要发送敏感信息。此外,我使用 controllers.Secure.Security
的子类用于身份验证(通过实现 authenticate(..)
,因此登录信息必须通过 ssl。
我浏览了 documentation on configuring ssl .从SO帖子中,发现我需要一个 Controller 来确保ssl。
class EnsureSSL extends Controller {
@Before
static void verifySSL() {
if(!request.secure) {
redirect("https://" + request.host + request.url);
}
}
}
现在,我需要在任何发送敏感信息的请求上使用它。我想在 login /authentication requests
上使用它以及CustomerController
的两个敏感方法.
这样做的正确方法是什么?@With(..) 只能用于整个类。所以我不能只让 CustomerController 类中的某些方法使用 SSL。如果我限制整个类(class),那不是增加了负担吗?
想要 CustomerController.java 的方法级修饰
class CustomerController extends Controller{
@With(EnsureSSL.class)//cannot do this!
public static void sensitiveMethod1(...){
...
}
@With(EnsureSSL.class)
public static void sensitiveMethod2(...){
...
}
public static void freeForAllToSee(...){
...
}
}
Security.java 的类级装饰
@With(EnsureSSL.class)
class Security extends controllers.Secure.Security {
static boolean authenticate(String username, String password) {
...
}
}
我想知道我是否在错误的轨道上..有人可以建议吗?
最佳答案
您可以为此创建自己的注释:
package utils;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequiresSSL {
}
现在像这样创建 Controller 方法:
@With(EnsureSSL.class)
class CustomerController extends Controller{
@RequiresSSL
public static void sensitiveMethod1(...){
...
}
@RequiresSSL
public static void sensitiveMethod2(...){
...
}
public static void freeForAllToSee(...){
...
}
}
并在检查之前将您的 EnsureSSL 修改为:
class EnsureSSL extends Controller {
@Before
static void verifySSL() {
if((!request.secure)
&& (request.invokedMethod.getAnnotation(RequiresSSL.class) != null)) {
redirect("https://" + request.host + request.url);
}
}
}
关于java - 在 play1.2.4 应用程序中使用 ssl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9543083/