در این بخش خواهیم دید وقتی لایه Spring Security را در پروژه Spring داریم از چه راه هایی میتوان اطلاعات کاربر احراز هویت شده را دریافت کرد
ساده ترین روش دریافت اطلاعات کاربر معتبر استفاده از SecurityContextHolder است :
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String currentPrincipalName = authentication.getName();
میتوان قبل از اینکه اطلاعات کاربر را دریافت کنیم از احراز هویت کاربر مطمئن شویم :
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (!(authentication instanceof AnonymousAuthenticationToken)) { String currentUserName = authentication.getName(); return currentUserName; }
دریافت اطلاعات کاربر در Controller@ :
بصورت ابجکتی از Principal در ورودی متد کنترلر :
@Controller public class SecurityController { @RequestMapping(value = "/username", method = RequestMethod.GET) @ResponseBody public String currentUserName(Principal principal) { return principal.getName(); } }
و یا بصورت ابجکتی از Authentication که توکن احراز هویت است :
@Controller public class SecurityController { @RequestMapping(value = "/username", method = RequestMethod.GET) @ResponseBody public String currentUserName(Authentication authentication) { return authentication.getName(); } }
ابجکت Principal میتواند به UserDetails تبدیل شود Cast :
UserDetails userDetails = (UserDetails) authentication.getPrincipal(); System.out.println("User has authorities: " + userDetails.getAuthorities());
دسترسی به ابجکت Principal از HttpServletRequest :
@Controller public class GetUserWithHTTPServletRequestController { @RequestMapping(value = "/username", method = RequestMethod.GET) @ResponseBody public String currentUserNameSimple(HttpServletRequest request) { Principal principal = request.getUserPrincipal(); return principal.getName(); } }
روش دریافت اطلاعات کاربر معتبر با طراحی اینترفیس سفارشی :
برای اینکه از ویژگی Dependency Injection در Spring بهره ببریم و اطلاعات کاربر را در هر جایی استخراج کنیم و نیازی به کلاس Controller@ نداشته باشیم میتوانیم اینترفیسی را برای این منظور طراحی کنیم :
public interface IAuthenticationFacade { Authentication getAuthentication(); }
و آنرا در کلاس Component@ که از IAuthenticationFacade پیاده سازی کرده باشد معرفی میکنیم که این کلاس برای Spring قابل شناسایی است :
@Component public class AuthenticationFacade implements IAuthenticationFacade { @Override public Authentication getAuthentication() { return SecurityContextHolder.getContext().getAuthentication(); } }
و طریقه استفاده از آن به عنوان یک Bean و تزریق خودکار اطلاعات توسط Spring :
@Controller public class GetUserWithCustomInterfaceController { @Autowired private IAuthenticationFacade authenticationFacade; @RequestMapping(value = "/username", method = RequestMethod.GET) @ResponseBody public String currentUserNameSimple() { Authentication authentication = authenticationFacade.getAuthentication(); return authentication.getName(); } }
دریافت اطلاعات کاربری در JSP :
با استفاده از Spring Security taglib میتوان از امکانات این taglib برای دریافت اطلاعات استفاده کنیم :
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
و دریافت ابجکت Principal :
<security:authorize access="isAuthenticated()"> authenticated as <security:authentication property="principal.username" /> </security:authorize>
دریافت اطلاعات کاربر در Thymeleaf :
ابتدا نیاز است که وابستگی های مورد نیاز را اضافه کنیم :
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency>
و در تمپلیت به ابجکت Principal دسترسی پیدا کنیم :
<html xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> <body> <div sec:authorize="isAuthenticated()"> Authenticated as <span sec:authentication="name"></span></div> </body> </html>