本文整理匯總了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();
}