2014-01-20 360 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 –