本文整理匯總了Java中org.springframework.security.core.context.SecurityContext.getAuthentication方法的典型用法代碼示例。如果您正苦於以下問題:Java SecurityContext.getAuthentication方法的具體用法?Java SecurityContext.getAuthentication怎麽用?Java SecurityContext.getAuthentication使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.security.core.context.SecurityContext
的用法示例。
在下文中一共展示了SecurityContext.getAuthentication方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getCurrentUser
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
/**
* Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's
* {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our
* application Spring Security usernames are email addresses).
*/
@Override
public CalendarUser getCurrentUser() {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (authentication == null) {
return null;
}
CalendarUser user = (CalendarUser) authentication.getPrincipal();
String email = user.getEmail();
if (email == null) {
return null;
}
CalendarUser result = calendarService.findUserByEmail(email);
if (result == null) {
throw new IllegalStateException(
"Spring Security is not in synch with CalendarUsers. Could not find user with email " + email);
}
logger.info("CalendarUser: {}", result);
return result;
}
開發者ID:PacktPublishing,項目名稱:Spring-Security-Third-Edition,代碼行數:28,代碼來源:SpringSecurityUserContext.java
示例2: findFromSession
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
public UserAuthDTO findFromSession(HttpSession session) {
SecurityContext securityContext = (SecurityContext) session
.getAttribute("SPRING_SECURITY_CONTEXT");
if (securityContext == null) {
return null;
}
Authentication authentication = securityContext.getAuthentication();
if (authentication == null) {
return null;
}
Object principal = authentication.getPrincipal();
if (!(principal instanceof UserAuthDTO)) {
return null;
}
return ((UserAuthDTO) principal);
}
示例3: createProcessing
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
/**
* Logs information into temporary cache. According to the Valve
* configuration, log will also display into the logger.
*
* @param request the input user request to log.
* @param response the response to the user to be incremented.
* return the log entry.
* @throws IOException
* @throws ServletException
*/
private ProcessingInformation createProcessing(Request request, Response response)
throws IOException, ServletException
{
String request_string = null;
if (request.getQueryString() != null)
{
request_string = request.getRequestURL().append('?').append(request.getQueryString()).toString();
}
else
{
request_string = request.getRequestURL().toString();
}
ProcessingInformation pi = new ProcessingInformation(request_string);
// Retrieve cookie to obtains existing context if any.
Cookie integrityCookie = CookieKey.getIntegrityCookie(request.getCookies());
SecurityContext ctx = null;
if (integrityCookie != null)
{
String integrity = integrityCookie.getValue();
if (integrity != null && !integrity.isEmpty())
{
ctx = SecurityContextProvider.getSecurityContext(integrity);
}
}
if ((ctx != null) && (ctx.getAuthentication() != null))
{
pi.setUsername(ctx.getAuthentication().getName());
}
else
{
String[] basicAuth = extractAndDecodeHeader(request.getHeader("Authorization"));
if (basicAuth != null)
{
pi.setUsername(basicAuth[0]);
}
}
pi.setRemoteAddress(ProxyWebAuthenticationDetails.getRemoteIp(request));
pi.setRemoteHost(ProxyWebAuthenticationDetails.getRemoteHost(request));
return pi;
}
示例4: getCurrentUserLogin
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
/**
* Get the login of the current user.
*
* @return the login of the current user
*/
public static String getCurrentUserLogin() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String userName = null;
if (authentication != null) {
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
userName = springSecurityUser.getUsername();
} else if (authentication.getPrincipal() instanceof String) {
userName = (String) authentication.getPrincipal();
}
}
return userName;
}
示例5: isAuthenticated
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
/**
* Check if a user is authenticated.
*
* @return true if the user is authenticated, false otherwise
*/
public static boolean isAuthenticated() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null) {
return authentication.getAuthorities().stream()
.noneMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(AuthoritiesConstants.ANONYMOUS));
}
return false;
}
示例6: isCurrentUserInRole
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
/**
* If the current user has a specific authority (security role).
*
* <p>The name of this method comes from the isUserInRole() method in the Servlet API</p>
*
* @param authority the authority to check
* @return true if the current user has the authority, false otherwise
*/
public static boolean isCurrentUserInRole(String authority) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null) {
return authentication.getAuthorities().stream()
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(authority));
}
return false;
}
示例7: isCurrentUserInRole
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
/**
* If the current user has a specific authority (security role).
* <p>
* The name of this method comes from the isUserInRole() method in the Servlet API
*
* @param authority the authority to check
* @return true if the current user has the authority, false otherwise
*/
public static boolean isCurrentUserInRole(String authority) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null) {
return authentication.getAuthorities().stream()
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals(authority));
}
return false;
}
示例8: getLoggedUserDetails
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
public Optional<TwoFaUserDetails> getLoggedUserDetails(){
SecurityContext context = SecurityContextHolder.getContext();
Authentication auth = context.getAuthentication();
if (auth != null && auth.getPrincipal() instanceof TwoFaUserDetails){
return Optional.of( ((TwoFaUserDetails) auth.getPrincipal() ));
}
return Optional.empty();
}
示例9: testSecurity
import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testSecurity() {
SecurityContext sc = SecurityContextHolder.getContext();
Authentication authentication = sc.getAuthentication();
isNull(authentication, "未認證");
UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken("watano", "123456");
// Perform the security
authentication = authenticationManager.authenticate(upToken);
sc.setAuthentication(authentication);
authentication = sc.getAuthentication();
notNull(authentication, "已認證");
isTrue(authentication.isAuthenticated(), "未認證");
//1.訪問/user頁麵, 失敗返回失敗,提示登錄
//2.使用username和password提交到/login, 返回登錄成功
//3.附帶token信息,再次訪問/user頁麵, 成功返回
}