Announcement Announcement Module
Collapse
No announcement yet.
Newbie question: RequestMapping and Principl Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie question: RequestMapping and Principl

    Hello,

    I get the principal variable not null in the adminHome method. Why do no I get correctly the principal variable (Which is null) in homeHome method ?

    Code:
    @Controller
    public class HomeController {
    
        @RequestMapping("/admin")
        public String adminHome(Principal principal, Model model) {
            model.addAttribute("username", principal.getName());
            return "view.admin";
        }
        
        @RequestMapping("/home")
        public String homeHome(Principal principal, Model model) {
        	model.addAttribute("username", principal.getName());
            return "view.home";
        }
    
    }
    OR

    Same for this solution:

    Code:
        @RequestMapping("/home")
        public String homeHome(Principal principal, Model model) {
        	User user = (User) SecurityContextHolder.getContext().getAuthentication();
        	if (user != null) {
        		String username = user.getUsername(); // Storing logged in username into String 
            	model.addAttribute("user", username);
        	}
        	
        	return "view.home";
        }
    I don't understand where in the spring configuration, the mapping is done between "/admin" and my credential information.

    Thxs

  • #2
    Same for this solution:
    Both eventually takes from the request.

    Is user already authenticated when it hits /home?

    Comment


    • #3
      Originally posted by amiladomingo View Post
      Both eventually takes from the request.

      Is user already authenticated when it hits /home?
      Here is the code of the view "home"... User can be either authenticated or not.

      Code:
      <%@ page contentType="text/html;charset=UTF-8" language="java" trimDirectiveWhitespaces="true" %>
      
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
      <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
      <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
      <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
      <%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
      <%@ taglib prefix="util" tagdir="/WEB-INF/tags/util" %>
      <%@ taglib prefix="tb" uri="/WEB-INF/taglib/taglib.tld" %>
      
      <util:js value="/resources/js/pages/home.js" />
      
      <h1>Bootstrap starter template</h1>
      <p>Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p>
      
      <p> Hello, 
      <c:choose>
            <c:when test="${empty username}">Unknown !
            <br />
            </c:when>
      
            <c:otherwise>${username}!
            <br />
            </c:otherwise>
      </c:choose> </p>
      My missunderstanding is on the Principal Object which is null in homeHome method then is instanced in adminHome method.

      Here is the spring's configuration files I use:

      https://github.com/noroutine/tobacco...webapp/WEB-INF

      Thxs

      Comment


      • #4
        I guess the answer is:

        spring-security.xml
        Code:
        <intercept-url pattern="/home" access="permitAll" />
        I m learning :/
        Last edited by azman0101; May 20th, 2013, 06:14 PM.

        Comment

        Working...
        X