2015-10-19 13 views
5

मैं oauth2 के लिए अलग ऑथ और संसाधन सर्वर कॉन्फ़िगर करने का प्रयास कर रहा हूं। मैं सफलतापूर्वक लेखांकन सर्वर को कॉन्फ़िगर करने और प्रमाणीकरण और पहुंच टोकन उत्पन्न करने में सक्षम हूं। अब मैं एक संसाधन सर्वर को कॉन्फ़िगर करना चाहता हूं जो एक्सेस टोकन को सत्यापित करने के लिए एपीआई एंड पॉइंट के साथ ऑथ सर्वर से बात कर सकता है। नीचे मेरा संसाधन सर्वर कॉन्फ़िगरेशन है।स्प्रिंग ओथ 2 अलग संसाधन सर्वर कॉन्फ़िगरेशन

@Configuration 
@EnableResourceServer 
@EnableWebSecurity 
public class Oauth2SecurityConfiguration extends WebSecurityConfigurerAdapter  { 


@Override 
protected void configure(HttpSecurity http) throws Exception { 
    System.out.println("Oauth2SecurityConfiguration before"); 
    http 
       .authorizeRequests() 
       .antMatchers(HttpMethod.GET, "/api/v1/**").authenticated(); 
    System.out.println("Oauth2SecurityConfiguration after"); 
} 

@Bean 
public AccessTokenConverter accessTokenConverter() { 
    return new DefaultAccessTokenConverter(); 
} 

@Bean 
public RemoteTokenServices remoteTokenServices() { 
    final RemoteTokenServices remoteTokenServices = new RemoteTokenServices(); 
    remoteTokenServices.setCheckTokenEndpointUrl("http://localhost:9000/authserver/oauth/check_token"); 
    remoteTokenServices.setClientId("clientId"); 
    remoteTokenServices.setClientSecret("clientSecret"); 
    remoteTokenServices.setAccessTokenConverter(accessTokenConverter()); 
    return remoteTokenServices; 
} 

@Override 
@Bean 
public AuthenticationManager authenticationManager() throws Exception { 
    OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager(); 
    authenticationManager.setTokenServices(remoteTokenServices()); 
    return authenticationManager; 
} 
} 


@Configuration 
@EnableResourceServer 
public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 
    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable(); 
     System.out.println("http.csrf().disable()"); 
     http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/v1/**").fullyAuthenticated(); 
     System.out.println("http.authorizeRequests().anyRequest().authenticated()"); 
    } 
} 


@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) 
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { 

@Override 
protected MethodSecurityExpressionHandler createExpressionHandler() { 
    return new OAuth2MethodSecurityExpressionHandler(); 
} 
} 

प्रश्न: 1. कारण है कि मैं AuthenticationManager संसाधन सर्वर पर सभी प्रमाणीकरण सर्वर प्रमाणन को सौंपा जाता है, जबकि। (मुझे इसे एप्लिकेशन संदर्भ लोड करने के लिए जोड़ना पड़ा)

इसके अलावा मुझे नीचे दिए गए मुद्दों का सामना करना पड़ रहा है।

  1. भले ही मैं प्राधिकरण शीर्षलेख पास नहीं कर रहा हूं और अनुरोध के साथ टोकन एक्सेस नहीं कर रहा हूं। यह जा रहा है

    http GET "http://localhost:8080/DataPlatform/api/v1/123sw/members" 
    HTTP/1.1 200 OK 
    Content-Type: application/json;charset=UTF-8 
    Date: Mon, 19 Oct 2015 19:45:14 GMT 
    Server: Apache-Coyote/1.1 
    Transfer-Encoding: chunked 
    { 
    "entities": [], 
    "errors": [], 
    "message": null 
    } 
    
  2. फिल्टर केवल एक ही बार में मैं अनुरोध निम्न के लिए लॉग नहीं दिख रहा है लागू कर रहे हैं। क्या यह कहीं प्राधिकरण कैश करता है?

मैं वसंत ओथ के लिए नया हूं कृपया मुझे बताएं कि क्या मैं कुछ गलत कर रहा हूं। मैं

spring-security-oauth2 : 2.0.7.RELEASE 
spring-security-core : 4.0.1.RELEASE 
java : 1.8 

उत्तर

0

उपयोग कर रहा हूँ आप की जरूरत नहीं है @EnableWebSecurityOauth2SecurityConfiguration पर @EnableResourceServer पर्याप्त है। आपको को extends ResourceServerConfigurerAdapter के साथ भी बदलना चाहिए।

मैं डॉक्स में http आप

@Override 
public void configure(ResourceServerSecurityConfigurer resources) throws Exception 
{ 
    resources.tokenServices(serverConfig.getTokenServices()); 
} 
+0

साथ ResourceServerConfigurerAdapterpublic void configure(ResourceServerSecurityConfigurer resources) throws Exception ओवरराइड लेकिन मैं देख रहा हूँ सुझाव है कि आप अपने RemoteTokenServices उदाहरण उपयोग करना चाहते हैं 'इस फिल्टर आप कहीं अपने आवेदन में @EnableWebSecurity चाहिए उपयोग करने के लिए': //docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/config/annotation/web/configuration/EnableResourceServer.html –

संबंधित मुद्दे