简体   繁体   English

SessionScoped cdi bean注入了过滤器-是否为不同的会话重新注入?

[英]SessionScoped cdi bean injected in filter - not re-inject for different session?

I have this issue, and I'm not sure if this is "expected" behaviour, but here is my problem: 我遇到了这个问题,我不确定这是否是“预期的”行为,但这是我的问题:

I have a Http Filter: 我有一个Http过滤器:

@WebFilter(filterName = "VerificationFilter", urlPatterns = {"/activation/*"})
public class VerificationFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(VerificationFilter.class);
    private static final String ACTIVATION_CODE_PARAM_KEY = "vc";
    @Inject
    private UserInfo userInfo;
    @Inject
    private ActivationInfo activationInfo;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.debug("************VerificatoniFilter initializing*************");
    }

    /**
     * This filter filters requests by path - anything in the /activate/ namespace will
     * be filtered to first determine if the user has already passed through this filter once.
     * If the user has been "filtered" and the validation code was deemed to be valid, navigation will
     * be permitted to pass through.  If not, then they will be redirected to an error page
     * 
     *  If the user has not yet been filtered, (determined by the presence of details available in the user's
     *  current session), then the filter will check the validation code for validity and/or allow or reject
     *  access.
     *
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (!activationInfoPopulated()) {
            String activationCode = request.getParameter(ACTIVATION_CODE_PARAM_KEY);
            if (StringUtils.isEmpty(activationCode)) {
                throwActivationInvalid();
            } else {
                try {
                    ActivationService aService = new ActivationService();
                    ClPersonInfo info = aService.findActivationCodeUser(activationCode);
                    if (info == null) {
                        throwActivationInvalid();
                    }
                    userInfo.setClPersonInfo(info);
                    setActivationInfoPopulated();
                    setActivationValid();
                } catch (ServiceUnavailableException sue) {
                    throw new IamwebApplicationException(sue.getMessage());
                } 
            }
        } else {
            // if the validationCode is not valid, send the user directly to error page.  Else, continue...
            if (!activationInfo.getValidationCodeIsValid()) {
                throw new ActivationCodeInvalidException();
            } 
        }
        // if all is good, continue along the chain
        chain.doFilter(request, response);
    }

    private boolean activationInfoPopulated() {
        return (activationInfo.getValidationCodeChecked());
    }

    private void setActivationInfoPopulated() {
        activationInfo.setValidationCodeChecked(Boolean.TRUE);
    }

    private void setActivationValid() {
        activationInfo.setValidationCodeIsValid(Boolean.TRUE);
    }

    private void setActivationInvalid() {
        activationInfo.setValidationCodeIsValid(Boolean.FALSE);
    }

    private void throwActivationInvalid() throws ActivationCodeInvalidException {
        setActivationInfoPopulated();
        setActivationInvalid();
        throw new ActivationCodeInvalidException();
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    /**
     * @return the activationInfo
     */
    public ActivationInfo getActivationInfo() {
        return activationInfo;
    }

    /**
     * @param activationInfo the activationInfo to set
     */
    public void setActivationInfo(ActivationInfo activationInfo) {
        this.activationInfo = activationInfo;
    }

    /**
     * @return the userInfo
     */
    public UserInfo getUserInfo() {
        return userInfo;
    }

    /**
     * @param userInfo the userInfo to set
     */
    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }   
}

and the UserInfo and ActivationInfo are both @SessionScoped as follows: 和UserInfo和ActivationInfo都为@SessionScoped,如下所示:

@Named("activationInfo")
@SessionScoped
public class ActivationInfo implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(ActivationInfo.class);
    private static final long serialVersionUID = -6864025809061343463L;
    private Boolean validationCodeChecked = Boolean.FALSE;
    private Boolean validationCodeIsValid = Boolean.FALSE;
    private String validationCode;

    @PostConstruct
    public void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        logger.debug("ActivationInfo being constructed for sessionId:  " + (session == null ? " no session found " : session.getId()));
    }

and

@Named("userInfo")
@SessionScoped
public class UserInfo implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(UserInfo.class);
    private static final long serialVersionUID = -2137005372571322818L;
    private ClPersonInfo clPersonInfo;
    private String password;

    /**
     * @return the clPersonInfo
     */
    public ClPersonInfo getClPersonInfo() {
        return clPersonInfo;
    }

    @PostConstruct
    public void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        logger.debug("UserInfo being constructed for sessionId:  " + (session == null ? " no session found" : session.getId()));
    }

