Lấy thông tin User Sử Dụng Spring Security trong Java code
Trong quá trình sử dụng Spring security, để có thể lấy các thông tin User sau khi đăng nhập từ Spring security trong Java code thì sẽ làm như thế nào
Bài viết này sẽ giúp cho các bạn giải quyết bài toán đó
Học spring framework cơ bản miễn phí
- Viết api web service trong spring framework: Học spring boot
- Thiết kế website trong spring framework: Học spring mvc
Lấy thông tin User sử dụng Spring security trong Java code
1. Xem nhanh 1 số đoạn code
Thông tin file getUserInformation
2. Các thư viện, phần mềm sử dụng
- Apache maven 3.6.0
- Spring boot 2.0.9.RELEASE
- Inteliij
- JDK 1.8
- Project sử dụng là project api web service
Download các phần mềm tại đây: laptrinhjavaweb.com/huong-dan-cai-dat-phan-mem-4
3. Các bước thực hiện
3.1. Mặc định User trong Spring Security (org.springframework.security.core.userdetails.User) chỉ gồm các thông tin như id, pass, role là những thông tin chung
Nếu muốn lưu trữ các thông tin khác ngoài những thông tin chung thì các bạn phải tự custom lại User đó theo User của mình
Bạn tạo file MyUser và copy đoạn code sau vào file nhé
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import java.util.Collection;
public class MyUser extends User {
public MyUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
}
private Long id;
private String fullName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
MyUser.java
3.2. Tiếp tục bạn tạo file SecurityUtils và add đoạn code sau vô file
(SecurityContextHolder.getContext()).getAuthentication().getPrincipal(): đây chính là method giúp lấy thông tin User trong Spring Security
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.ArrayList;
import java.util.List;
public class SecurityUtils {
public static MyUser getPrincipal() {
return (MyUser) (SecurityContextHolder.getContext()).getAuthentication().getPrincipal();
}
}
SecurityUtils.java
3.3. Bạn tạo controller getUserInformation và add thông tin như sau
@GetMapping("/api/user/information")
public UserDTO getUserInformation() {
UserDTO result = new UserDTO();
result.setUserName(SecurityUtils.getPrincipal().getUsername());
result.setId(SecurityUtils.getPrincipal().getId());
result.setFullName(SecurityUtils.getPrincipal().getFullName());
return result;
}
Controller getUserInformation
Bây giờ bạn chỉ việc run project và kiểm tra kết quả
Học spring framework cơ bản miễn phí
- Viết api web service trong spring framework: Học spring boot
- Thiết kế website trong spring framework: Học spring mvc
Follow Fanpage, Facebook cá nhân, Group Facebook, kênh Youtube, nhóm Facebook để nhận các nội dung hay ho về lập trình Java core, Java web, Java hosting
- Fanpage facebook: XEM TẠI ĐÂY
- Kênh Youtube: XEM TẠI ĐÂY
- Facebook cá nhân cô giáo Trương Tùng Lâm: XEM TẠI ĐÂY
- Nhóm Facebook hỏi đáp miễn phí: XEM TẠI ĐÂY
7 ngày học java master miễn phí: Học java core, java web, java hosting