Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using some beans in Filter bean class?

In my filter bean class, I added some beans dependency (with @Autowired annotation). But in the method doFilter(), all my dependency beans have null ...

public class FacebookOAuth implements Filter
{
@Autowired
private BusinessLogger logger;

@Autowired
private IUserSessionInfo userSessionInfo;

@Autowired
private FacebookOAuthHelper oAuthHelper;

public void init(FilterConfig fc) throws ServletException
{
    // Nothing to do
}

public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws   IOException, ServletException
{
    // HttpServletRequest req = (HttpServletRequest)sr;
    HttpServletResponse res = (HttpServletResponse) sr1;

    String code = sr.getParameter("code");

    if (StringUtil.isNotBlankStr(code))
    {
        String authURL = this.oAuthHelper.getAuthURL(code);

this.oAuthHelper is equal at null (and other dependancy beans to) ...

Could you help me ?


In fact I don't use MVC notion on server side (Spring). For my side client I use Flex technology and BlazeDS servlet ton communicate with my server.

So, that is the reason, I use the Filter bean notion.

So, how can I handle my session bean notion in my Filter bean ?


Skaffman,

I implemented your idea, so I update my application.xml with :

<bean id="FacebookOAuthHandler" class="com.xx.FacebookOAuthHandler" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
    <props>
       <prop key="/fbauth">FacebookOAuthHandler</prop>         
    </props>
   </property>
</bean>

and my FacebookOAuthHandler class :

public class FacebookOAuthHandler extends AbstractController
{
@Autowired
private BusinessLogger logger;

@Autowired
private IUserSessionInfo userSessionInfo;

@Autowired
private FacebookOAuthHelper oAuthHelper;

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    // TODO

    return null;
}

But, this method handleRequestInternal is never called when my URL is : http://xx.xx.xx.xx/MyApp/fbauth

like image 752
Anthony Avatar asked Sep 05 '10 09:09

Anthony


2 Answers

I was facing the same problem and my first idea was to manually force Spring to apply @Autowired annotation to the filter like proposed here

http://forum.springsource.org/showthread.php?60983-Autowiring-the-servlet-filter

But I don't like the idea of hardcoding the bean name in my Java class.

I found an cleaner way that works as well:

public void init(FilterConfig filterConfig) throws ServletException {
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
            filterConfig.getServletContext());
}
like image 200
Mathias G. Avatar answered Sep 27 '22 17:09

Mathias G.


Assuming this Filter is wired up in your web.xml, then this isn't going to work, since it's not managed by Spring, it's managed by the servlet container. So things like autowiring won't work.

If you want to define a servlet filter as Spring bean, then you need to define it in the webapp's root application context (using a ContextLoaderListener in web.xml), and then defining a DelegatingFilterProxy which delegates to your Spring-managed bean to do the work.

However, do you really need a servlet filter for this? What what I know of Facebook auth stuff, this could be done just as easily with a Spring HandlerInterceptor. This would be considerably less configuration work than a delegating filter.

like image 37
skaffman Avatar answered Sep 27 '22 17:09

skaffman