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