2014-09-03 4 views
8

스프링 보안 자바 설정을 사용하여 웹 애플리케이션을 보호하려고합니다.스프링 보안 자바 설정을 사용하는 동안 기본 인증을 사용하지 않음

이 구성이 모습입니다 : - 내가 명시 적으로 비활성화 HTTP 기본 인증을

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    private String googleClientSecret; 

    @Autowired 
    private CustomUserService customUserService; 

    /* 
    * (non-Javadoc) 
    * 
    * @see org.springframework.security.config.annotation.web.configuration. 
    * WebSecurityConfigurerAdapter 
    * #configure(org.springframework.security.config 
    * .annotation.web.builders.HttpSecurity) 
    */ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     // @formatter:off 
     http 
      .authorizeRequests() 
       .antMatchers(HttpMethod.GET, "/","/static/**", "/resources/**","/resources/public/**").permitAll() 
       .anyRequest().authenticated() 
      .and() 
       .formLogin() 
        .and() 
       .httpBasic().disable() 
      .requiresChannel().anyRequest().requiresSecure(); 
     // @formatter:on 
     super.configure(http); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception { 
     // @formatter:off 
     auth 
      .eraseCredentials(true) 
      .userDetailsService(customUserService); 
     // @formatter:on 
     super.configure(auth); 
    } 
} 

주의 사용 : - 보안 된 액세스하는 동안

.httpBasic().disable() 

난 여전히 HTTP Authenticaton 프롬프트 상자를 얻고있다 url. 왜?

제발 도와주세요. 번들로 제공되는 기본 로그인 양식 만 렌더링하려고합니다.

봄 부팅 스타터 버전 : 1.1.5 봄 보안 버전 : 3.2.5

감사

경우
+6

'security.basic.enabled = false'를'application.properties'에 추가하십시오. 또한 오버라이드 된 메소드에서'super.configure'를 호출해서는 안됩니다. –

+0

@ M.Deinum 그걸 고쳤습니다. 하지만 내가 왜 자바 config에서 명시 적으로 비활성화되었을 때 비활성화되지 않았습니까? –

+0

전체 구성에 각각 기여하는 여러 개의 'WebSecurityConfigurer'를 가질 수 있습니다. 그것은 아주 잘 당신은 기본 인증 및 양식과 정상적인 사이트로 보호되는 귀하의 웹 사이트의 나머지 부분을 가지고있을 수 있습니다. 나머지는'WebSecurityConfigurer'를, 폼은 하나씩 2 개의 WebSecurityConfigurer를 만들 수 있습니다. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html (스프링 부트 참조, 보안 섹션)을 체크 아웃 할 수도 있습니다. –

답변

6

당신은 봄 부트의 documentation 상태 사용

오프 전환하려면를 응용 프로그램에서 @EnableWebSecurity와 함께 bean을 추가 할 수 있습니다.

옵션 일 수있는 자체 기능을 완벽하게 사용자 정의하려는 경우.

그냥 알기 쉽게 ... 메인 애플리케이션 클래스 또는 애플리케이션 구성 클래스에 @EnableWebSecurity 주석을 추가하기 만하면됩니다.

+2

이것은 제게는 효과가 없었습니다 (저는 Spring Boot 클래스와 확장 된 WebSecurityConfigurerAdapter에 @EnableWebSecurity를 ​​넣었습니다). '@EnableWebSecurity가있는 빈을 추가하는'것이 실제로 무엇을 의미합니까? – Ivan

16

먼저 super.configure (http)를 호출하십시오. 그 전에 당신이 생각한 모든 구성을 덮어 씁니다. 이을 시도해보십시오 나를 위해 일한 다음

http 
    .authorizeRequests() 
     .anyRequest().authenticated() 
     .and() 
    .formLogin() 
     .and() 
    .httpBasic().disable(); 
+1

작은 수정 .. .httpBasic(). disable()이어야합니다. – ticktock

-4

:

  http 
       .authorizeRequests() 
       .anyRequest().permitAll(); 
+5

기본 보안을 비활성화하지 않으며 모든 요청에 ​​대한 보안 검사를 무시합니다. – jkerak

1

당신은 다음과 같은 HttpSecurity 인스턴스를 통해 formLogin을 해제 할 수 있습니다 :

http.authorizeRequests().antMatchers("/public/**").permitAll() 
     .antMatchers("/api/**").hasRole("USER") 
     .anyRequest().authenticated() 
     .and().formLogin().disable(); 

이 403 HTTP 오류 때를 수신 이어질 것 어떤 보안 자원에 접근하려고 시도합니다.