일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 이클립스
- full text indexing
- 자바
- 전체텍스트
- while
- jquery
- angular2
- 상속예제
- 상속
- 다운캐스팅
- Validations
- 전체
- 자바 야구게임
- 업캐스팅
- 25가지 효율적인 sql작성법
- 로또
- 전자정부
- 다형성
- IBatis procedure
- 단축키
- 페이징
- 추상클래스
- 야구게임
- Full text
- 스프링
- 가변인자
- Login with OAuth Authentication
- 형변환
- Random
Archives
- Today
- Total
nalaolla
Get current logged in username in Spring Security 본문
728x90
반응형
In this article, we will show you three ways to get the current logged in username in Spring Security.
1. SecurityContextHolder + Authentication.getName()
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
@RequestMapping(value="/login", method = RequestMethod.GET)
public String printUser(ModelMap model) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
model.addAttribute("username", name);
return "hello";
}
//...
2. SecurityContextHolder + User.getUsername()
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
@RequestMapping(value="/login", method = RequestMethod.GET)
public String printUser(ModelMap model) {
User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String name = user.getUsername(); //get logged in username
model.addAttribute("username", name);
return "hello";
}
//...
3. UsernamePasswordAuthenticationToken
This is more elegant solution, in runtime, Spring will injects UsernamePasswordAuthenticationToken
into the Principal
interface.
import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
@RequestMapping(value="/login", method = RequestMethod.GET)
public String printWelcome(ModelMap model, Principal principal ) {
String name = principal.getName(); //get logged in username
model.addAttribute("username", name);
return "hello";
}
//...
Download Source Code
Download it – Spring-Security-Get-Logged-In-Username.zip (9 KB)
728x90
반응형
'SPRING' 카테고리의 다른 글
1) 스프링부트로 웹 서비스 출시하기 - 1. SpringBoot & Gradle & Github 프로젝트 생성하기 (0) | 2020.02.06 |
---|---|
트랜잭션 관리 (0) | 2017.11.16 |
스프링 시큐리티 로그인 (0) | 2017.02.14 |
Spring Security Custom AuthenticationProvider example (0) | 2017.02.06 |
java abstract factory pattern (추상 팩토리 패턴) (0) | 2016.11.28 |