2017-05-16 20 views
9

Xin chào, tôi đã bảo mật một ứng dụng web với bảo mật mùa xuân, với trang đăng nhập. Đây là Cấu hình Bảo mật của tôiOAUTH mùa xuân - đăng nhập khác nhau cho web e REST

@Configuration 
@ComponentScan("it.besmart") 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ 

    @Autowired 
    @Qualifier("customUserDetailsService") 
    UserDetailsService userDetailsService; 

    @Autowired 
    CustomSuccessHandler customSuccessHandler; 

    @Autowired 
    CustomAuthenticationFailureHandler customAuthenticationFailureHandler; 

    @Autowired 
    DataSource dataSource; 

    @Autowired 
    private ConnectionFactoryLocator connectionFactoryLocator; 

    @Autowired 
    private UsersConnectionRepository usersConnectionRepository; 

    @Autowired 
    private FacebookConnectionSignup facebookConnectionSignup; 



    private final static Logger logger = LoggerFactory.getLogger(SecurityConfiguration.class); 

    @Autowired 
    public void configureGlobalService(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 

    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 


     protected void configure(HttpSecurity http) throws Exception { 
      logger.debug("Webapp security configured"); 
      http. 

      authorizeRequests() 

        .antMatchers("/", "/register", "/registrationConfirm", "/resendRegistrationToken", "/park/**") 
        .permitAll() 

        .antMatchers("/edit/**", "/payment/**", "/plate/**", "/book/**", "/home", "/stop/**", 
          "/notification/**", "/include/**") 
        .access("hasRole('USER') or hasRole('ADMIN') or hasRole('PARK')").antMatchers("/admin/**") 
        .access("hasRole('ADMIN') or hasRole('PARK')").antMatchers("/updatePassword") 
        .hasAuthority("CHANGE_PASSWORD_PRIVILEGE") 

        .and().formLogin().loginPage("/") 
        .successHandler(customSuccessHandler).failureHandler(customAuthenticationFailureHandler) 
        .usernameParameter("email").passwordParameter("password").and().rememberMe() 
        .rememberMeParameter("remember-me").tokenRepository(persistentTokenRepository()) 
        .tokenValiditySeconds(86400).and().exceptionHandling().accessDeniedPage("/Access_Denied").and() 
        .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
        .logoutSuccessUrl("/?logout=true").permitAll(); 
     } 


     @Override 
     @Bean 
     public AuthenticationManager authenticationManagerBean() throws Exception { 
      return super.authenticationManagerBean(); 
     } 

    @Bean 
    public PersistentTokenRepository persistentTokenRepository() { 
     JdbcTokenRepositoryImpl db = new JdbcTokenRepositoryImpl(); 
     db.setDataSource(dataSource); 
     return db; 
    } 

} 

Điều này hoạt động tốt bằng cách đảm bảo tất cả ứng dụng web của tôi.

Trong cùng một ứng dụng, tôi cũng có một Máy chủ tài nguyên/ủy quyền để bảo vệ một số api REST.

Một số tài nguyên được bảo vệ bằng quyền cấp mã ủy quyền, do đó, Ứng dụng dành cho thiết bị di động không đáng tin cậy sẽ lấy mã thông báo truy cập từ ứng dụng của tôi bằng biểu mẫu đăng nhập. Tôi muốn ứng dụng sử dụng trang đăng nhập khác khi cố gắng đăng nhập từ Ứng dụng dành cho thiết bị di động.

Đây là cấu hình resourceServer tôi

@EnableResourceServer 
@ComponentScan("it.besmart.easyparking") 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class ResourceServerConfig { 

    private final Logger logger = LoggerFactory.getLogger(ResourceServerConfig.class); 

    @Autowired 
    DataSource dataSource; 

    private static final String RESOURCE_ID = "easyparking_api"; 

    @Configuration 
    // @Order(2) 
    public class grantCredentialsConfiguration extends ResourceServerConfigurerAdapter { 
     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      logger.debug("Api security configured"); 

      http 

        .requestMatchers().antMatchers("/api/oauth/**").and().authorizeRequests() 
        .antMatchers("/api/oauth/**").access("hasRole('USER')").and().formLogin().loginPage("/apilogin") 
        .permitAll(); 
     } 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 

      resources.tokenStore(tokenStore()).resourceId(RESOURCE_ID); 
     } 
    } 

    @Configuration 
    // @Order(4) 
    public class clientCredentialsConfiguration extends ResourceServerConfigurerAdapter { 
     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      logger.debug("Client security configured"); 
      http 
        .requestMatchers().antMatchers("/oauth2/**", "/api/registration", "/api/park/**").and() 
        .authorizeRequests().antMatchers("/oauth2/**", "/api/registration", "/api/park/**").authenticated(); 
     } 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 

      resources.tokenStore(tokenStore()).resourceId(RESOURCE_ID); 
     } 
    } 

    @Bean 
    public TokenStore tokenStore() { 
     return new JdbcTokenStore(dataSource); 
    } 

} 

