2016-06-23 7 views
1

私は認証に関して巨大な問題を抱えています。私は2つのタイプの認証を持っています.1つはWebページであり、もう1つはWebサービスで動作しません。実際、資格情報を与えなくても、サーバーは自分の認証を受け入れ、先に進む。それは大きな問題であり、私は理由を理解できません。スプリングセキュリティ:httpの基本ログインとフォームログインによる認証

1)外部Webサービスは/ client/* URLであり、これらは認証されている必要があります。 POST /クライアント/ファイルには認証が必要ではありません。他のすべてのWebサービスは提供されないため、アクセス不能または認証が可能です。これはワンショット認証です。ユーザが有効になっているかどうかを確認してWebサービスの結果を返します。

2)内部Webサービスが動作しているようです。

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true) 
@PropertySource(value = { "classpath:application.properties" }) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 


    @Autowired 
    AuthenticationConfiguration authenticationConfiguration; 

    @Configuration 
    protected static class AuthenticationConfiguration implements 
    AuthenticationProvider { 

     @Autowired 
     private UserServices userServices; 
     @Autowired 
     LdapServices ldapServices; 

     @Override 
     public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
      Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); 
      String name = authentication.getName(); 
      String password = authentication.getCredentials().toString(); 
      boolean isFind = ldapServices.ldapSearch(name, password);       
      if (isFind){ 
       com.domain.User user = userServices.getByUsersEnabled(name); 
       if (user!=null) 
        authorities.add(new SimpleGrantedAuthority("ROLE_"+user.getRole().getRole()));   
       return new UsernamePasswordAuthenticationToken(name, password, authorities); 
      }   
      else return null; 
     } 


     @Override 
     public boolean supports(Class<?> authentication) { 
      return authentication.equals(UsernamePasswordAuthenticationToken.class); 
     } 
    } 

    @Autowired 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(authenticationConfiguration); 
    } 

    @Configuration 
    @Order(1) 
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{ 
     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
     .csrf().disable() 
     .antMatcher("/client/**") 
     .authorizeRequests() 
     //Exclude send file from authentication because it doesn't work with spring authentication 
     .antMatchers(HttpMethod.POST, "/client/file").permitAll() 
     .anyRequest().authenticated() 
     .and() 
     .httpBasic();  
     } 
    } 

    @Configuration 
    @Order(2) 
    public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{ 

     @Autowired 
     RoleServices roleServices; 

     @Override 
     public void configure(WebSecurity web) throws Exception { 
      web 
      //Spring Security ignores request to static resources such as CSS or JS files. 
      .ignoring() 
      .antMatchers("/static/**"); 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception {   
      List<Role> roles=roleServices.getRoles(); 
      //Retrieve array of roles(only string field without id) 
      String[] rolesArray = new String[roles.size()]; 
      int i=0; 
      for (Role role:roles){ 
       rolesArray[i++] = role.getRole(); 
      } 

      http 
      .authorizeRequests() //Authorize Request Configuration 
      .anyRequest().hasAnyRole(rolesArray)//.authenticated() 
      .and() //Login Form configuration for all others 
      .formLogin() 
      .loginPage("/login") 
      //important because otherwise it goes in a loop because login page require authentication and authentication require login page 
      .permitAll() 
      .and() 
      //redirect on page 403 if user doens't exist in DART database 
      .exceptionHandling().accessDeniedPage("/403") 
      .and() 
      .logout() 
      .logoutSuccessUrl("/login?logout") 
      .permitAll(); 

     } 
    } 
} 

答えて

0

これは新しいコードで、動作しているようです:すべての/client URLリクエスト.antMatcher("/client/**")キャッチと

@Configuration 
@Order(1) 
public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{ 
    @Autowired 
    RoleServices roleServices; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     List<Role> roles = roleServices.getRoles(); 
     //Retrieve array of roles(only string field without id) 
     String[] rolesArray = new String[roles.size()]; 
     int i=0; 
     for (Role role:roles){ 
      rolesArray[i++] = role.getRole(); 
     } 

     http 
     .csrf().disable() 
     .antMatcher("/client/**") 
     .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
     .and() 
     .authorizeRequests() 
     //Exclude send file from authentication because it doesn't work with spring authentication 
     .antMatchers(HttpMethod.POST, "/client/file").permitAll() 
     .anyRequest().hasAnyRole(rolesArray) 
     .and() 
     .httpBasic(); 
    } 
} 

とhttpBasic認証を使用し、.anyRequest().hasAnyRole(rolesArray)は唯一の役割を持つユーザーの認証を可能にします。 .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)は、Spring Securityでセッションが使用されないことを保証します。 Cookieは使用されていないため、すべてのリクエストを再認証する必要があります。このステートレスアーキテクチャは、REST APIとステートレス制約に適しています。