2015-07-18 23 views
7

Tôi đang theo dõi Part V of Getting Started with Spring Boot Security để bảo mật các dịch vụ nhỏ của RESTful.Bảo mật khởi động mùa xuân OAuth2 với Mẫu đăng nhập

Dòng chảy đơn giản mà tôi có ý định thực hiện là: -

  1. Nếu không được thẩm định, người dùng sẽ được chuyển hướng đến một trang đăng nhập tùy chỉnh tại nói '/ đăng nhập.

  2. Người dùng cung cấp bằng chứng xác thực của mình.

  3. Khi người dùng xác thực thành công được chuyển hướng đến trang chủ ('/ home'). Tôi có thể truy cập điểm cuối REST của tôi (sau một Máy chủ proxy Zuul ) sau khi cung cấp mã thông báo truy cập trong yêu cầu.

Hướng dẫn bắt đầu trong liên kết được đề cập ở trên sử dụng Basic Auth và người dùng giả được định cấu hình trong tệp .properties hoặc .yml.

Đây là cách tôi đã cố gắng với cấu hình của tôi: -

@Configuration 
@EnableAuthorizationServer 
public class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

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

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

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

} 



@Configuration 
@Import({ OptoSoftSecurityServiceConfig.class }) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserDetailsService userDetailsService; // backed by MongoDB 

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

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.httpBasic().disable().formLogin();// disabled basic auth and configured to use dafault Spring Security form login. 
    } 
} 

Va vào authorization endpoint chuyển hướng tôi đến 'http://localhost:9999/uaa/login' với thông báo lỗi như: -

<oauth> 
<error_description> 
Full authentication is required to access this resource 
</error_description> 
<error>unauthorized</error> 
</oauth> 

VẤN ĐỀ

  1. Cách ca n Tôi định cấu hình máy chủ ủy quyền để sử dụng UserDetailsService thay vì người dùng tĩnh và sử dụng Đăng nhập biểu mẫu thay vì Xác thực cơ bản.

  2. Tôi làm cách nào để định cấu hình Tự động phê duyệt trong khi sử dụng 'authorization_code' làm loại trợ cấp?

  3. Có phải bắt buộc/oauth/ủy quyền điểm cuối để được bảo vệ bởi Xác thực cơ bản không? Tại sao 'Yêu cầu xác thực đầy đủ là bắt buộc' để truy cập vào điểm cuối /oauth/ủy quyền '. Tôi tin rằng chúng tôi không biết ai là người dùng trước điểm cuối này. Người dùng chỉ có thể được xác định khi anh ấy đã được xác thực bằng thông tin đăng nhập hợp lệ xuất hiện sau khi đăng ký hình thức .

Trả lời

4

Cuối cùng nó hoạt động. Các repo git trong blog đã đề cập đã có điều này được cấu hình. Hóa ra nó khá thẳng về phía trước.

Đây là những gì làm việc cho tôi (tôi cũng đã cấu hình chính auto true): -

** 
* @author kumar 
* 
*/ 
@SpringBootApplication 
public class AuthenticationServerApplication { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     SpringApplication.run(AuthenticationServerApplication.class, args); 

    } 

    @Configuration 
    protected static class LoginConfig extends WebSecurityConfigurerAdapter { 

     @Autowired 
     private AuthenticationManager authenticationManager; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.formLogin().permitAll().and().authorizeRequests().anyRequest().authenticated();//.and().userDetailsService(yourCustomerUserDetailsService); 
     } 

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

    @Configuration 
    @EnableAuthorizationServer 
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

     @Autowired 
     private AuthenticationManager authenticationManager; 

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

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

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

    } 

} 

application.yml: -

security: 
     user: 
     password: password 
    server: 
     port: 9999 
     context-path: /uaa 
+0

Ông có thể vui lòng cung cấp cho tôi những url git repo cho ví dụ này? – DIVA

+0

Hi Kumar, repo github không được tìm thấy. Tôi có thể có mã nguồn không? – tranceholic

+2

@tranceholic Bạn có thể kiểm tra của tôi: https://github.com/ksambhav/trueyes hoặc hướng dẫn mùa xuân của Daye Syer: https: // github.com/spring-guides/tut-spring-security-và-angular-js/cây/master/oauth2 –

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