use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.
the class OAuth2ClientAuthenticationProcessingFilterTests method testAuthenticationWithTokenType.
@Test
public void testAuthenticationWithTokenType() throws Exception {
filter.setRestTemplate(restTemplate);
filter.setTokenServices(tokenServices);
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
token.setTokenType("foo");
Mockito.when(restTemplate.getAccessToken()).thenReturn(token);
Set<String> scopes = new HashSet<String>();
scopes.addAll(Arrays.asList("read", "write"));
OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("client", false, scopes);
this.authentication = new OAuth2Authentication(storedOAuth2Request, null);
Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
Authentication authentication = filter.attemptAuthentication(new MockHttpServletRequest(), null);
assertEquals("foo", ((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenType());
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.
the class OAuth2AuthenticationManagerTests method testDetailsEnhancedOnce.
@Test
public void testDetailsEnhancedOnce() throws Exception {
authentication.setDetails("DETAILS");
Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
PreAuthenticatedAuthenticationToken request = new PreAuthenticatedAuthenticationToken("FOO", "");
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
servletRequest.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, "BAR");
OAuth2AuthenticationDetails details = new OAuth2AuthenticationDetails(servletRequest);
request.setDetails(details);
Authentication result = manager.authenticate(request);
// Authenticate the same request again to simulate what happens if the app is caching the result from
// tokenServices.loadAuthentication():
result = manager.authenticate(request);
assertEquals(authentication, result);
assertEquals("BAR", ((OAuth2AuthenticationDetails) result.getDetails()).getTokenValue());
assertEquals("DETAILS", ((OAuth2AuthenticationDetails) result.getDetails()).getDecodedDetails());
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.
the class ResourceServerConfigurationTests method testCustomAuthenticationDetailsSource.
@Test
public void testCustomAuthenticationDetailsSource() throws Exception {
tokenStore.storeAccessToken(token, authentication);
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(AuthenticationDetailsSourceContext.class);
context.refresh();
MockMvc mvc = buildMockMvc(context);
mvc.perform(MockMvcRequestBuilders.get("/").header("Authorization", "Bearer FOO")).andExpect(MockMvcResultMatchers.status().isNotFound());
context.close();
OAuth2AuthenticationDetails authenticationDetails = (OAuth2AuthenticationDetails) authentication.getDetails();
assertEquals("Basic", authenticationDetails.getTokenType());
assertEquals("BAR", authenticationDetails.getTokenValue());
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-cloud-framework by zhuwj921.
the class WebContextUtil method getDetails.
/**
* 获取当前上下文token的信息
* @return
*/
public static OAuth2AuthenticationDetails getDetails() {
Authentication authentication = getAuthentication();
if (authentication == null) {
return null;
}
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
return details;
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-cloud-security by spring-cloud.
the class OAuth2TokenRelayFilter method shouldFilter.
@Override
public boolean shouldFilter() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof OAuth2Authentication) {
Object details = auth.getDetails();
if (details instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails oauth = (OAuth2AuthenticationDetails) details;
RequestContext ctx = RequestContext.getCurrentContext();
if (ctx.containsKey("proxy")) {
String id = (String) ctx.get("proxy");
if (routes.containsKey(id)) {
if (!Route.Scheme.OAUTH2.matches(routes.get(id).getScheme())) {
return false;
}
}
}
ctx.set(ACCESS_TOKEN, oauth.getTokenValue());
ctx.set(TOKEN_TYPE, oauth.getTokenType() == null ? "Bearer" : oauth.getTokenType());
return true;
}
}
return false;
}
Aggregations