|
27 | 27 | import org.springframework.mock.web.MockFilterChain;
|
28 | 28 | import org.springframework.mock.web.MockHttpServletRequest;
|
29 | 29 | import org.springframework.mock.web.MockHttpServletResponse;
|
| 30 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
30 | 31 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
31 | 32 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
32 | 33 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
35 | 36 | import org.springframework.security.core.GrantedAuthority;
|
36 | 37 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
37 | 38 | import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
|
| 39 | +import org.springframework.security.core.context.SecurityContextImpl; |
38 | 40 | import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
|
39 | 41 | import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
|
40 | 42 | import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
|
@@ -154,6 +156,31 @@ public void oauth2Login() throws Exception {
|
154 | 156 | .isInstanceOf(OAuth2UserAuthority.class).hasToString("ROLE_USER");
|
155 | 157 | }
|
156 | 158 |
|
| 159 | + @Test |
| 160 | + public void oauth2LoginWhenAuthenticatedThenIgnored() throws Exception { |
| 161 | + // setup application context |
| 162 | + loadConfig(OAuth2LoginConfig.class); |
| 163 | + |
| 164 | + // authenticate |
| 165 | + TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("a", |
| 166 | + "b", "ROLE_TEST"); |
| 167 | + |
| 168 | + this.request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, new SecurityContextImpl(expectedAuthentication)); |
| 169 | + |
| 170 | + // setup authentication parameters |
| 171 | + this.request.setParameter("code", "code123"); |
| 172 | + this.request.setParameter("state", "state"); |
| 173 | + |
| 174 | + // perform test |
| 175 | + this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
| 176 | + |
| 177 | + // assertions |
| 178 | + Authentication authentication = this.securityContextRepository |
| 179 | + .loadContext(new HttpRequestResponseHolder(this.request, this.response)) |
| 180 | + .getAuthentication(); |
| 181 | + assertThat(authentication).isEqualTo(expectedAuthentication); |
| 182 | + } |
| 183 | + |
157 | 184 | @Test
|
158 | 185 | public void oauth2LoginCustomWithConfigurer() throws Exception {
|
159 | 186 | // setup application context
|
|
0 commit comments