programing

Spring Boot 401 보안 없음에도 인증되지 않음

css3 2023. 3. 6. 21:21

Spring Boot 401 보안 없음에도 인증되지 않음

Spring Security를 사용하지 않았는데 인증을 요청하고 있습니다.

여기에 이미지 설명 입력

URL의 예외(http://localhost:8080/SpringJob/ExecuteJob):

{
    "timestamp": 1500622875056,
    "status": 401,
    "error": "Unauthorized",
    "message": "Bad credentials",
    "path": "/SPPA/ExecuteSPPAJob"
}
----below log details
2017-07-21 13:15:35.210  INFO 19828 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/SpringJob]   : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-07-21 13:15:35.210 [http-nio-8080-exec-1] INFO 
                    o.a.c.c.C.[.[localhost].[/SpringJob]-Initializing Spring FrameworkServlet 'dispatcherServlet' 
2017-07-21 13:15:35.211  INFO 19828 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-07-21 13:15:35.211 [http-nio-8080-exec-1] INFO 
                    o.s.web.servlet.DispatcherServlet-FrameworkServlet 'dispatcherServlet': initialization started 
2017-07-21 13:15:35.494  INFO 19828 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 282 ms
2017-07-21 13:15:35.494 [http-nio-8080-exec-1] INFO 
                    o.s.web.servlet.DispatcherServlet-FrameworkServlet 'dispatcherServlet': initialization completed in 282 ms 

application-dev.xml

#Spring Boot based configurations
management.security.enabled: "false"
spring.autoconfigure.exclude:  "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
spring.batch.job.enabled: false
server.contextPath: /SpringJob

build.gradle 스니펫

plugins {
    id 'jacoco'
    id 'org.sonarqube' version '2.5'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: "no.nils.wsdl2java"
apply plugin: 'jacoco'
apply plugin: "org.sonarqube"
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-batch")
    compile("org.springframework.boot:spring-boot-starter-mail")
    //compile("org.springframework.boot:spring-boot-devtools")
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
    compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.1.10'
    compile group: 'org.apache.cxf', name: 'cxf-rt-ws-security', version: '3.1.10'
    compile("org.springframework.boot:spring-boot-starter-actuator")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

컨트롤러

@Controller
@EnableAutoConfiguration
@EnableBatchProcessing
public class MyController {
    @Autowired
    JobLauncher jobLauncher;

    @RequestMapping("/ExecuteJob")
    @ResponseBody
    public String callPrelegalJob(@RequestParam("user") String userName, @RequestParam("password") String password) {
        log.info("Job is to be launched from controller...");
}
}

현재 버전의 스프링 부트(v2.1.0)릴리스) 보안 문제를 해결하는 가장 쉬운 방법은 다음과 같이 프로젝트에 "Web SecurityConfig.java"를 추가하는 것입니다.

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }

}

물론 사이트 간 요청 위조에 대한 보호 기능이 제거되므로 단순한 읽기 전용 엔드포인트에만 적합합니다.

다음 행을 추가해 보십시오.application.properties파일

security.basic.enable: false
security.ignored=/**

spring doc에 따르면security.ignored=

기본 보안 경로에서 제외할 쉼표로 구분된 경로 목록

pom.xml 파일에서 스프링 보안 종속성을 삭제하기만 하면 됩니다.나를 위해 일했다 :)..

CXF 보안과 스프링부트 보안을 사용하면 이 문제가 발생합니다.의존관계를 코멘트 아웃합니다.즉, 스프링 부트보안을 무효로 하고, 그 후에 허가합니다.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>       
</dependency>

이것을 유효하게 하려면 , 커스텀 시큐러티를 작성하거나, 이하의 설정을 추가할 필요가 있습니다.

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll();
    }
}

인증을 무효로 하는 정확한 방법을 찾을 수 없었습니다.하지만 작동기 의존성을 제거함으로써 작동합니다.

compile("org.springframework.boot:spring-boot-starter-actuator")

모든 요구를 무시하도록 springSecurityFilterChain을 설정하여 다음과 같이 모든 엔드포인트에 대한 인증되지 않은 접근을 허용할 수 있습니다.

@Configuration
@EnableWebSecurity
public class WebConfiguration extends WebSecurityConfigurerAdapter {


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

}

@harshit-sharma의 솔루션은 나에게 효과가 있었습니다.메인 어플리케이션 클래스에 제외를 추가했습니다.

@SpringBoot Application(= { 보안 제외)AutoConfiguration.class })

Spring Security는 여전히 과도 의존성을 통해 프로젝트 lib에 포함될 수 있습니다.Spring Boot에서는 다음과 같이 동작합니다.

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/**")
                .permitAll();
    }

주의: http는org.springframework.security.config.annotation.web.builders.HttpSecurity그리고 이거는@Component이미 주입되어 호출될 수 있는org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter 응용 프로그램 프레임워크에서 구현합니다.이것은 모든 기본 제공 catch-all 절을 덮어씁니다.http.authorizeRequests().anyRequest().denyAll()테스트용 등

나는 이 의존성을 제거함으로써 해결했다.pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

언급URL : https://stackoverflow.com/questions/45232071/springboot-401-unauthorized-even-with-out-security