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


Java SecurityContextHolder.setContext方法代碼示例

本文整理匯總了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);
	}
}
 
開發者ID:EsupPortail,項目名稱:esup-ecandidat,代碼行數:27,代碼來源:UserController.java

示例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");
}
 
開發者ID:IBM,項目名稱:Microservices-with-JHipster-and-Spring-Boot,代碼行數:9,代碼來源:SecurityUtilsUnitTest.java

示例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();
}
 
開發者ID:michaelhoffmantech,項目名稱:patient-portal,代碼行數:9,代碼來源:SecurityUtilsUnitTest.java

示例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");
}
 
開發者ID:torgcrm,項目名稱:TorgCRM-Server,代碼行數:9,代碼來源:SecurityUtilsUnitTest.java

示例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();
}
 
開發者ID:torgcrm,項目名稱:TorgCRM-Server,代碼行數:11,代碼來源:SecurityUtilsUnitTest.java

示例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();
}
 
開發者ID:xm-online,項目名稱:xm-uaa,代碼行數:12,代碼來源:SecurityUtilsUnitTest.java

示例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();
    }
}
 
開發者ID:blackarbiter,項目名稱:Android_Code_Arbiter,代碼行數:28,代碼來源:SpringSafeSessionFilter.java

示例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;
}
 
開發者ID:ligoj,項目名稱:bootstrap,代碼行數:24,代碼來源:AbstractSecurityTest.java

示例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);
}
 
開發者ID:PatternFM,項目名稱:tokamak,代碼行數:9,代碼來源:OAuth2AuthorizationContextTest.java

示例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);
}
 
開發者ID:PatternFM,項目名稱:tokamak,代碼行數:9,代碼來源:OAuth2AuthorizationContextTest.java

示例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());
}
 
開發者ID:pandboy,項目名稱:pingguopai,代碼行數:9,代碼來源:Tester.java

示例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);
}
 
開發者ID:mjfcolas,項目名稱:infotaf,代碼行數:9,代碼來源:JwtTokenAuthenticationProcessingFilter.java

示例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();
}
 
開發者ID:EsupPortail,項目名稱:esup-ecandidat,代碼行數:52,代碼來源:UserController.java


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