当前位置: 首页>>代码示例>>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;未经允许,请勿转载。