When I try to access a page that invokes the filter, I see the following on the console: 当我尝试访问一个调用过滤器的页面时,在控制台上看到以下内容:

2014-02-20 21:32:22 DEBUG UserInfo:35 - UserInfo being constructed for sessionId:   no session found
2014-02-20 21:32:22 DEBUG ActivationInfo:27 - ActivationInfo being constructed for sessionId:   no session found 
2014-02-20 21:32:22 DEBUG VerificationFilter:38 - ************VerificatoniFilter initializing*************

And if I go to a different browser and enter a "bad" verification code, the UserInfo/ActivationInfo are never re-injected. 而且,如果我使用其他浏览器并输入“错误的”验证码,则永远不会重新注入UserInfo / ActivationInfo。 IE, with a different session, I do NOT see a new UserInfo/ActivationInfo. IE,具有不同的会话,我看不到新的UserInfo / ActivationInfo。

My questions are: 1. Why is there no session found when the UserInfo/ActivationInfo are being constructed (see the log messages) 2. How can I implement this so that the UserInfo and ActivationInfo can be injected in my other CDI beans later on so that they have the user/activationInfo that I need? 我的问题是:1.为什么在构造UserInfo / ActivationInfo时找不到会话(请参阅日志消息)2.如何实现这一点,以便以后可以将UserInfo和ActivationInfo注入我的其他CDI bean中他们有我需要的user / activationInfo吗? Currently because of this problem, I'm setting the activationInfo directly on the user's session in the VerificationFilter, but when I inject to my CDI bean, a DIFFERENT UserInfo and DIFFERENT ActivationInfo are injected. 当前,由于这个问题,我直接在VerificationFilter中的用户会话上设置activationInfo,但是当我将其注入CDI bean时,将注入DIFFERENT UserInfo和DIFFERENT ActivationInfo。

I'm using Tomcat 7 with JEE 6, WELD. 我将Tomcat 7与JEE 6,WELD一起使用。

Thanks in advance! 提前致谢!

If you're using Tomcat 7, then you don't have a full Java EE 6 container, so JSF and CDI are not available by default, and it's not clear how you've set them up. 如果您使用的是Tomcat 7,则没有完整的Java EE 6容器,因此默认情况下JSF和CDI不可用,并且不清楚如何设置它们。

Actually, I'd expect context.getExternalContext() to throw a NullPointerException in your session-scoped beans, since there is no FacesContext when the bean methods are called by the filter. 实际上,我希望context.getExternalContext()在会话范围的Bean中抛出NullPointerException ,因为在由过滤器调用Bean方法时没有FacesContext

The filter is invoked before the FacesServlet has had a chance to set up a FacesContext . FacesServlet有机会设置FacesContext之前调用该过滤器。

So it seems your question is based on unclear and/or incorrect assumptions. 因此,您的问题似乎是基于不清楚和/或错误的假设。

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

相关问题 在CDI SessionScoped bean中注入HttpServletRequest - Inject HttpServletRequest in CDI SessionScoped bean Java EE将CDI @SessionScoped注入EJB @Stateless会话bean - Java EE inject CDI @SessionScoped into EJB @Stateless session bean 具有生产者bean的@Inject的SessionScoped cdi观察器 - SessionScoped cdi observer with @Inject for producer bean 在SessionScoped CDI Bean中注入无状态EJB - Inject a Stateless EJB in a SessionScoped CDI Bean 结合Resteasy异步未能使用CDI注入SessionScoped和RequestScoped bean - Combine Resteasy async fail to inject SessionScoped and RequestScoped bean with CDI 如果我们将SessionScoped bean注入到无状态bean中,如果没有HTTP会话该怎么办? - If we inject a SessionScoped bean into a Stateless bean, what happens if there is no HTTP session? 防止在SessionScoped Bean中序列化CDI注入的Logger - Prevent serialization of CDI injected Logger in SessionScoped beans 重新注入Spring Autowired依赖项 - Re-Inject Spring Autowired dependency 简单的CDI注入问题…在SessionScoped Controller bean中使用@Inject时,出现与“钝化”相关的错误 - Trouble with simple CDI Injection… “Passivation” related error when using @Inject in SessionScoped Controller bean 在另一个注入@SessionScoped CDI bean时,它们是否属于同一个会话? - When injecting a @SessionScoped CDI bean in another one, does they belong to the same session?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM