2015-01-05 21 views
28

特定春のセキュリティHTTPベーシック - 注釈

で私は、特定のURLパターンのためのHTTP基本認証を持っていると思います。

詳細に

私は自分のアプリケーションのためのAPIインタフェースを作成していますし、それは簡単なHTTP基本認証によって認証される必要があります。しかし、他のウェブページはでなければ、はHTTPの基本的なものを使用するのではなく、通常のフォームログインを使用するべきです。

現在の構成 - 2日を待っていた

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http //HTTP Security 
      .csrf().disable() //Disable CSRF 
      .authorizeRequests() //Authorize Request Configuration 
       .antMatchers("/connect/**").permitAll() 
       .antMatchers("/", "/register").permitAll() 
       .antMatchers("/admin/**").hasRole("ADMIN") 
       .antMatchers("/api/**").hasRole("API") 
       .anyRequest().authenticated() 
      .and() //HTTP basic Authentication only for API 
       .antMatcher("/api/**").httpBasic() 
      .and() //Login Form configuration for all others 
       .formLogin().loginPage("/login").permitAll() 
      .and() //Logout Form configuration 
       .logout().permitAll(); 

} 

答えて

41

を動作していないし、ここに任意のヘルプを取得できませんでした。しかし、私の研究は私にソリューションを提供:)

ソリューション

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

    @Autowired 
    private AuthenticationProvider authenticationProvider; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(authenticationProvider); 
    } 

    @Configuration 
    @Order(1) 
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{ 
     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.csrf().disable() 
        .antMatcher("/api/**") 
        .authorizeRequests() 
         .anyRequest().hasAnyRole("ADMIN", "API") 
         .and() 
        .httpBasic(); 
     } 
    } 

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

     @Override 
     public void configure(WebSecurity web) throws Exception { 
      web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/lib/**"); 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.csrf().disable() //HTTP with Disable CSRF 
        .authorizeRequests() //Authorize Request Configuration 
         .antMatchers("/connect/**").permitAll() 
         .antMatchers("/", "/register").permitAll() 
         .antMatchers("/admin/**").hasRole("ADMIN") 
         .anyRequest().authenticated() 
         .and() //Login Form configuration for all others 
        .formLogin() 
         .loginPage("/login").permitAll() 
         .and() //Logout Form configuration 
        .logout().permitAll(); 
     } 
    } 
} 
+0

その基本的なhttp authポップアップを削除する方法を教えてください。私はすべてを試みましたが、そうすることができませんでした。 – 89n3ur0n

+0

httpBasicを使用しないで、formLoginを使用してください。 –

+0

は、成功しなかったので、httpBasic()。disable()で無効にしてformLogin()を使用しましたが、修正されていないようです。 – 89n3ur0n

0

を、それが役立つことがあれば、私は知らないが、私は上記のソリューションを実装することができませんでした。私は、単一のセキュリティ

@ConfigurationクラスhttpBasic()とformLogin()は、構成の両方で

WebSecurityConfigurerAdapter

を拡張

を定義回避策を発見しました。それから私は

CustomAuthEntryPointが始まるメソッドでこのロジックを持っている

AuthenticationEntryPoint

実装するカスタム作成:

@Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException 
    { 
     String urlContext = UtilityClass.extractUrlContext(request); 
     if (!urlContext.equals(API_URL_PREFIX)) 
     { 
      String redirectUrl = "urlOfFormLogin" 
      response.sendRedirect(request.getContextPath() + redirectUrl); 
     } 
     else 
     { 
      response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 
     } 

知らんこの問題について、「ベストプラクティス戦略」である

関連する問題