-1

I want to output userName if he log in. From @Controller i have access:

@RequestMapping("/success")
    public String success(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName(); //get logged in username
        model.addAttribute("name", name);
        return "success";
    }

It work an if i use name on jsp, I see typed name.

But if in this jsp I write

 <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    <sec:authentication property="principal.username"/>

I get stackTrace:

 INFO : com.epam.hhsystem.util.CustomAuthentificationProvider - User with name 'Nikolay_Tkachev' log in
07.08.2013 17:00:57 org.apache.jasper.compiler.TldLocationsCache tldScanJar
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
07.08.2013 17:00:57 org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.springframework.beans.NotReadablePropertyException: Invalid property 'principal.username' of bean class [org.springframework.security.authentication.UsernamePasswordAuthenticationToken]: Bean property 'principal.username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707)
    at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699)

...

1 Answer 1

9

As you have authentiucation using using username and password, principal is it self the name of user. try using

<sec:authentication property="principal"/>
2
  • Why everywhere write <sec:authentication property="principal.username"/> ? Aug 8, 2013 at 7:04
  • 1
    AFAIK, if you have a simple username and password, username can be stored directly in principal as a string. But if you have extra details like company name, department etc with username , you can assign a customized object to principal and hence in that case you will use principal,username
    – coder
    Aug 8, 2013 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.