2015-10-19 24 views
5

Tôi đang cố định cấu hình các máy chủ xác thực và tài nguyên riêng biệt cho oauth2. Tôi có thể định cấu hình máy chủ xác thực thành công và có thể xác thực và tạo mã thông báo truy cập. Bây giờ tôi muốn cấu hình một máy chủ tài nguyên có thể nói chuyện với máy chủ auth với điểm kết thúc api để xác thực các mã thông báo truy cập. Dưới đây là cấu hình máy chủ tài nguyên của tôi.Cấu hình máy chủ tài nguyên riêng biệt Spring Oauth2

@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(); 
} 
} 

Câu hỏi: 1. lý do tại sao tôi AuthenticationManager tại máy chủ tài nguyên trong khi tất cả xác thực được ủy quyền cho máy chủ xác thực. (Tôi phải thêm nó để tải bối cảnh ứng dụng)

Ngoài việc này tôi đang đối mặt với các vấn đề bên dưới.

  1. Mặc dù tôi không chuyển các tiêu đề ủy quyền và mã thông báo truy cập theo yêu cầu. Nó đang trải qua.

    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. Bộ lọc chỉ được gọi cùng một lúc Tôi không thấy nhật ký cho các yêu cầu sau. Liệu nó có cấp phép cache ở đâu đó không?

Tôi mới đến mùa xuân oauth Vui lòng cho tôi biết nếu tôi làm gì sai. Tôi đang sử dụng

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

Trả lời

0

Bạn không cần @EnableWebSecurity trên Oauth2SecurityConfiguration@EnableResourceServer là đủ. Bạn cũng nên thay thế extends WebSecurityConfigurerAdapter bằng extends ResourceServerConfigurerAdapter.

Nếu bạn muốn sử dụng RemoteTokenServices dụ của bạn tôi khuyên bạn nên ghi đè ResourceServerConfigurerAdapterpublic void configure(ResourceServerSecurityConfigurer resources) throws Exception với

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

Nhưng tôi đang nhìn thấy 'Để sử dụng bộ lọc này bạn phải @EnableWebSecurity nơi nào đó trong ứng dụng của bạn, trong tài liệu http: //docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/config/annotation/web/configuration/EnableResourceServer.html –

Các vấn đề liên quan