17

나는 데이브 Syer에서 기본 봄 부팅으로 OAuth2 다음 예제있어 기본 이외의 암호를 부여 자격 증명을 사용하여 얻을 수 있습니다 그러나 권한 부여는 스프링 부트 기본 보안 사용자 (시작할 때 "기본 보안 암호 사용 : 927ca0a0-634a-4671-bd1c-1323a866618a"가 출력 됨)를 사용합니다.어떻게 봄 부팅 및 OAuth2를 예제

제 질문은 어떻게 기본 사용자 계정을 대체하고 실제로 WebSecurityConfig에 의존합니까?

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
protected static class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) 
      throws Exception { 
     authManagerBuilder.inMemoryAuthentication().withUser("user") 
       .password("password").roles("USER"); 
    } 
} 

그러나 설명서에 제시된대로 기본 Spring 사용자/암호를 덮어 쓰는 것처럼 보이지는 않습니다.

이 기능을 사용하려면 무엇이 누락 되었습니까? 나는 2.0.3에 여전히 해요으로

+0

이 아니 당신이 그것을'@Order (SecurityProperties.ACCESS_OVERRIDE_ORDER)를'추가하지 말아야하지 않는 한 - 또한 위의 문제 문에 대한 좋은 참고를 발견했다. 'security.user.name'과'security.user.password' 속성을 설정하여 application.properties 파일에서 기본 사용자 이름/암호를 설정할 수 있습니다. 더 많은 속성에 대해서는 [참조 설명서] (http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)를 참조하십시오. –

+0

더 나은 샘플이 있습니다. (최신 버전은 https://github.com/spring-projects/spring-security-oauth/blob/master/tests/annotation/jdbc/src/main/java/demo/Application입니다.) .java # L81. 그'authenticationManager' 메소드는 2.0.4 스냅 샷에서 새로운 오버 라이드입니다 (2.0.3에서 사용하고자한다면 구현을보십시오). –

+0

@DaveSyer 샘플이 실행되지 않았습니다. "org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration '" –

답변

7
@Configuration 
protected static class AuthenticationManagerConfiguration extends GlobalAuthenticationConfigurerAdapter { 

     @Override 
     public void init(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("min").password("min").roles("USER"); 
     } 

    } 
+0

예, 2.0.3에서 사용했던 것보다 훨씬 좋은 솔루션입니다. –

6

, 나는 몇 가지를 시도하고이 일 것으로 보인다 :

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
protected static class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception { 
     authManagerBuilder 
      .inMemoryAuthentication() 
       .withUser("user1").password("password1").roles("USER").and() 
       .withUser("admin1").password("password1").roles("ADMIN"); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManager() throws Exception { 
     return super.authenticationManager(); 
    } 
} 

명시 적으로 AuthenticationManager에 빈을 정의하여 내장 된 사용자 인증 멀리 가서 그것은 내 자신의 inMemoryAuthentication에 의존하기 시작했습니다. 2.0.4가 출시되면 데이브 (Dave)가 위에 게시 한 솔루션을보다 우아하게 보일 때 다시 평가할 것입니다. (일반적으로 지금의 경우가 될 것이다) 위의 봄 1.5을 사용하고 필요하면 추가 속성을 추가 할 경우 https://github.com/dsyer/sparklr-boot/blob/master/src/main/java/demo/Application.java

봄 1.3입니다 -

+0

OAuth Server 내에서 정적 페이지를 구성해야하고 공개해야하는 경우이 솔루션이 더 적합하다고 생각합니다. 'configure (HttpSecurity http)'와'configure (WebSecurity web)'를 무시하고 그 목적을 위해 특정'antmatchers'를 정의 할 수 있습니다. –

+0

사용자를 데이터베이스에 저장하고이 시점에서 자격 증명을 확인하는 방법을 알고 있습니까? – Marcel

+0

건배 !!! 위의 해결 방법은 @shawn을 사용합니다. – Harleen

3

위의 예는 지적했다.

security.oauth2.resource.filter-order = 3 

문제를 많이 직면 - 또한 다음과 같은 속성을 추가 할 필요가 봄 부팅 1.5을 사용하는 경우 위의

다른 사람들이 지적했듯이 우리는

@Configuration 
@EnableWebSecurity 
public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList") 
      .hasAnyRole("ADMIN").anyRequest().authenticated().and().formLogin() 
      .permitAll().and().logout().permitAll(); 

     http.csrf().disable(); 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception { 
     authenticationMgr.inMemoryAuthentication().withUser("javainuse").password("javainuse") 
      .authorities("ROLE_ADMIN"); 
    } 
} 

중요 포인트를 사용할 수있다 이것을 확인하려고 노력합니다. Spring Boot + OAuth2 Example