Java Code Examples for org.springframework.security.core.context.SecurityContext#setAuthentication()
The following examples show how to use
org.springframework.security.core.context.SecurityContext#setAuthentication() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 7 votes |
@Test public void testGetCurrentUserLoginForOAuth2() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); Map<String, Object> claims = new HashMap<>(); claims.put("groups", "ROLE_USER"); claims.put("sub", 123); claims.put("preferred_username", "admin"); OidcIdToken idToken = new OidcIdToken(ID_TOKEN, Instant.now(), Instant.now().plusSeconds(60), claims); Collection<GrantedAuthority> authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER)); OidcUser user = new DefaultOidcUser(authorities, idToken); OAuth2AuthenticationToken bla = new OAuth2AuthenticationToken(user, authorities, "oidc"); securityContext.setAuthentication(bla); SecurityContextHolder.setContext(securityContext); Optional<String> login = SecurityUtils.getCurrentUserLogin(); assertThat(login).contains("admin"); }
Example 2
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 6 votes |
@Test public void testGetCurrentUserLoginForOAuth2() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); Map<String, Object> claims = new HashMap<>(); claims.put("groups", "ROLE_USER"); claims.put("sub", 123); claims.put("preferred_username", "admin"); OidcIdToken idToken = new OidcIdToken(ID_TOKEN, Instant.now(), Instant.now().plusSeconds(60), claims); Collection<GrantedAuthority> authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER)); OidcUser user = new DefaultOidcUser(authorities, idToken); OAuth2AuthenticationToken bla = new OAuth2AuthenticationToken(user, authorities, "oidc"); securityContext.setAuthentication(bla); SecurityContextHolder.setContext(securityContext); Optional<String> login = SecurityUtils.getCurrentUserLogin(); assertThat(login).contains("admin"); }
Example 3
Source File: SecurityUtilsUnitTest.java From alchemy with Apache License 2.0 | 5 votes |
@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(); }
Example 4
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@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(); }
Example 5
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@Test public void testIsAuthenticated() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin")); SecurityContextHolder.setContext(securityContext); boolean isAuthenticated = SecurityUtils.isAuthenticated(); assertThat(isAuthenticated).isTrue(); }
Example 6
Source File: SecurityUtilsUnitTest.java From cubeai with Apache License 2.0 | 5 votes |
@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(); }
Example 7
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@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(); }
Example 8
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@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(); }
Example 9
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@Test public void testIsAuthenticated() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin")); SecurityContextHolder.setContext(securityContext); boolean isAuthenticated = SecurityUtils.isAuthenticated(); assertThat(isAuthenticated).isTrue(); }
Example 10
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@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"); }
Example 11
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@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(); }
Example 12
Source File: SecurityUtilsUnitTest.java From java-microservices-examples with Apache License 2.0 | 5 votes |
@Test public void testIsAuthenticated() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin")); SecurityContextHolder.setContext(securityContext); boolean isAuthenticated = SecurityUtils.isAuthenticated(); assertThat(isAuthenticated).isTrue(); }
Example 13
Source File: JSR250SpittrServiceSecurityTest.java From Project with Apache License 2.0 | 5 votes |
private void setupUser(String... privs) { SecurityContext securityContext = SecurityContextHolder.getContext(); List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); for (String priv : privs) { authorities.add(new SimpleGrantedAuthority(priv)); } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("user", "password", authorities); securityContext.setAuthentication(authenticationToken); }
Example 14
Source File: SecurityUtilsUnitTest.java From alchemy with Apache License 2.0 | 5 votes |
@Test public void testgetCurrentUserJWT() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "token")); SecurityContextHolder.setContext(securityContext); Optional<String> jwt = SecurityUtils.getCurrentUserJWT(); assertThat(jwt).contains("token"); }
Example 15
Source File: SecurityUtilsUnitTest.java From alchemy with Apache License 2.0 | 5 votes |
@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"); }
Example 16
Source File: SecurityUtilsUnitTest.java From flair-engine with Apache License 2.0 | 5 votes |
@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(); }
Example 17
Source File: SecurityUtilsUnitTest.java From cubeai with Apache License 2.0 | 5 votes |
@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(); }
Example 18
Source File: SecurityUtilsUnitTest.java From flair-engine with Apache License 2.0 | 5 votes |
@Test public void testIsAuthenticated() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin")); SecurityContextHolder.setContext(securityContext); boolean isAuthenticated = SecurityUtils.isAuthenticated(); assertThat(isAuthenticated).isTrue(); }
Example 19
Source File: SecurityUtilsUnitTest.java From cubeai with Apache License 2.0 | 5 votes |
@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"); }
Example 20
Source File: SecurityUtilsUnitTest.java From flair-engine with Apache License 2.0 | 5 votes |
@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"); }