本文整理汇总了Java中org.springframework.security.core.context.SecurityContextHolder.setContext方法的典型用法代码示例。如果您正苦于以下问题:Java SecurityContextHolder.setContext方法的具体用法?Java SecurityContextHolder.setContext怎么用?Java SecurityContextHolder.setContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.core.context.SecurityContextHolder
的用法示例。
在下文中一共展示了SecurityContextHolder.setContext方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
}
示例2: 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");
}
示例3: 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();
}
示例4: 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);
Optional<String> login = SecurityUtils.getCurrentUserLogin();
assertThat(login).contains("admin");
}
示例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: 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.USER));
securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("user", "user", authorities));
SecurityContextHolder.setContext(securityContext);
assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.USER)).isTrue();
assertThat(SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)).isFalse();
}
示例7: doFilterInternal
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Override
protected void doFilterInternal(
HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws ServletException, IOException {
ServletRequestAttributes attributes = new ServletRequestAttributes(req, res);
try {
if(1 + 1 == 2) {
SecurityContext oldCtx = SecurityContextHolder.getContext();
SecurityContextHolder.setContext(null); //
try {
super.doFilter(req, res, chain);
} finally {
SecurityContextHolder.setContext(oldCtx);
}
}
else {
super.doFilter(req, res, chain);
}
}
finally {
attributes.requestCompleted();
}
}
示例8: initSpringSecurityContext
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Initialize {@link SecurityContextHolder} for given user.
*
* @param user
* the user to set in the context.
* @param authorities
* the optional authorities name
* @return The configured {@link SecurityContext}.
*/
@SuppressWarnings("unchecked")
protected SecurityContext initSpringSecurityContext(final String user, final GrantedAuthority... authorities) {
SecurityContextHolder.clearContext();
final SecurityContext context = Mockito.mock(SecurityContext.class);
final Authentication authentication = Mockito.mock(Authentication.class);
final List<GrantedAuthority> authoritiesAsList = Arrays.asList(authorities);
final User userDetails = new User(user, USER_DETAILS_NA, authoritiesAsList);
Mockito.when((List<GrantedAuthority>) authentication.getAuthorities()).thenReturn(authoritiesAsList);
Mockito.when(context.getAuthentication()).thenReturn(authentication);
Mockito.when(authentication.getPrincipal()).thenReturn(userDetails);
Mockito.when(authentication.getName()).thenReturn(user);
SecurityContextHolder.setContext(context);
return context;
}
示例9: shouldReturnAnEmtpySetOfAuthoritiesIfTheAuthenticationIsNotAnOAuth2Authentication
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Test
public void shouldReturnAnEmtpySetOfAuthoritiesIfTheAuthenticationIsNotAnOAuth2Authentication() {
when(context.getAuthentication()).thenReturn(testAuthentication);
SecurityContextHolder.setContext(context);
OAuth2AuthorizationContext ctx = new OAuth2AuthorizationContext();
assertThat(ctx.getAuthorities()).hasSize(0);
}
示例10: shouldReturnAnEmtpySetOfScopesIfTheAuthenticationIsNotAnOAuth2Authentication
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Test
public void shouldReturnAnEmtpySetOfScopesIfTheAuthenticationIsNotAnOAuth2Authentication() {
when(context.getAuthentication()).thenReturn(testAuthentication);
SecurityContextHolder.setContext(context);
OAuth2AuthorizationContext ctx = new OAuth2AuthorizationContext();
assertThat(ctx.getScopes()).hasSize(0);
}
示例11: setupMockMvc
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Before
public void setupMockMvc() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
securityContext = Mockito.mock( SecurityContext.class );
SecurityContextHolder.setContext( securityContext );
Mockito.when( securityContext.getAuthentication() ).thenReturn( new AnonAuthentication() );
mockAuthenticatedUser(buildTestAdmin());
}
示例12: successfulAuthentication
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException {
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authResult);
SecurityContextHolder.setContext(context);
chain.doFilter(request, response);
}
示例13: connectCandidatInterne
import org.springframework.security.core.context.SecurityContextHolder; //导入方法依赖的package包/类
/**
* Connexion d'un candidat
*
* @param username
* login
* @param password
* mot de passe
*/
public void connectCandidatInterne(String username, String password) {
if (loadBalancingController.isLoadBalancingGestionnaireMode()) {
return;
}
CompteMinima cptMin = candidatController.searchCptMinByNumDossier(username);
if (cptMin != null) {
if (!validPwdCandidat(password, cptMin)) {
return;
}
if (!cptMin.getTemValidCptMin() || !cptMin.getTemValidMailCptMin()) {
Notification.show(applicationContext.getMessage("compteMinima.connect.valid.error", null,
UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
return;
}
} else {
Notification.show(
applicationContext.getMessage("compteMinima.connect.user.error", null, UI.getCurrent().getLocale()),
Type.WARNING_MESSAGE);
return;
}
SecurityUser user = constructSecurityUserCandidat(username, cptMin);
if (user == null) {
return;
}
// authentication
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(user, username,
user.getAuthorities());
Authentication authentication = authenticationManagerCandidat.authenticate(authRequest);
/* Se désinscrit de la réception de notifications */
uiController.unregisterUiCandidat(MainUI.getCurrent());
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
UI.getCurrent().getSession().getSession()
.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
MainUI current = (MainUI) UI.getCurrent();
uiController.registerUiCandidat(current);
i18nController.initLanguageUI(true);
current.navigateToAccueilView();
}