vậy, grantCredentialsConfiguration nên chuyển hướng các yêu cầu/form apilogin, nhưng nó không, tôi đang chuyển hướng đến trang đăng nhập ứng dụng web chính ... Làm thế nào nó có thể được hoàn thành?

EDIT

Nhìn gần hơn vào các bản ghi, có vẻ như rằng khi tôi cố gắng để đạt/oauth/ủy quyền/chuỗi bảo mật thông thường diễn ra và tôi nhận được

2017-05-25 12:23:15 DEBUG o.s.security.web.FilterChainProxy[310] - /oauth/authorize?response_type=token&client_id=test&redirect_uri=https://www.getpostman.com/oauth2/callback reached end of additional filter chain; proceeding with original chain 
2017-05-25 12:23:15 DEBUG o.s.s.o.p.e.FrameworkEndpointHandlerMapping[310] - Looking up handler method for path /oauth/authorize 
2017-05-25 12:23:15 DEBUG o.s.s.o.p.e.FrameworkEndpointHandlerMapping[317] - Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(java.util.Map<java.lang.String, java.lang.Object>,java.util.Map<java.lang.String, java.lang.String>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)] 
2017-05-25 12:23:15 DEBUG o.s.s.w.a.ExceptionTranslationFilter[163] - Authentication exception occurred; redirecting to authentication entry point 
org.springframework.security.authentication.InsufficientAuthenticationException: User must be authenticated with Spring Security before authorization can be completed. 

Vì vậy, nó trông như tìm kiếm một trình xử lý để quản lý yêu cầu, thay vì chuyển hướng đến/api/apilogin như mong muốn, anh ta tìm thấy một số Authentication exception và vì vậy tôi truy cập trang đăng nhập chuẩn ... Nhưng tại sao tôi nhận ngoại lệ này?

Trả lời

0

Các bạn đã thử thêm đường dẫn URL /apilogin, đến

.antMatchers("/", "/register", "/registrationConfirm",/resendRegistrationToken", "/park/**") 
.permitAll() 

Tôi đoán ứng dụng được chuyển hướng truy cập/apilogin đến trang đăng nhập xác thực thông thường, vì nó không được bổ sung vào danh sách truy cập không được thẩm định.

+0

cảm ơn cho câu trả lời, tôi đã cố gắng, nhưng tôi vẫn tiếp tục được chuyển hướng đến trang đăng nhập ứng dụng web, apilogin không bao giờ được đưa vào tài khoản – besmart

0

Điều này xảy ra vì bạn chưa chỉ định thứ tự của các lớp cấu hình bảo mật.

Trong bảo vệ tài nguyên bảo mật mùa xuân nên được đề cập từ cụ thể đến chung chung.

Lớp SecurityConfiguration có nhiều điểm chung hơn grantCredentialsConfiguration. Vì cả hai đều bảo vệ tài nguyên sau.

  • SecurityConfiguration bảo vệ /** (Default URL)
  • grantCredentialsConfiguration /api/oauth/**

Kể từ khi trật tự không được định nghĩa, cấu hình chung SecurityConfiguration 's ẩn cấu hình cụ thể bằng cách grantCredentialsConfiguration

Để có được những công việc như mong đợi bạn sẽ phải xác định thứ tự như dưới đây.

@Configuration 
@Order(2)//Generic config should have larger value (lower priority) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ 
} 

@Configuration 
@Order(1)//Specific with lower value (higher priority) 
public class grantCredentialsConfiguration extends ResourceServerConfigurerAdapter { 

} 

Lưu ý: Vì các trang đăng nhập không phải là từ các ứng dụng khác nhau, họ chia sẻ hoặc bối cảnh an ninh SecurityContextHolder. Vì vậy, nếu bạn đăng nhập từ một trang đăng nhập và sau đó cố gắng truy cập tài nguyên được bảo vệ của trang khác, bạn sẽ không được chuyển hướng đến trang đăng nhập tiếp theo. Thay vào đó, bạn sẽ nhận được 403 (tùy thuộc vào vai trò được chỉ định bởi các trang đăng nhập khác nhau). Tại một thời điểm chỉ có thể duy trì một phiên đăng nhập.

Đây là một mẫu trên Github

https://github.com/ConsciousObserver/TestMultipleLoginPages.git

+0

Cảm ơn lời giải thích , tôi đặt Đơn đặt hàng của Cấu hình, nhưng tôi vẫn được chuyển hướng đến trang đăng nhập chính khi cố gắng truy cập/tài nguyên api/oauth ... – besmart

+0

Thử dự án mẫu github tôi đã liên kết, so sánh sự khác biệt. – 11thdimension

+0

cố gắng, mẫu hoạt động, không có của tôi ... Tôi nghĩ sự khác biệt là vì mẫu sử dụng hai 'WebSecurityConfigurerAdapter' trong khi tôi đang cố gắng sử dụng một' WebSecurityConfigurerAdapter' và một 'ResourceServerConfigurerAdapter' – besmart

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