2015-02-16 32 views
22

Tôi đã cố gắng triển khai máy chủ xác thực OAuth2 bằng cách sử dụng hướng dẫn của Dave Syer với một số nguồn cảm hứng từ JHipster. Nhưng tôi không thể tìm ra cách nó hoạt động cùng nhau.Bảo mật mùa xuân OAuth2, quyết định bảo mật?

Có vẻ như thiết lập bảo mật bằng cách sử dụng WebSecurityConfigurerAdapter được ghi đè khi tôi sử dụng ResourceServerConfigurerAdapter.

@Configuration 
@EnableResourceServer 
public class OAuth2ResourceConfig extends ResourceServerConfigurerAdapter { 

    private TokenExtractor tokenExtractor = new BearerTokenExtractor(); 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
       .addFilterAfter(contextClearer(), AbstractPreAuthenticatedProcessingFilter.class) 
       .authorizeRequests() 
       .anyRequest().authenticated().and().httpBasic(); 
    } 

    private OncePerRequestFilter contextClearer() { 
     return new OncePerRequestFilter() { 
      @Override 
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 
       if (tokenExtractor.extract(request) == null) { 
        SecurityContextHolder.clearContext(); 
       } 
       filterChain.doFilter(request, response); 
      } 
     }; 
    } 

@Component 
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

    private final AuthenticationManager authenticationManager; 

    @Autowired 
    public CustomWebSecurityConfigurerAdapter(AuthenticationManager authenticationManager) { 
     this.authenticationManager = authenticationManager; 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .parentAuthenticationManager(authenticationManager); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .formLogin() 
        .loginPage("/login").permitAll() 
       .and() 
        .authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll() 
       .and() 
        .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access") 
       .and() 
        .authorizeRequests().anyRequest().authenticated(); 
    } 
} 

Đây là mã được lấy từ một vài ví dụ khác nhau, để chúng có thể không được trộn lẫn. Nhưng tôi không thể tìm thấy một danh sách tài liệu/ví dụ tốt cho OAuth2 (không giống như Spring Boot có tài liệu tuyệt vời), vì vậy tôi gặp sự cố khi hiểu được cách thức tất cả chúng phù hợp với nhau. Nếu tôi không thêm loginForm vào ResourceServerConfigurerAdapter, nó sẽ cho tôi trái phép. Nhưng tôi đã định nghĩa nó trong WebSecurityConfigurererAdapter dưới dạng allowAll().

Đây là AuthorizationServerConfigurerAdapter:

@Configuration 
@EnableAuthorizationServer 
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Autowired 
    private JwtAccessTokenConverter jwtAccessTokenConverter; 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient("acme") 
       .secret("acmesecret") 
       .authorizedGrantTypes("authorization_code", "refresh_token", 
         "password").scopes("openid"); 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     endpoints.authenticationManager(authenticationManager).accessTokenConverter(jwtAccessTokenConverter); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
     oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()"); 
    } 
} 

Bất cứ điều gì tôi đang làm sai? Tôi có phải thiết lập tất cả bảo mật trong ResourceServerConfigurerAdapter không? Thậm chí tôi có cần đến WebSecurityConfigurerAdapter nữa không?

Nếu có ai biết bất kỳ hướng dẫn, hướng dẫn, blog hoặc bất cứ thứ gì giống nhau có thể giúp tôi cuốn đầu về cách thức hoạt động của nó, điều đó sẽ được đánh giá cao.

Trân trọng, Kenneth.

+0

'OAuth2ResourceConfig' của bạn là dư thừa theo như tôi có thể thấy. Chỉ cần nói. –

+0

Các triệu chứng (bạn đang đánh đường nào và bạn thấy gì)? Sử dụng curl (-v để xem tiêu đề) và đăng nhập DEBUG cho Spring Security sẽ cho bạn biết mọi thứ bạn cần biết. –

+0

Các triệu chứng là về cơ bản nó bỏ qua WebSecurityConfigurerAdapter. Nhưng sau khi đọc cho bạn lời giải thích dưới đây, tôi sẽ hiểu thêm một chút về cách thức hoạt động của nó. – LG87

Trả lời

21

Bạn cần WebSecurityConfigurerAdapter để đảm bảo/ủy quyền điểm cuối và cung cấp cách để người dùng xác thực. Một ứng dụng Spring Boot sẽ làm điều đó cho bạn (bằng cách thêm WebSecurityConfigurerAdapter của riêng nó với xác thực cơ sở HTTP). Nó tạo ra một chuỗi bộ lọc với thứ tự = 0 theo mặc định và bảo vệ tất cả các tài nguyên trừ khi bạn cung cấp một trình khớp yêu cầu. Các @EnableResourceServer làm một cái gì đó tương tự, nhưng chuỗi bộ lọc nó thêm là theo thứ tự = 3 theo mặc định, do đó, nó là một dự phòng tất cả bắt cho riêng của bạn WebSecurityConfigurerAdapter tại order = 0. Cấu hình của bạn trông có vẻ lành mạnh (chuỗi đăng nhập được ưu tiên, nhưng chỉ khớp với một tập hợp các yêu cầu nhỏ).

+2

Tôi phải tạo thứ tự WebSecurityConfigurerAdapter = 2 để làm cho nó hoạt động. –

+0

Bạn có chỉ định thứ tự với chú thích Đặt hàng hoặc bạn chỉ định điều này ở đâu? Tôi có cùng một vấn đề và chú thích cấu hình với Lệnh không có bất kỳ hiệu ứng nào, ResourceServerConfigurerAdapter là lớp duy nhất đang được sử dụng và WebSecurityConfigurerAdapter đang bị bỏ qua hoàn toàn. – Cenobyte321

+1

@ Cenobyte321 'thực hiện Ordered' hoặc' @Order (1) 'hoặc' @Order (2) ' – robinhowlett

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