2014-01-20 14 views
5

私はSpring 4.0の起動アプリケーションをSpringのOpenIdで起動しようとしています。春の起動と春のセキュリティでSpring 4.0を設定する方法openId

@Configuration 
@ComponentScan("x.y.z") 
@EnableAutoConfiguration 
@Import({SecurityConfig.class}) 
public class ServiceRegistryStart extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
     SpringApplication.run(ServiceRegistryStart.class, args); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     application.sources(getClass()); 
     return application; 
    } 
} 

SecurityConfig.classはこのようになります(春のセキュリティに「OpenIDの-JCサンプルプロジェクトによって影響を受け):

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/resources/**").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .openidLogin() 
       .loginPage("/login.html") 
       .permitAll() 
       .authenticationUserDetailsService(new CustomUserDetailsService()) 
       .attributeExchange("https://www.google.com/.*") 
        .attribute("email") 
         .type("http://axschema.org/contact/email") 
         .required(true) 
         .and() 
        .attribute("firstname") 
         .type("http://axschema.org/namePerson/first") 
         .required(true) 
         .and() 
        .attribute("lastname") 
         .type("http://axschema.org/namePerson/last") 
         .required(true) 
         .and() 
        .and() 
       .attributeExchange(".*yahoo.com.*") 
        .attribute("email") 
         .type("http://axschema.org/contact/email") 
         .required(true) 
         .and() 
        .attribute("fullname") 
         .type("http://axschema.org/namePerson") 
         .required(true) 
         .and() 
        .and() 
       .attributeExchange(".*myopenid.com.*") 
        .attribute("email") 
         .type("http://schema.openid.net/contact/email") 
         .required(true) 
         .and() 
        .attribute("fullname") 
         .type("http://schema.openid.net/namePerson") 
         .required(true); 
    } 

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

    class CustomUserDetailsService implements AuthenticationUserDetailsService<OpenIDAuthenticationToken> { 

     @Override 
     public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException { 
      return new User(token.getName(), "", AuthorityUtils.createAuthorityList("ROLE_USER")); 
     } 
    } 
} 

私は春のブートアプリをブートストラップするための標準的な方法を使用していますログインページは次のようになります。

<form id="googleLoginForm" action="/j_spring_openid_security_check" method="post"> 
    <h1>Login</h1> 

    <input name="openid_identifier" type="hidden" value="https://www.google.com/accounts/o8/id"/> 
    <input name="openid.ns.pape" type="hidden" value="http://specs.openid.net/extensions/pape/1.0"/> 
    <input name="openid.pape.max_auth_age" type="hidden" value="0"/> 

    <p> 
     <input name="submit" value="Login using Google" type="submit"/> 
    </p> 
</form> 

問題は「/ j_spring_openid_security_check」が存在していないようだということです私はこの問題は、私がAbstractSecurityWebApplicから延びるべきことだと思いますationInitializerでは、Spring Securityを使用していますが、起動時にはSpringBootServletInitializerを使用してください。 2つを組み合わせるにはどうすればいいですか? SpringBootServletInitializerのjavadocは、Spring Securityが検出されたときに自動的にフィルタを登録しますが、この場合は動作しないようです。

+0

OpenIDをサポートするためにpom.xmlファイルで何をしていますか?私はあなたと似たようなことをやっていて、 "spring-boot-starter-security"依存関係を含んでいますが、OpenID関連のクラスを引き出すわけではありません。私は自分自身に "spring-security-openid"依存関係を追加することができると思いますが、どのバージョンを指定するのか、Springブートバージョン(常に変化する)と同期させる方法はわかりません。私はSpring Boot Securityがこれをまだインポートしていないことに驚いています。 –

+0

スプリングブートのポイントはバージョンを指定する必要はありません。 spring-security-openidに依存関係を追加するだけで、バージョンは親のpomにすでに指定されています。 –

答えて

6

私は実際にこれを解決することができました。まず最初に私はSpringのブートを使って組み込みのコンテナを始めましたので、WebApplicationInitializersは必要ありませんでした。第二に、ログインページで投稿のURLは「/ログイン/ openidのを」指している必要があります第三に私が使用してセキュリティ設定でクロスサイトリクエストフォージェリの防止を無効にする必要がありました:SecurityConfigクラスのconfigureメソッドで

http.csrf().disable(). .. 

+0

問題が解決したらあなた自身の答えを受け入れることができます – andyb

+1

ありがとう、私はこれが可能になる前に2日待たなければなりませんでした。 – Johan

+2

ちょうど私の2セント:この回答のハイライトは、ウェブ上のどこにでも表示されるj_spring_openid_security_checkの代わりに "/ login/openid"を使うことでした –

関連する問題