以上就是给各位分享将WebSecurityConfigurerAdapter与SpringOAuth2和user-info-uri结合使用,其中也会对如何将spring加入web容器中进行解释,同时本
以上就是给各位分享将WebSecurityConfigurerAdapter与Spring OAuth2和user-info-uri结合使用,其中也会对如何将spring加入web容器中进行解释,同时本文还将给你拓展org.springframework.boot.autoconfigure.security.oauth2.method.OAuth2MethodSecurityConfiguration的实例源码、org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer的实例源码、org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateFactory的实例源码、org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices的实例源码等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- 将WebSecurityConfigurerAdapter与Spring OAuth2和user-info-uri结合使用(如何将spring加入web容器中)
- org.springframework.boot.autoconfigure.security.oauth2.method.OAuth2MethodSecurityConfiguration的实例源码
- org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer的实例源码
- org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateFactory的实例源码
- org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices的实例源码
将WebSecurityConfigurerAdapter与Spring OAuth2和user-info-uri结合使用(如何将spring加入web容器中)
我创建了一个授权服务,如下所示
@SpringBootApplication@EnableAuthorizationServerpublic class AuthorizationApplication { ...}
有了这个application.properties
。
server.port=9000security.oauth2.client.client-id=monederobingosecurity.oauth2.client.client-secret=monederobingosecretsecurity.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password,client_credentialssecurity.oauth2.client.scope=company,client
然后,在一个单独的spring boot项目中,我创建了一个资源服务器。
@SpringBootApplication@EnableResourceServerpublic class App { ...}
有了这个application.properties
。
server.port=9090spring.application.name=appsecurity.oauth2.resource.user-info-uri=http://localhost:9000/user
现在,如果我发送localhost:9090/api
带有授权服务检索到的适当令牌的此类请求,一切正常。
但是,我不想在向发送请求时发送此令牌localhost:9090/login
。
为此,我在资源服务器spring boot应用程序中创建了此类。
@Configurationpublic class SpringConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers("/login") .permitAll() .antMatchers("/api/**") .authenticated(); }}
现在,我无需发送任何令牌即可向发送请求/login
。
但是,当/api
使用有效令牌向发送请求时,我现在收到以下消息。
{ "timestamp": 1496027102659, "status": 403, "error": "Forbidden", "message": "Access Denied", "path": "/api/v1/points_configuration/314"}
如何在Spring Security OAuth2中仅为少数几个URL模式配置安全性?
答案1
小编典典请关注此以获取有关Spring OAuth安全性的更多信息: 使用OAuth保护Spring REST
Api
为了在Spring启动中实现OAuth安全性,您必须通过分别从AuthorizationServerConfigurerAdapter
和扩展它们来创建授权和资源服务器ResourceServerConfigurerAdapter
。
授权服务器
@Configuration @EnableAuthorizationServer public class AuthorizationApplication extends AuthorizationServerConfigurerAdapter{ @Autowired private UserDetailsService userDetailsService; @Autowired private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints .userDetailsService(userDetailsService) .authenticationManager(this.authenticationManager).tokenStore(tokenStore()).approvalStoreDisabled(); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.withClientDetails(mongoClientDetailsService); /*inMemory() .withClient(propertyResolver.getProperty(PROP_CLIENTID)) .scopes("read", "write") .authorities("ROLE_CLIENT") .authorizedGrantTypes("password", "refresh_token","client_credentials") .secret(propertyResolver.getProperty(PROP_SECRET)) .accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 18000));*/ }//Do others stuff }
资源服务器
此服务器配置中应提及您要使用OAuth保护的所有网址。它启用了一个Spring
Security过滤器,该过滤器使用传入的OAuth2令牌对请求进行身份验证。虽然大多数WebSecurityConfigurerAdapter
扩展类用于基本安全配置,例如添加过滤器,允许使用不安全的url或实现会话策略等。
@Configuration@EnableResourceServerpublic class App extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.requestMatchers().antMatchers("/api/**").and().authorizeRequests() .antMatchers("/api/**").authenticated();} //Do others stuff}
org.springframework.boot.autoconfigure.security.oauth2.method.OAuth2MethodSecurityConfiguration的实例源码
@Test public void testDefaultPrePostSecurityAnnotations() { this.context = new AnnotationConfigServletWebServerApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,MinimalSecureWebApplication.class); this.context.refresh(); this.context.getBean(OAuth2MethodSecurityConfiguration.class); ClientDetails config = this.context.getBean(ClientDetails.class); DelegatingMethodSecurityMetadataSource source = this.context .getBean(DelegatingMethodSecurityMetadataSource.class); List<MethodSecurityMetadataSource> sources = source .getmethodSecurityMetadataSources(); assertthat(sources.size()).isEqualTo(1); assertthat(sources.get(0).getClass().getName()) .isEqualTo(PrePostAnnotationSecurityMetadataSource.class.getName()); verifyAuthentication(config); }
@Test public void testClassicSecurityAnnotationOverride() { this.context = new AnnotationConfigServletWebServerApplicationContext(); this.context.register(SecuredEnabledConfiguration.class,MinimalSecureWebApplication.class); this.context.refresh(); this.context.getBean(OAuth2MethodSecurityConfiguration.class); ClientDetails config = this.context.getBean(ClientDetails.class); DelegatingMethodSecurityMetadataSource source = this.context .getBean(DelegatingMethodSecurityMetadataSource.class); List<MethodSecurityMetadataSource> sources = source .getmethodSecurityMetadataSources(); assertthat(sources.size()).isEqualTo(1); assertthat(sources.get(0).getClass().getName()) .isEqualTo(SecuredAnnotationSecurityMetadataSource.class.getName()); verifyAuthentication(config,HttpStatus.OK); }
@Test public void testJsr250SecurityAnnotationOverride() { this.context = new AnnotationConfigServletWebServerApplicationContext(); this.context.register(Jsr250EnabledConfiguration.class,MinimalSecureWebApplication.class); this.context.refresh(); this.context.getBean(OAuth2MethodSecurityConfiguration.class); ClientDetails config = this.context.getBean(ClientDetails.class); DelegatingMethodSecurityMetadataSource source = this.context .getBean(DelegatingMethodSecurityMetadataSource.class); List<MethodSecurityMetadataSource> sources = source .getmethodSecurityMetadataSources(); assertthat(sources.size()).isEqualTo(1); assertthat(sources.get(0).getClass().getName()) .isEqualTo(Jsr250MethodSecurityMetadataSource.class.getName()); verifyAuthentication(config,HttpStatus.OK); }
@Test public void testDefaultPrePostSecurityAnnotations() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,MinimalSecureWebApplication.class); this.context.refresh(); this.context.getBean(OAuth2MethodSecurityConfiguration.class); ClientDetails config = this.context.getBean(ClientDetails.class); DelegatingMethodSecurityMetadataSource source = this.context .getBean(DelegatingMethodSecurityMetadataSource.class); List<MethodSecurityMetadataSource> sources = source .getmethodSecurityMetadataSources(); assertthat(sources.size()).isEqualTo(1); assertthat(sources.get(0).getClass().getName()) .isEqualTo(PrePostAnnotationSecurityMetadataSource.class.getName()); verifyAuthentication(config); }
@Test public void testClassicSecurityAnnotationOverride() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(SecuredEnabledConfiguration.class,HttpStatus.OK); }
@Test public void testJsr250SecurityAnnotationOverride() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(Jsr250EnabledConfiguration.class,HttpStatus.OK); }
@Test public void testDefaultConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,MinimalSecureWebApplication.class); this.context.refresh(); this.context.getBean(AUTHORIZATION_SERVER_CONfig); this.context.getBean(RESOURCE_SERVER_CONfig); this.context.getBean(OAuth2MethodSecurityConfiguration.class); ClientDetails config = this.context.getBean(BaseClientDetails.class); AuthorizationEndpoint endpoint = this.context .getBean(AuthorizationEndpoint.class); UserApprovalHandler handler = (UserApprovalHandler) ReflectionTestUtils .getField(endpoint,"userApprovalHandler"); ClientDetailsService clientDetailsService = this.context .getBean(ClientDetailsService.class); ClientDetails clientDetails = clientDetailsService .loadClientByClientId(config.getClientId()); assertthat(AopUtils.isJdkDynamicProxy(clientDetailsService),equalTo(true)); assertthat(AopUtils.getTargetClass(clientDetailsService).getName(),is(equalTo(InMemoryClientDetailsService.class.getName()))); assertthat(handler instanceof ApprovalStoreUserApprovalHandler,equalTo(true)); assertthat(clientDetails,equalTo(config)); verifyAuthentication(config); }
@Test public void testDefaultPrePostSecurityAnnotations() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,MinimalSecureWebApplication.class); this.context.refresh(); this.context.getBean(OAuth2MethodSecurityConfiguration.class); ClientDetails config = this.context.getBean(ClientDetails.class); DelegatingMethodSecurityMetadataSource source = this.context .getBean(DelegatingMethodSecurityMetadataSource.class); List<MethodSecurityMetadataSource> sources = source .getmethodSecurityMetadataSources(); assertthat(sources.size(),equalTo(1)); assertthat(sources.get(0).getClass().getName(),equalTo(PrePostAnnotationSecurityMetadataSource.class.getName())); verifyAuthentication(config); }
@Test public void testClassicSecurityAnnotationOverride() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(SecuredEnabledConfiguration.class,equalTo(SecuredAnnotationSecurityMetadataSource.class.getName())); verifyAuthentication(config,HttpStatus.OK); }
@Test public void testJsr250SecurityAnnotationOverride() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(Jsr250EnabledConfiguration.class,equalTo(Jsr250MethodSecurityMetadataSource.class.getName())); verifyAuthentication(config,HttpStatus.OK); }
@Test public void testDefaultConfiguration() { this.context = new AnnotationConfigServletWebServerApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,"userApprovalHandler"); ClientDetailsService clientDetailsService = this.context .getBean(ClientDetailsService.class); ClientDetails clientDetails = clientDetailsService .loadClientByClientId(config.getClientId()); assertthat(AopUtils.isJdkDynamicProxy(clientDetailsService)).isTrue(); assertthat(AopUtils.getTargetClass(clientDetailsService).getName()) .isEqualTo(InMemoryClientDetailsService.class.getName()); assertthat(handler).isinstanceOf(ApprovalStoreUserApprovalHandler.class); assertthat(clientDetails).isEqualTo(config); verifyAuthentication(config); assertthat(this.context.getBeanNamesForType(OAuth2RestOperations.class)) .isEmpty(); }
@Test public void testDefaultConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,"userApprovalHandler"); ClientDetailsService clientDetailsService = this.context .getBean(ClientDetailsService.class); ClientDetails clientDetails = clientDetailsService .loadClientByClientId(config.getClientId()); assertthat(AopUtils.isJdkDynamicProxy(clientDetailsService)).isTrue(); assertthat(AopUtils.getTargetClass(clientDetailsService).getName()) .isEqualTo(InMemoryClientDetailsService.class.getName()); assertthat(handler).isinstanceOf(ApprovalStoreUserApprovalHandler.class); assertthat(clientDetails).isEqualTo(config); verifyAuthentication(config); assertthat(this.context.getBeanNamesForType(OAuth2RestOperations.class)) .isEmpty(); }
@Test public void testDefaultPrePostSecurityAnnotations() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,MinimalSecureWebApplication.class); this.context.refresh(); this.context.getBean(OAuth2MethodSecurityConfiguration.class); ClientDetails config = this.context.getBean(ClientDetails.class); DelegatingMethodSecurityMetadataSource source = this.context .getBean(DelegatingMethodSecurityMetadataSource.class); List<MethodSecurityMetadataSource> sources = source .getmethodSecurityMetadataSources(); assertthat(sources.size()).isEqualTo(1); assertthat(sources.get(0).getClass().getName()) .isEqualTo(PrePostAnnotationSecurityMetadataSource.class.getName()); verifyAuthentication(config); }
@Test public void testClassicSecurityAnnotationOverride() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(SecuredEnabledConfiguration.class,HttpStatus.OK); }
@Test public void testJsr250SecurityAnnotationOverride() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(Jsr250EnabledConfiguration.class,HttpStatus.OK); }
@Test public void testDefaultConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(AuthorizationAndResourceServerConfiguration.class,"userApprovalHandler"); ClientDetailsService clientDetailsService = this.context .getBean(ClientDetailsService.class); ClientDetails clientDetails = clientDetailsService .loadClientByClientId(config.getClientId()); assertthat(AopUtils.isJdkDynamicProxy(clientDetailsService)).isTrue(); assertthat(AopUtils.getTargetClass(clientDetailsService).getName()) .isEqualTo(InMemoryClientDetailsService.class.getName()); assertthat(handler).isinstanceOf(ApprovalStoreUserApprovalHandler.class); assertthat(clientDetails).isEqualTo(config); verifyAuthentication(config); assertthat(this.context.getBeanNamesForType(OAuth2RestOperations.class)) .isEmpty(); }
org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer的实例源码
@Bean public UserInfoRestTemplateCustomizer getUserInfoRestTemplateCustomizer() { return new UserInfoRestTemplateCustomizer() { @Override public void customize(oauth2resttemplate template) { template.setAccesstokenProvider(new MyAuthorizationCodeAccesstokenProvider()); } }; }
@Bean public UserInfoRestTemplateCustomizer userInfoRestTemplateCustomizer( TraceRestTemplateInterceptor traceRestTemplateInterceptor) { return restTemplate -> { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>( restTemplate.getInterceptors()); interceptors.add(traceRestTemplateInterceptor); restTemplate.setInterceptors(interceptors); }; }
@Bean public UserInfoRestTemplateCustomizer loadBalancedUserInfoRestTemplateCustomizer( final LoadBalancerInterceptor loadBalancerInterceptor) { return new UserInfoRestTemplateCustomizer() { @Override public void customize(oauth2resttemplate restTemplate) { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>( restTemplate.getInterceptors()); interceptors.add(loadBalancerInterceptor); restTemplate.setInterceptors(interceptors); } }; }
@Bean public UserInfoRestTemplateCustomizer retryLoadBalancedUserInfoRestTemplateCustomizer( final RetryLoadBalancerInterceptor loadBalancerInterceptor) { return new UserInfoRestTemplateCustomizer() { @Override public void customize(oauth2resttemplate restTemplate) { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>( restTemplate.getInterceptors()); interceptors.add(loadBalancerInterceptor); restTemplate.setInterceptors(interceptors); } }; }
org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateFactory的实例源码
@Test public void userInfoLoadBalancednoRetry() throws Exception { this.context = new SpringApplicationBuilder(ClientConfiguration.class) .properties("spring.config.name=test","server.port=0","security.oauth2.resource.userInfoUri:http://nosuchservice","security.oauth2.resource.loadBalanced=true") .run(); assertTrue(this.context.containsBean("loadBalancedUserInfoRestTemplateCustomizer")); assertFalse(this.context.containsBean("retryLoadBalancedUserInfoRestTemplateCustomizer")); oauth2resttemplate template = this.context .getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate(); ClientHttpRequest request = template.getRequestFactory() .createRequest(new URI("http://nosuchservice"),HttpMethod.GET); expected.expectMessage("No instances available for nosuchservice"); request.execute(); }
private oauth2clientAuthenticationProcessingFilter oauth2SsoFilter( OAuth2SsoProperties sso) { OAuth2RestOperations restTemplate = this.applicationContext .getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate(); ResourceServerTokenServices tokenServices = this.applicationContext .getBean(ResourceServerTokenServices.class); oauth2clientAuthenticationProcessingFilter filter = new oauth2clientAuthenticationProcessingFilter( sso.getLoginPath()); filter.setRestTemplate(restTemplate); filter.setTokenServices(tokenServices); filter.setApplicationEventPublisher(this.applicationContext); return filter; }
private oauth2clientAuthenticationProcessingFilter oauth2SsoFilter( OAuth2SsoProperties sso) { OAuth2RestOperations restTemplate = this.applicationContext .getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate(); ResourceServerTokenServices tokenServices = this.applicationContext .getBean(ResourceServerTokenServices.class); oauth2clientAuthenticationProcessingFilter filter = new oauth2clientAuthenticationProcessingFilter( sso.getLoginPath()); filter.setRestTemplate(restTemplate); filter.setTokenServices(tokenServices); filter.setApplicationEventPublisher(this.applicationContext); return filter; }
private oauth2clientAuthenticationProcessingFilter oauth2SsoFilter( OAuth2SsoProperties sso) { OAuth2RestOperations restTemplate = this.applicationContext .getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate(); ResourceServerTokenServices tokenServices = this.applicationContext .getBean(ResourceServerTokenServices.class); oauth2clientAuthenticationProcessingFilter filter = new oauth2clientAuthenticationProcessingFilter( sso.getLoginPath()); filter.setRestTemplate(restTemplate); filter.setTokenServices(tokenServices); filter.setApplicationEventPublisher(this.applicationContext); return filter; }
@Bean oauth2resttemplate restTemplate(UserInfoRestTemplateFactory templateFactory) { return templateFactory.getUserInfoRestTemplate(); }
org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices的实例源码
public static Filter general(AuthorizationCodeResourceDetails client,ResourceServerProperties resourceServerProperties,String path,oauth2clientContext oauth2clientContext) { oauth2clientAuthenticationProcessingFilter oauth2clientAuthenticationFilter = new oauth2clientAuthenticationProcessingFilter(path){ protected void successfulAuthentication(HttpServletRequest request,HttpServletResponse response,FilterChain chain,Authentication authResult) throws IOException,servletexception { super.successfulAuthentication(request,response,chain,authResult); OAuth2Accesstoken accesstoken = restTemplate.getAccesstoken(); log.warn(new Gson().toJson(authResult)); log.warn(new Gson().toJson(accesstoken)); } }; oauth2resttemplate oauth2resttemplate = new oauth2resttemplate(client,oauth2clientContext); oauth2clientAuthenticationFilter.setRestTemplate(oauth2resttemplate); UserInfoTokenServices tokenServices = new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(),client.getClientId()); tokenServices.setRestTemplate(oauth2resttemplate); oauth2clientAuthenticationFilter.setTokenServices(tokenServices); return oauth2clientAuthenticationFilter; }
private Filter ssoFilter() { CompositeFilter filter = new CompositeFilter(); List<Filter> filters = new ArrayList<>(); oauth2clientAuthenticationProcessingFilter facebookFilter = new oauth2clientAuthenticationProcessingFilter("/login/facebook"); oauth2resttemplate facebookTemplate = new oauth2resttemplate(facebook(),oauth2clientContext); facebookFilter.setRestTemplate(facebookTemplate); facebookFilter.setTokenServices(new UserInfoTokenServices(facebookResource().getUserInfoUri(),facebook().getClientId())); filters.add(facebookFilter); oauth2clientAuthenticationProcessingFilter googleFilter = new oauth2clientAuthenticationProcessingFilter("/login/google"); oauth2resttemplate googleTemplate = new oauth2resttemplate(google(),oauth2clientContext); googleFilter.setRestTemplate(googleTemplate); googleFilter.setTokenServices(new UserInfoTokenServices(googleResource().getUserInfoUri(),google().getClientId())); filters.add(googleFilter); filter.setFilters(filters); return filter; }
public static Filter wechat(AuthorizationCodeResourceDetails client,oauth2clientContext oauth2clientContext) { oauth2clientAuthenticationProcessingFilter oauth2clientAuthenticationFilter = new oauth2clientAuthenticationProcessingFilter(path); oauth2resttemplate oauth2resttemplate = new oauth2resttemplate(client,oauth2clientContext); AuthorizationCodeAccesstokenProvider accesstokenProvider = new AuthorizationCodeAccesstokenProvider(); accesstokenProvider.setAuthorizationRequestEnhancer((request,resource,form,headers) -> { form.set("appid",resource.getClientId()); form.set("secret",resource.getClientSecret()); form.set("scope","snsapi_userinfo"); form.set("response_type","code"); form.set("#wechat_redirect",""); }); accesstokenProvider.setMessageConverters(converters()); oauth2resttemplate.setAccesstokenProvider(accesstokenProvider); oauth2resttemplate.setRetryBadAccesstokens(true); oauth2clientAuthenticationFilter.setRestTemplate(oauth2resttemplate); UserInfoTokenServices tokenServices = new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(),client.getClientId()); tokenServices.setRestTemplate(oauth2resttemplate); oauth2clientAuthenticationFilter.setTokenServices(tokenServices); return oauth2clientAuthenticationFilter; }
@Test public void clientConfigured() throws Exception { this.context = new SpringApplicationBuilder(ClientConfiguration.class) .properties("spring.config.name=test","server.port=0","security.oauth2.resource.userInfoUri:http://example.com","security.oauth2.client.clientId=foo") .run(); RequestContextHolder.setRequestAttributes( new ServletRequestAttributes(new MockHttpServletRequest())); oauth2clientContext client = this.context.getBean(oauth2clientContext.class); assertNull(client.getAccesstoken()); UserInfoTokenServices services = context.getBean(UserInfoTokenServices.class); oauth2resttemplate template = (oauth2resttemplate) ReflectionTestUtils .getField(services,"restTemplate"); MockRestServiceServer server = MockRestServiceServer.createServer(template); server.expect(requestTo("http://example.com")) .andRespond(withSuccess("{\"id\":\"user\"}",MediaType.APPLICATION_JSON)); services.loadAuthentication("FOO"); assertEquals("FOO",client.getAccesstoken().getValue()); server.verify(); }
private Filter ssoFilter() { oauth2clientAuthenticationProcessingFilter facebookFilter = new oauth2clientAuthenticationProcessingFilter("/login"); oauth2resttemplate facebookTemplate = new oauth2resttemplate(facebook(),oauth2clientContext); facebookFilter.setRestTemplate(facebookTemplate); UserInfoTokenServices tokenServices = new UserInfoTokenServices(facebookResource().getUserInfoUri(),facebook().getClientId()); tokenServices.setRestTemplate(facebookTemplate); facebookFilter.setTokenServices(tokenServices); SavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler = new SavedRequestAwareAuthenticationSuccessHandler(); authenticationSuccessHandler.setUseReferer(true); authenticationSuccessHandler.setTargetUrlParameter("continue"); facebookFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler); return facebookFilter; }
@Override public Set<Relationship> detect() { if (tokenService instanceof RemotetokenServices || tokenService instanceof UserInfoTokenServices) { return Dependency.on(Component.of(getDefaultName(),ComponentType.HTTP_APPLICATION)).asRelationshipSet(); } return Collections.emptySet(); }
@Test public void usingUserInfoTokenServicesShouldReturnDependency() { ResourceServerTokenServices tokenService = new UserInfoTokenServices("/info","nope"); detector = new AuthorizationServerRelationshipDetector(tokenService); Set<Relationship> expected = new HashSet<>(Arrays .asList(Dependency.on(Component.of("oauth2-authorization-server",ComponentType.HTTP_APPLICATION)))); Set<Relationship> result = detector.detect(); Assertions.assertthat(result).isEqualTo(expected); }
private Filter ssoFilter() { oauth2clientAuthenticationProcessingFilter facebookFilter = new oauth2clientAuthenticationProcessingFilter( "/login/facebook"); oauth2resttemplate facebookTemplate = new oauth2resttemplate(facebook(),facebook().getClientId()); tokenServices.setRestTemplate(facebookTemplate); facebookFilter.setTokenServices( new UserInfoTokenServices(facebookResource().getUserInfoUri(),facebook().getClientId())); return facebookFilter; }
private oauth2clientAuthenticationProcessingFilter ssoFilter(String provideName,ClientResources clientResources) { oauth2clientAuthenticationProcessingFilter filter = new oauth2clientAuthenticationProcessingFilter(format("/login/%s",provideName)); filter.setRestTemplate(new oauth2resttemplate(clientResources.getClient(),oauth2clientContext)); filter.setTokenServices(new UserInfoTokenServices(clientResources.getResource().getUserInfoUri(),clientResources.getClient().getClientId())); filter.setApplicationEventPublisher(applicationEventPublisher); return filter; }
private Filter ssoFilter(ClientResources client,String path) { oauth2clientAuthenticationProcessingFilter filter = new oauth2clientAuthenticationProcessingFilter(path); oauth2resttemplate template = new oauth2resttemplate(client.getClient(),oauth2clientContext); filter.setRestTemplate(template); filter.setTokenServices(new UserInfoTokenServices(client.getResource().getUserInfoUri(),client.getClient().getClientId())); return filter; }
private Filter ssoFilter() { oauth2clientAuthenticationProcessingFilter eveFilter = new oauth2clientAuthenticationProcessingFilter("/login/eve"); oauth2resttemplate eveTemplate = new oauth2resttemplate(eve(),oauth2clientContext); eveFilter.setRestTemplate(eveTemplate); eveFilter.setTokenServices(new UserInfoTokenServices(eveResource().getUserInfoUri(),eve().getClientId())); return eveFilter; }
/** * ## todo : describe. * * @param client {@link ClientResources} * @param processUrl {@link String} * @return {@link Filter} */ private Filter customSSOFilter(final ClientResources client,final String processUrl) { final oauth2clientAuthenticationProcessingFilter filter = new oauth2clientAuthenticationProcessingFilter(processUrl); final oauth2resttemplate template = new oauth2resttemplate(client.getClient(),oauth2clientContext); filter.setRestTemplate(template); final UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),client.getClient().getClientId()); tokenServices.setRestTemplate(template); filter.setTokenServices(tokenServices); return filter; }
public CustomOAuthAuthenticationProcessingFilter(String path,ClientResourceDetails clientResourceDetails,MappingJackson2HttpMessageConverter jsonMessageConverter) { super(path); this.clientResourceDetails = clientResourceDetails; this.tokenService = new UserInfoTokenServices(clientResourceDetails.getResource().getUserInfoUri(),clientResourceDetails.getClient().getClientId()); this.accesstokenProvider.setStateMandatory(false); this.jsonMessageConverter = jsonMessageConverter; }
private oauth2clientAuthenticationProcessingFilter createSsoFilter(ClientResourceDetails clientDetails,AuthenticationSuccessHandler successHandler,String path) { oauth2clientAuthenticationProcessingFilter ssoFilter = new oauth2clientAuthenticationProcessingFilter(path); ssoFilter.setAllowSessionCreation(false); oauth2resttemplate restTemplate = new oauth2resttemplate(clientDetails.getClient(),oauth2clientContext); ssoFilter.setRestTemplate(restTemplate); ssoFilter.setTokenServices(new UserInfoTokenServices(clientDetails.getResource().getUserInfoUri(),clientDetails.getClient().getClientId())); ssoFilter.setAuthenticationSuccessHandler(successHandler); return ssoFilter; }
private Filter ssoFilter(ClientResources client,String path) { oauth2clientAuthenticationProcessingFilter filter = new oauth2clientAuthenticationProcessingFilter(path); oauth2resttemplate template = new oauth2resttemplate(client.getClient(),oauth2clientContext); filter.setRestTemplate(template); UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),client.getClient().getClientId()); tokenServices.setRestTemplate(template); filter.setTokenServices(tokenServices); return filter; }
private Filter ssoFilter(ClientResources client,String path) { oauth2clientAuthenticationProcessingFilter oauth2clientAuthenticationFilter = new oauth2clientAuthenticationProcessingFilter(path); oauth2resttemplate oauth2resttemplate = new oauth2resttemplate(client.getClient(),oauth2clientContext); oauth2clientAuthenticationFilter.setRestTemplate(oauth2resttemplate); UserInfoTokenServices tokenServices = new UserInfoTokenServices( client.getResource().getUserInfoUri(),client.getClient().getClientId()); tokenServices.setRestTemplate(oauth2resttemplate); oauth2clientAuthenticationFilter.setTokenServices(tokenServices); return oauth2clientAuthenticationFilter; }
@Bean @Primary public ResourceServerTokenServices tokenServices() { return new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(),resourceServerProperties.getClientId()); }
private oauth2clientAuthenticationProcessingFilter filter() { oauth2clientAuthenticationProcessingFilter oAuth2Filter = new oauth2clientAuthenticationProcessingFilter("/google/login"); oauth2resttemplate oauth2resttemplate = new oauth2resttemplate(authCodeResourceDetails,oauth2clientContext); oAuth2Filter.setRestTemplate(oauth2resttemplate); oAuth2Filter.setTokenServices(new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(),resourceServerProperties.getClientId())); return oAuth2Filter; }
关于将WebSecurityConfigurerAdapter与Spring OAuth2和user-info-uri结合使用和如何将spring加入web容器中的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于org.springframework.boot.autoconfigure.security.oauth2.method.OAuth2MethodSecurityConfiguration的实例源码、org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer的实例源码、org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateFactory的实例源码、org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices的实例源码等相关知识的信息别忘了在本站进行查找喔。
本文标签: