java - 在 play1.2.4 应用程序中使用 ssl

标签 java ssl https playframework

我需要在我的 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/

相关文章:

javascript - 如何在node.js中发送表单数据

java - Jersey 2.0 的 Restful Web 服务

php - 在 Ubuntu 上下载 https ://repo. packagist.org 证书问题时出现 curl 错误 60?

flash - 从闪存安全访问 Amazon S3

ssl - 我可以将主机的多个替代证书放在一个证书文件中吗?

php - 使用路由将 htaccess 重写为 https

java - 为什么 System.console() 对于命令行应用程序返回 null?

java - 根据数字将数组排序为 3 个桶

java - 可用于调试 Java 应用程序中的生产问题的工具

amazon-web-services - 弹性负载均衡器后面的 ec2 实例是否需要 https(即 ssl 证书)?