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