복붙노트

[SPRING] 스프링 보안, 보안 성 및 보안 액세스 없음

SPRING

스프링 보안, 보안 성 및 보안 액세스 없음

먼저 로그인해야하는 작은 응용 프로그램을 만들고 있습니다. 하지만 일부 타사 도구의 경우 로그인이 필요없는 API를 제공하고 싶습니다. 로그인 자체는 잘 작동하지만 API 자체는 작동하지만 Spring Security에 인증을 요구하지 않고 API에 액세스 할 수 있다는 것을 알 수는 없습니다. 나는 여기와 다른 웹 사이트에서 여러 주제를 확인하고 다른 버전을 시도했지만 아무도 효과가 없었다. API에 액세스 할 때마다 로그인 양식으로 전달되어 먼저 로그인해야합니다.

내 코드 내 봄 보안 구성 내에서 지금까지 이렇게 보입니다.

/**
 * configuration of spring security, defining access to the website
 * 
 * @param http
 * @throws Exception 
 */
@Override
protected void configure(HttpSecurity http) throws Exception {        
    http.authorizeRequests()                
            .antMatchers("/rest/open**").permitAll()
            .antMatchers("/login**").permitAll()
            .and()
        .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error")
            .defaultSuccessUrl("/dashboard")
            .loginProcessingUrl("/j_spring_security_check")
            .usernameParameter("username")
            .passwordParameter("password")
            .and()
        .logout()
            .logoutUrl("/j_spring_security_logout")
            .logoutSuccessUrl("/login?logout")
            .and()
        .csrf();
}

그리고 내 컨트롤러 :

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PredictionOpenRestController {

    @RequestMapping("/rest/open/prediction")
    public String getPrediction() {
        return "First Try!";
    }
}

어떻게 든 나는 뭔가를 놓치고 싶다.

해결법

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

    1.자세한 내용은 Spring Security Reference :

    자세한 내용은 Spring Security Reference :

    .authorizeRequests ()를 두 번째 사용하면 첫 번째가 우선 적용됩니다.

    AntPathMatcher도 참조하십시오.

    수정 된 코드 :

    protected void configure(HttpSecurity http) throws Exception {        
        http.authorizeRequests()                
                .antMatchers("/rest/open/**").permitAll()
                .antMatchers("/login/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error")
                .defaultSuccessUrl("/dashboard")
                .loginProcessingUrl("/j_spring_security_check")
                .usernameParameter("username")
                .passwordParameter("password")
                .and()
            .logout()
                .logoutUrl("/j_spring_security_logout")
                .logoutSuccessUrl("/login?logout")
                .and()
            .csrf();
    }
    
  2. from https://stackoverflow.com/questions/39052457/spring-security-secured-and-none-secured-access by cc-by-sa and MIT license