2014-01-20 359 views
5

我想要使用Spring Security OpenId啓動並運行Spring 4.0引導應用程序。我使用的是標準的方式來啓動一個春天啓動的應用程序:如何使用彈簧引導和彈簧安全性配置彈簧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。結合這兩者的最佳方式是什麼? SpringBootServletInitializer的javadoc表示它在檢測到Spring Security時自動註冊一個過濾器,但在這種情況下似乎不起作用。

+0

你在你的pom.xml文件中做什麼來獲得OpenID支持?我正在做類似於你的事情,並且包含了「spring-boot-starter-security」依賴,但是這並沒有引入與OpenID相關的類。我想我可以自己添加「spring-security-openid」依賴項,但我不確定要指定哪個版本,以及如何保持它與Spring Boot版本(不斷更改)的同步。我很驚訝Spring Boot Security不會導入它。 –

+0

春季啓動的重點不在於指定版本。只需將依賴關係添加到spring-security-openid中,該版本已經在父pom中指定。 –

回答

6

我真的設法解決這個問題。首先,我使用Spring Boot來啓動一個嵌入式容器,因此我不需要任何WebApplicationInitializers。其次文章網址登錄頁面應該指向「/登錄/ OpenID的」;第三,我在安全配置禁用跨站點請求僞造預防使用:

http.csrf().disable(). .. 
在SecurityConfig類的配置方法

+0

如果它解決了問題,您可以接受您自己的答案 – andyb

+1

謝謝,儘管如此,在此之前,我必須等待兩天。 – Johan

+2

只需要我2美分:這個答案的亮點是使用「/ login/openid」而不是在網上隨處可見的j_spring_openid_security_check –