当前位置: 首页>>代码示例>>Java>>正文


Java SecurityContext.getAuthentication方法代码示例

本文整理汇总了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);
}
 
开发者ID:zhaojunfei,项目名称:lemon,代码行数:23,代码来源:InternalUserAuthConnectorImpl.java

示例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;
}
 
开发者ID:SentinelDataHub,项目名称:dhus-core,代码行数:54,代码来源:ProcessingValve.java

示例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;
}
 
开发者ID:asanzdiego,项目名称:codemotion-2017-taller-de-jhipster,代码行数:20,代码来源:SecurityUtils.java

示例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;
}
 
开发者ID:ElectronicArmory,项目名称:Armory,代码行数:15,代码来源:SecurityUtils.java

示例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;
}
 
开发者ID:mraible,项目名称:devoxxus-jhipster-microservices-demo,代码行数:18,代码来源:SecurityUtils.java

示例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;
}
 
开发者ID:xm-online,项目名称:xm-gate,代码行数:18,代码来源:SecurityUtils.java

示例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();
}
 
开发者ID:peterjurkovic,项目名称:travel-agency,代码行数:9,代码来源:UserUtils.java

示例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页面, 成功返回

}
 
开发者ID:DataAgg,项目名称:DAFramework,代码行数:25,代码来源:CoreServiceApplicationTests.java


注:本文中的org.springframework.security.core.context.SecurityContext.getAuthentication方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。