Announcement Announcement Module
Collapse
No announcement yet.
java.lang.NullPointerException at SecurityContextHolder when trying to get the user Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.lang.NullPointerException at SecurityContextHolder when trying to get the user

    Hi guys i'm having a problem with the spring security module i'm using it with spring mvc, well the probleme is that when i try to access the :
    Code:
        localhost:45045/loginTutorial/welcome
    the page cannot get the user name from the spring-security.xml file
    Code:
        <http auto-config="true">
    		<intercept-url pattern="/welcome*" access="ROLE_USER" />
    		<form-login login-page="/login" default-target-url="/welcome"
    			authentication-failure-url="/loginfailed" />
    		<logout logout-success-url="/logout" />
    	</http>
     
    	<authentication-manager>
    	  <authentication-provider>
    		<user-service>
    			<user name="mkyong" password="123456" authorities="ROLE_USER" />
    		</user-service>
    	  </authentication-provider>
    	</authentication-manager>
    and it gives the following Exception :
    Code:
        exception
    
        org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        cause mère
    
        java.lang.NullPointerException
    	org.gov.tgr.formation.controller.LoginController.printWelcome(LoginController.java:16)
    	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	java.lang.reflect.Method.invoke(Method.java:616)
    	org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    knowing that the controller class is :
    Code:
        @Controller
        public class LoginController {
     
    	@RequestMapping(value="/welcome", method = RequestMethod.GET)
    	public String printWelcome(ModelMap model) {
     
    		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    		String name = user.getUsername();
    	
    		model.addAttribute("username", name);
    		model.addAttribute("message", "Spring Security login + database example");
    		return "hello";
     
    	}
     
    	@RequestMapping(value="/login", method = RequestMethod.GET)
    	public String login(ModelMap model) {
     
    		return "login";
     
    	}
    	
    	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
    	public String loginerror(ModelMap model) {
     
    		model.addAttribute("error", "true");
    		return "login";
     
    	}
    	
    	@RequestMapping(value="/logout", method = RequestMethod.GET)
    	public String logout(ModelMap model) {
     
    		return "login";
     
    	}
    	
    }
    i was just following a tutorial and i did as they asked what's causing this problem ? can different version from spring mvc jars and spring security jars cause this ? thanks
Working...
X