Q:

java.lang.String cannot be cast to com.model.User error while trying to display logged in users using Spring Security and Thymeleaf

I am very new to Spring Security and I am trying to display username of logged user. I am using Thymeleaf view.
Here is my code:
Controller:
@Controller
public class HomeController {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(Model model) {

        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = user.getUsername(); //get logged in username
        model.addAttribute("username", name);

        return "login";
    }
}

I am trying to display it like this 
Username: <span th:text="${username}"></span>

I am not quite sure this is the right way to do it.
I am getting the following error:
java.lang.String cannot be cast to com.model.User

How can I fix it?

A:

I have found solution that works for me.
My controller looks like this 
@RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(Model model) {
        return "login";
}

So it basic
and in my index.html file I have used this to display username
<span sec:authorize="isAuthenticated()">
    | Logged user: <span sec:authentication="name"></span> |
    Roles: <span sec:authentication="principal.authorities"></span> |

I have used basic Spring Security integration. 
Here is the useful link 
http://www.thymeleaf.org/doc/articles/springsecurity.html
Hope that helps someone with same issue.

