本文整理汇总了Java中org.springframework.security.core.context.SecurityContextHolder.createEmptyContext方法的典型用法代码示例。如果您正苦于以下问题:Java SecurityContextHolder.createEmptyContext方法的具体用法?Java SecurityContextHolder.createEmptyContext怎么用?Java SecurityContextHolder.createEmptyContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.core.context.SecurityContextHolder
的用法示例。
在下文中一共展示了SecurityContextHolder.createEmptyContext方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSecurityContext
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的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;
}
示例2: setup
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Before
public void setup() {
if (this.registry != null) {
this.registry.clear();
}
this.context = SecurityContextHolder.createEmptyContext();
this.context.setAuthentication(
new UsernamePasswordAuthenticationToken("username-" + UUID.randomUUID(),
"na", AuthorityUtils.createAuthorityList("ROLE_USER")));
this.changedContext = SecurityContextHolder.createEmptyContext();
this.changedContext.setAuthentication(new UsernamePasswordAuthenticationToken(
"changedContext-" + UUID.randomUUID(), "na",
AuthorityUtils.createAuthorityList("ROLE_USER")));
}
示例3: alimenteSecurityUserCptMin
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Alimente la session pour un compte local
*
* @param cptMin
* le compte a minima a connecter
*/
public void alimenteSecurityUserCptMin(CompteMinima cptMin) {
SecurityUser user = (SecurityUser) getCurrentUser();
if (user != null) {
List<GrantedAuthority> authoritiesListe = new ArrayList<GrantedAuthority>();
SimpleGrantedAuthority sga = new SimpleGrantedAuthority(ConstanteUtils.ROLE_CANDIDAT);
authoritiesListe.add(sga);
SecurityUserCandidat securityUserCandidat = new SecurityUserCandidat(user.getUsername(),
user.getDisplayName(), authoritiesListe, cptMin.getIdCptMin(), cptMin.getNumDossierOpiCptMin(),
cptMin.getTemValidCptMin(), cptMin.getTemValidMailCptMin(), null);
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
securityUserCandidat, securityUserCandidat.getUsername(), securityUserCandidat.getAuthorities());
Authentication authentication = authenticationManagerCandidat.authenticate(authRequest);
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
UI.getCurrent().getSession().getSession()
.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
}
}
示例4: testIsAuthenticated
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的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();
}
示例5: testAnonymousIsNotAuthenticated
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的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();
}
示例6: testgetCurrentUserLogin
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的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");
}
示例7: testgetCurrentUserJWT
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Test
public void testgetCurrentUserJWT() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "token"));
SecurityContextHolder.setContext(securityContext);
String jwt = SecurityUtils.getCurrentUserJWT();
assertThat(jwt).isEqualTo("token");
}
示例8: testIsCurrentUserInRole
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Test
public void testIsCurrentUserInRole() {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
Collection<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.PATIENT));
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
SecurityContextHolder.setContext(securityContext);
assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.PATIENT)).isTrue();
assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)).isFalse();
}
示例9: testCurrentUserInRole
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的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();
}