일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- IBatis procedure
- 추상클래스
- while
- 가변인자
- 로또
- 페이징
- 전체
- Full text
- 스프링
- jquery
- 25가지 효율적인 sql작성법
- 야구게임
- Login with OAuth Authentication
- Random
- 상속
- Validations
- 전자정부
- 전체텍스트
- 다운캐스팅
- 자바 야구게임
- 자바
- 상속예제
- 이클립스
- full text indexing
- 다형성
- 단축키
- 업캐스팅
- angular2
- 형변환
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 |