복붙노트

[SPRING] Spring 보안은 HttpSecurity를 ​​사용하여 url과 메소드에 대한 요청을 승인한다.

SPRING

Spring 보안은 HttpSecurity를 ​​사용하여 url과 메소드에 대한 요청을 승인한다.

org.springframework.security.config.annotation.web.builders.HttpSecurity를 ​​사용하여 특정 URL에 게시물 요청을 인증 할 수있는 방법이 있습니까?

나는 HttpSecurityas를 사용하고있다.

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .addFilterAfter(new CsrfCookieGeneratorFilter(), CsrfFilter.class)
            .exceptionHandling()
            .authenticationEntryPoint(authenticationEntryPoint)
        .and()
            .rememberMe()
            .rememberMeServices(rememberMeServices)
            .key(env.getProperty("jhipster.security.rememberme.key"))
        .and()
            .formLogin()
            .loginProcessingUrl("/api/authentication")
            .successHandler(ajaxAuthenticationSuccessHandler)
            .failureHandler(ajaxAuthenticationFailureHandler)
            .usernameParameter("j_username")
            .passwordParameter("j_password")
            .permitAll()
        .and()
            .logout()
            .logoutUrl("/api/logout")
            .logoutSuccessHandler(ajaxLogoutSuccessHandler)
            .deleteCookies("JSESSIONID")
            .permitAll()
        .and()
            .headers()
            .frameOptions()
            .disable()
            .authorizeRequests()
                .antMatchers("/api/register").permitAll()
                .antMatchers("/api/activate").permitAll()
                .antMatchers("/api/authenticate").permitAll()
                .antMatchers("/api/logs/**").hasAuthority(AuthoritiesConstants.ADMIN)
                .antMatchers("/api/subscriptions").permitAll()
                .antMatchers("/api/**").authenticated();
}

POST 요청을 / api / subscription 경로에 허용하고 싶습니다. POST 만. 감사.

해결법

  1. ==============================

    1.여기를보십시오 https://github.com/spring-projects/spring-data-examples/tree/master/rest/security

    여기를보십시오 https://github.com/spring-projects/spring-data-examples/tree/master/rest/security

    http
      .httpBasic().and()
      .authorizeRequests()
        .antMatchers(HttpMethod.POST, "/employees").hasRole("ADMIN")
        .antMatchers(HttpMethod.PUT, "/employees/**").hasRole("ADMIN")
        .antMatchers(HttpMethod.PATCH, "/employees/**").hasRole("ADMIN");
    
  2. ==============================

    2.나는이 질문이 조금 오래되었다는 것을 알고 있지만 csrf 지원을 비활성화하는 것이 허용 가능한 대답이라고 생각하지 않습니다. 나는이 같은 문제가 있었지만 csrf.disable ()을 사용하여 좋은 기분을 느끼지 못했습니다. 대신 폼 태그 안에 페이지 하단에 다음 줄을 추가했습니다.

    나는이 질문이 조금 오래되었다는 것을 알고 있지만 csrf 지원을 비활성화하는 것이 허용 가능한 대답이라고 생각하지 않습니다. 나는이 같은 문제가 있었지만 csrf.disable ()을 사용하여 좋은 기분을 느끼지 못했습니다. 대신 폼 태그 안에 페이지 하단에 다음 줄을 추가했습니다.

    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
    
  3. from https://stackoverflow.com/questions/28907030/spring-security-authorize-request-for-url-method-using-httpsecurity by cc-by-sa and MIT license