0

I'm writing an very simple authorization system. I doesn't want to use Spring Security. I want to write very simple auth, based on sessions, cookie and MySQL.

Now, for remember me functionality I want that every time that an page loads, an method will check cookies.

I create init-bean, it works. But when I try read cookie, it fails.

For cookies I need HttpServletRequest. So this is what I do in init-bean:

    @Override
public void afterPropertiesSet() throws Exception {
    System.out.println("Init-Bean started");

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes()).getRequest();

    // Cookie cookies[] = request.getCookies();
    //
    // for (Cookie c : cookies) {
    // System.out.println(c.toString());
    // }
}

I fails on getRequest

I get: Error creating bean with name 'auth': Invocation of init method failed; nested exception is java.lang.NullPointerException

Is there any way get HttpServletRequest from init-bean? Is there any other way to this?

What I want is: Page load, read cookies, if cookie exists -> check if user exists in DB -> start session

Tnx.

P.S I feel lost after mooving from PHP to Spring (in PHP it just global array, 1 row of code). iN asp.net MVC I did it in MvcApplication. But in Java SPRING 3 I has no luck...

Alexander R.
  • 1,756
  • 12
  • 19
  • 1
    This doesn't make any sense. Cookies apply to specific HTTP requests; why do you think a cookie would be present on startup? Furthermore, you might be better off writing a filter for Spring Security instead and letting it handle all the session management. – chrylis -cautiouslyoptimistic- May 06 '14 at 00:08
  • I hate Spring's xmls and ton's of unnecessary code... It must be some very simple task. Cookies and session are very simple things. How can I "cathc" request, such that I will not need write code in every controller's method? In ASP.NET I did it in Application class, I can't find something similar in Spring... – Alexander R. May 06 '14 at 09:16
  • Modern Spring configuration doesn't need XML for most setups, and take a look at Spring Boot for a much simplified way of setting up a runtime environment. I know how to set up all the XML for a Spring `war`, but Spring Boot makes it so easy I'm using it for all my deployments now. – chrylis -cautiouslyoptimistic- May 06 '14 at 09:42

1 Answers1

1

I feel lost after mooving from PHP to Spring (in PHP it just global array, 1 row of code)

This sentence makes this question make a whole lot more sense. In Java, the servlet classes are instantiated at server startup, not at request time so when afterPropertiesSet() is called, no request has come in yet (in fact, there's no way a request could have come in because the server hasn't started accepting connections yet). I suggest you check out this SO question. The accepted answer does a great job of explaining the lifecycle of beans, etc. Fundamentally, you will need to rethink the way you handle requests in a Java/Spring environment coming from a PHP environment.

Community
  • 1
  • 1
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109