简体   繁体   English

在方法体内注入HttpServletRequest(Errai服务实现)

[英]Inject HttpServletRequest inside method body (Errai service implementation)

How to inject HttpServletRequest in a Errai service implementation (server side) for use with code like this: 如何在Errai服务实现(服务器端)中注入HttpServletRequest以与以下代码一起使用:

@Override
public void login(String username, String password, boolean rememberMe) {
    try {
        HttpServletRequest request = null; // <---- Inject here...
        String host = request.getRemoteHost();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe, host);
        try {
            Subject subject = SecurityUtils.getSubject();
            loginWithNewSession(token, subject);
            subject.login(token);
        } catch (AuthenticationException e) {
            throw new IllegalArgumentException("Service could not authenticate caller with the given authorization credentials.");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Something went wrong with the login request");
    }       
}

You can use the static method RpcContext.getServletRequest() in this case. 在这种情况下,可以使用静态方法RpcContext.getServletRequest() Internally, it retrieves the HttpServletRequest from a ThreadLocal Message object. 在内部,它从ThreadLocal Message对象检索HttpServletRequest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM