by.bsuir.finance.controllers.UserInfoController.java Source code

Java tutorial

Introduction

Here is the source code for by.bsuir.finance.controllers.UserInfoController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package by.bsuir.finance.controllers;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author igor
 */
@Controller
public class UserInfoController {

    @Autowired
    private UserInfoService infoService;

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    public @ResponseBody String getUserName() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName(); //get logged in username
        return name;
    }

    @RequestMapping(value = "userinfo")
    public ModelAndView getUserInfo() {
        ModelAndView model = new ModelAndView("userinfo");
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName(); //get logged in username
        mdoel.addObject("userinfo", infoService.getCurrentUserInfo(name));
        return userinfo;
    }
}