2014-01-20 5 views
5

스프링 보안 OpenId를 사용하여 스프링 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은 다음과 같습니다 (봄 보안에서 "오픈 ID-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에서 연장한다고 때문이다. Spring Security를 ​​사용할 때 ationInitializer를 부팅 할 때 SpringBootServletInitializer를 사용해야합니다. 두 가지를 결합하는 가장 좋은 방법은 무엇입니까? SpringBootServletInitializer의 javadoc은 Spring Security가 감지 될 때 자동으로 필터를 등록하지만,이 경우에는 작동하지 않는 것으로 보입니다.

+0

OpenID를 지원하기 위해 pom.xml 파일에서 무엇을하고 있습니까? 나는 당신과 비슷한 것을하고 있는데, "spring-boot-starter-security"의존성을 포함 시켰지만, OpenID 관련 클래스들을 끌어들이지는 않습니다. 나는 "spring-security-openid"의존성을 직접 추가 할 수 있다고 생각하지만, 어떤 버전을 지정해야하는지, 그리고 Spring Boot 버전 (지속적으로 변경됨)과 동기화하는 방법을 모르겠습니다. 나는 Spring Boot Security가 이것을 이미 가져 오지 않은 것에 놀랐다. –

+0

스프링 부트 포인트가 버전을 지정하지 않아도됩니다. spring-security-openid에 종속성을 추가하기 만하면 버전은 이미 부모 pom에 지정되어 있습니다. –

답변

6

사실이 문제를 해결할 수있었습니다. 먼저 Spring Boot를 사용하여 포함 된 컨테이너를 시작 했으므로 WebApplicationInitializers가 필요하지 않았습니다. 둘째 로그인 페이지에서 게시물의 URL이 "/ 로그인/오픈 ID"를 가리켜 야 셋째 나는 사용하여 보안 구성에서 크로스 사이트 요청 위조 방지를 해제했다 다음의 SecurityConfig 클래스의 구성 방법에

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

.

+0

문제가 해결되면 직접 대답을 받아 들일 수 있습니다. – andyb

+1

고맙습니다.이 작업이 가능해지기까지 이틀 정도 기다려야했습니다. – Johan

+2

그냥 내 2 센트 :이 대답의 하이라이트는 웹상의 모든 곳에서 제공되는 j_spring_openid_security_check 대신 "/ login/openid"를 사용하는 것이 었습니다 –

관련 문제