9

Ứng dụng của tôi nhận được tiêu đề yêu cầu AUTH_USER với tên người dùng từ Oracle Access Manager SSO. Bảo mật mùa xuân "Các chủ đề bổ sung" 2.2.1 có một ví dụ về "PreAuth" mà dường như là những gì tôi cần, nhưng không phải là một ví dụ làm việc đầy đủ.Làm cách nào để thiết lập xác thực dựa trên tiêu đề xác thực trước trong Khởi động mùa xuân?

Đoạn mã dưới đây là từ tài liệu/ví dụ, không hoạt động dựa trên cấu hình chú thích.

SiteMinder Ví dụ cấu hình - sử dụng XML với một RequestHeaderAuthenticationFilterPreAuthenticatedAuthenticationProvider và UserDetailsService để tra cứu người sử dụng.

Bản đồ này thành cấu hình dựa trên Java như thế nào?

<security:http> 
    <!-- Additional http configuration omitted --> 
    <security:custom-filter position="PRE_AUTH_FILTER" ref="siteminderFilter" /> 
</security:http> 

<bean id="siteminderFilter" class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter"> 
    <property name="principalRequestHeader" value="AUTH_USER"/> 
    <property name="authenticationManager" ref="authenticationManager" /> 
</bean> 

<bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth. PreAuthenticatedAuthenticationProvider"> 
    <property name="preAuthenticatedUserDetailsService"> 
    <bean id="userDetailsServiceWrapper" 
      class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper"> 
     <property name="userDetailsService" ref="userDetailsService"/> 
    </bean> 
    </property> 
</bean> 

<security:authentication-manager alias="authenticationManager"> 
    <security:authentication-provider ref="preauthAuthProvider" /> 
</security:authentication-manager> 

Ví dụ về bảo mật mùa xuân có thiết lập hoàn toàn khác (cấu hình XML thậm chí còn đáng sợ hơn). Không đề cập đến bộ lọc xác thực trước hoặc cách đặt tên tiêu đề.

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/login","/resources/**").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .jee() 
       .mappableRoles("USER","ADMIN"); 
    } 
} 

Mùa xuân-boot-mẫu-web-an toàn kéo dài WebMvcConfigurerAdapter thay vì WebSecurityConfigurerAdapter, và chỉ cần thực hiện đăng nhập form-based cơ bản, không có thông tin về cách để có được userid từ tiêu đề AUTH_USER pre-auth.

public class SampleWebSecureApplication extends WebMvcConfigurerAdapter { 

... omitted... 

    @Bean 
    public ApplicationSecurity applicationSecurity() { 
     return new ApplicationSecurity(); 
    } 

    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter { 

     @Autowired 
     private SecurityProperties security; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin() 
        .loginPage("/login").failureUrl("/login?error").permitAll(); 
     } 
    } 

} 

Tôi đã đọc nhiều tài liệu tham khảo/bài viết nhưng dường như chúng không liên quan đến mã hiện tại và Khởi động mùa xuân, vì vậy hãy cố gắng hiểu cách định cấu hình bảo mật trước ứng dụng.

+0

Giải thích những hành vi bạn đang nhìn thấy sẽ giúp rất nhiều. Điều đó nói rằng, mô tả của bạn nói rằng bạn đang nhận AUTH_USER, nhưng 'principalRequestHeader' được đặt thành" SM_USER ". –

+0

Cảm ơn, Devon. Hành vi là tôi không thể nhận được cấu hình Spring thông qua các chú thích để thành công. Ở trên là các đoạn mã từ các ví dụ. Các biến thể làm cho nó khó khăn để chắc chắn những gì tôi thậm chí nên được tạo ra: tức là. WebSecurytConfigurerAdapter hoặc WebMvcConfigurerAdpater? Khi tôi cố gắng cấu hình RequestHeaderAuthenticationFilter, nó yêu cầu một số bean lồng nhau như AuthenticationManager và AuthProvider và DetailsService, nhưng đối tượng http HttpSecurity cũng có các phương thức để thiết lập authprovider và dịch vụ chi tiết, nhưng có các kiểu hơi khác nhau. – Tim

+0

Chỉ cần xem [Spring Security 3.2 Webinar] (http://spring.io/blog/2014/01/21/webinar-replay-spring-security-3-2) đã giúp rất nhiều trong việc hiểu một số chú thích và cấu hình . Tiếp theo, tôi sẽ đọc toàn bộ [Tham chiếu bảo mật mùa xuân] (http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#preauth) – Tim

Trả lời

0

Đây là cách của tôi để cấu hình bảo mật trước auth dựa trên userService tiêm:

@Configuration 
@EnableWebSecurity 
@EnableWebMvcSecurity 
@EnableGlobalMethodSecurity(jsr250Enabled = true, securedEnabled = true, prePostEnabled = true, proxyTargetClass = true) 
public class ApplicationSecurity extends WebSecurityConfigurerAdapter { 

    private static final Logger LOG = Logger.getLogger(ApplicationSecurity.class.getName()); 

    @Autowired 
    private UserService userService; // implements AuthenticationUserDetailsService... 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     LOG.info("configure autentication provider with custom userService"); 
     PreAuthenticatedAuthenticationProvider paaProvider = new PreAuthenticatedAuthenticationProvider(); 
     paaProvider.setPreAuthenticatedUserDetailsService(userService); 
     auth.authenticationProvider(paaProvider); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     LOG.info("configure autentication filter"); 
     // ... 
    } 

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