當前位置: 首頁>>代碼示例>>Java>>正文


Java SecurityContext.setAuthentication方法代碼示例

本文整理匯總了Java中org.springframework.security.core.context.SecurityContext.setAuthentication方法的典型用法代碼示例。如果您正苦於以下問題:Java SecurityContext.setAuthentication方法的具體用法?Java SecurityContext.setAuthentication怎麽用?Java SecurityContext.setAuthentication使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.security.core.context.SecurityContext的用法示例。


在下文中一共展示了SecurityContext.setAuthentication方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createSecurityContext

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
public SecurityContext createSecurityContext(pingis.entities.User customUser) {
  SecurityContext context = SecurityContextHolder.createEmptyContext();

  TmcUserDto principal = new TmcUserDto(Long.toString(customUser.getId()),
          customUser.getName(),
          TEST_EMAIL,
          customUser.isAdministrator());

  Authentication authentication = new UsernamePasswordAuthenticationToken(principal,
          "password",
          principal.getAuthorities());

  context.setAuthentication(authentication);

  return context;
}
 
開發者ID:tdd-pingis,項目名稱:tdd-pingpong,代碼行數:17,代碼來源:WithMockCustomUserSecurityContextFactory.java

示例2: login

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@ApiOperation(value = "Login")
@RequestMapping(value = "/login", method = RequestMethod.POST)
public boolean login(@RequestBody User user, HttpServletRequest request) {
    User u = this.userService.login(user.getUsername(), user.getPassword());
    if (u != null) {
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
            user.getUsername(), user.getPassword());

        // Authenticate the user
        Authentication authentication = authenticationManager.authenticate(authRequest);
        SecurityContext securityContext = SecurityContextHolder.getContext();
        securityContext.setAuthentication(authentication);

        // Create a new session and add the security context.
        HttpSession session = request.getSession(true);
        session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
        return true;
    }
    return false;
}
 
開發者ID:bndynet,項目名稱:web-framework-for-java,代碼行數:21,代碼來源:UserController.java

示例3: auth

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
public static Authentication auth(AuthenticationManager authenticationManager, String userName, String password) {
	SecurityContext sc = SecurityContextHolder.getContext();
	UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(userName, password);
	Authentication authentication = authenticationManager.authenticate(upToken);
	sc.setAuthentication(authentication);
	return sc.getAuthentication();
}
 
開發者ID:DataAgg,項目名稱:DAFramework,代碼行數:8,代碼來源:SecurityHelper.java

示例4: testgetCurrentUserLogin

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testgetCurrentUserLogin() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
    SecurityContextHolder.setContext(securityContext);
    String login = SecurityUtils.getCurrentUserLogin();
    assertThat(login).isEqualTo("admin");
}
 
開發者ID:xm-online,項目名稱:xm-uaa,代碼行數:9,代碼來源:SecurityUtilsUnitTest.java

示例5: testgetCurrentUserJWT

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testgetCurrentUserJWT() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "token"));
    SecurityContextHolder.setContext(securityContext);
    Optional<String> jwt = SecurityUtils.getCurrentUserJWT();
    assertThat(jwt).contains("token");
}
 
開發者ID:torgcrm,項目名稱:TorgCRM-Server,代碼行數:9,代碼來源:SecurityUtilsUnitTest.java

示例6: testAnonymousIsNotAuthenticated

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testAnonymousIsNotAuthenticated() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    Collection<GrantedAuthority> authorities = new ArrayList<>();
    authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS));
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("anonymous", "anonymous", authorities));
    SecurityContextHolder.setContext(securityContext);
    boolean isAuthenticated = SecurityUtils.isAuthenticated();
    assertThat(isAuthenticated).isFalse();
}
 
開發者ID:xm-online,項目名稱:xm-uaa,代碼行數:11,代碼來源:SecurityUtilsUnitTest.java

示例7: testIsCurrentUserInRole

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testIsCurrentUserInRole() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    Collection<GrantedAuthority> authorities = new ArrayList<>();
    authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
    SecurityContextHolder.setContext(securityContext);

    assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.USER)).isTrue();
    assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)).isFalse();
}
 
開發者ID:xm-online,項目名稱:xm-uaa,代碼行數:12,代碼來源:SecurityUtilsUnitTest.java

示例8: testCurrentUserInRole

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testCurrentUserInRole() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    Collection<GrantedAuthority> authorities = new ArrayList<>();
    authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
    SecurityContextHolder.setContext(securityContext);
    boolean isInRole = SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.USER);
    assertThat(isInRole).isFalse();
}
 
開發者ID:klask-io,項目名稱:klask-io,代碼行數:11,代碼來源:SecurityUtilsUnitTest.java

示例9: testIsAuthenticated

import org.springframework.security.core.context.SecurityContext; //導入方法依賴的package包/類
@Test
public void testIsAuthenticated() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
    SecurityContextHolder.setContext(securityContext);
    boolean isAuthenticated = SecurityUtils.isAuthenticated();
    assertThat(isAuthenticated).isTrue();
}
 
開發者ID:GastonMauroDiaz,項目名稱:buenojo,代碼行數:9,代碼來源:SecurityUtilsTest.java


注:本文中的org.springframework.security.core.context.SecurityContext.setAuthentication方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。