2015-11-16 15 views
8

同じRESTサービスに対して基本認証とフォームログインを設定する方法はありますか?私はログインしたユーザがログインした後、そしてコマンドラインを実行してからログインした後にウェブブラウザを介してこのサービスを起動させたいと思っています。 今この投稿を見ました:Basic and form based authentication with Spring security Javaconfig です。 これを春に設定する方法はありますか? 私が今持っていることはこれです:同じREST APIの基本認証とフォームログインの結合

package com.my.company.my.app.security; 

import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.security.crypto.password.PasswordEncoder; 
import org.springframework.security.provisioning.JdbcUserDetailsManager; 

import javax.sql.DataSource; 

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    DataSource dataSource; 

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SecurityConfig.class); 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
       .antMatchers("/js/**", "/css/**") 
       .permitAll(); 

     http 
       .authorizeRequests() 
       .antMatchers("/api/**") 
       .authenticated() 
       .and() 
       .httpBasic(); 

     http 
       .authorizeRequests() 
       .antMatchers("/","/index") 
       .authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .loginProcessingUrl("/j_spring_security_check") 
       .defaultSuccessUrl("/monitor") 
       .failureUrl("/login?error") 
       .usernameParameter("j_username") 
       .passwordParameter("j_password") 
       .permitAll() 
       .and() 
       .logout() 
       .logoutUrl("/j_spring_security_logout") 
       .logoutSuccessUrl("/login?logout") 
       .permitAll() 
       .and() 
       .csrf() 
       .disable(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.jdbcAuthentication().dataSource(dataSource) 
       .passwordEncoder(passwordEncoder()) 
       .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?") 
       .authoritiesByUsernameQuery("SELECT username, authority FROM authorities WHERE username=?"); 
    } 

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

唯一の問題はhostname.com/indexまたはhostname.com/は、ウィンドウのポップ・アップは、基本認証の資格情報を求めて代わりに呼び出されたとき、それは私のログインページにリダイレクトしていないことです。

+0

答えを受け入れてくれてありがとう。あなたが気にしないなら、あなたは私に最高票を与えることができます! – SyntaX

+1

確かです。ご協力いただきありがとうございます。 –

答えて

13

以下のように複数のhttp構成を使用することで、これを簡単に達成できます。このコードでは、複数のhttp構成についてのみ説明しています。私はあなたが、セキュリティなどauthenticationMangerなどを春に関連する他の重要な構成のよく知っていると仮定しています

@EnableWebSecurity 
    public class MultiHttpSecurityCustomConfig { 
     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("user").password("password").roles("USER").and().withUser("admin").password("password") 
        .roles("USER", "ADMIN"); 
     } 

     @Configuration 
     @Order(1) 
     public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 
      protected void configure(HttpSecurity http) throws Exception { 
       http.antMatcher("/api/**").authorizeRequests().anyRequest().hasRole("ADMIN").and().httpBasic(); 
      } 
     } 

     @Configuration 
     public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

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


    } 
} 

春のセキュリティの公式のリンクを参照してください:Multiple HttpSecurity

私はまた、あなたがSecure REST Services with Spring Security

をチェックアウトするreccomendます

問題が発生した場合は、お気軽にコメントしてください。